Solved

parse an integer within a java class

Posted on 2006-10-19
7
233 Views
Last Modified: 2010-04-16
hi, i m new in java and got some problems here.

in the area "I want to get this value here: " + apache, i want to parse xyz_here  to  apache, i.e. apache: 2000, however

i got these from my console:

::: Case 4 :::       2000
I want to get this value here: 0

what should i do to parse the integer? here below is some of my codes.......my java file can be compiled without problem but just wanna get that apache: 2000 but not 0......thanks experts!




    //Added 20061017
    private static final String PKEY_BATCH_SIZE = "interface.batch.size";
    Integer apache = new Integer(0);
    //Added 20061017

    private ArrayList pConfigs = new ArrayList();
    private String pSchema;
    private String pLogger;
    private String pDatePattern;

//    private String pSeqPath;
    private static HashMap pConnectionMap = new HashMap();
    private static HashMap pSeqMap = new HashMap();

    public static void main(String[] args) {      
        Properties prop = new Properties();
       

        try {
           



            //added  (start)
//

    int xyz_here = 0;
            if (prop == null) {
                xyz_here = 1000;
                System.out.println("::: Case 1 :::       " + xyz_here);
            }
            if (Integer.valueOf(prop.getProperty(PKEY_BATCH_SIZE).trim()).intValue() == 0) {
                xyz_here = 1000;
                System.out.println("::: Case 2 :::       " + xyz_here);
            }
            if (Integer.valueOf(prop.getProperty(PKEY_BATCH_SIZE).trim()).intValue() <= 1000) {
                xyz_here = 1000;
                System.out.println("::: Case 3 :::       " + xyz_here);
            }
            if (Integer.valueOf(prop.getProperty(PKEY_BATCH_SIZE).trim()).intValue() > 1000) {
                //xyz_here = 1000;
                xyz_here = Integer.valueOf(prop.getProperty(PKEY_BATCH_SIZE).trim()).intValue();
                System.out.println("::: Case 4 :::       " + xyz_here);
            }
            Integer apache = new Integer(xyz_here);
         
           
            //added (end)
        }
    }

    public void start(InterfaceConfig config) {
   

System.out.println("I want to get this value here: " + apache);

    }
0
Comment
Question by:manfredino
  • 3
7 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 25 total points
ID: 17764495
This line:

  Integer apache = new Integer(xyz_here);

needs to become:

  apache = new Integer(xyz_here);
0
 

Author Comment

by:manfredino
ID: 17771009
i tried to replace it as apache = new Integer(xyz_here); but can't compile it......
my JDK there's something wrong with this row
0
 

Author Comment

by:manfredino
ID: 17771023
the error shows non-static variable apache cannot be referenced from a static context
0
 
LVL 2

Assisted Solution

by:harshgrover
harshgrover earned 25 total points
ID: 17772216
declare Apache as static, and then do what manfredino suggested.

another way to do this would be as follows.

assuming that the classname for your class is ABC,

class ABC{
   .
   .
   .

    Integer apache = new Integer(0);

    public static void main(String args[]){
        .
        .
        .
        ABC abc = new ABC();
        abc.apache = new Integer(xyz_here);
       
    }

    start{
        System.out.println(("I want to get this value here: " + apache);
    }

}


you should get 2000 for both the ways that i defined above.

The reason you are getting 0 in your start function is because of this.

you created a global variable apache. then, within your main, you created another variable, again named apache. but this was the local variable, local to the method main. When you tried to access apache from the method start, you accessed the global variable apache, and not the local variable apache, the one which you set to 2000.

in the second instance, your code does not compile. that again is because of the fact that you are trying to access apache, without having created an instance of the parent class.

gets kinda confusing, but once you know it, you would never forget it.

this link should help you in understanding the scope of variables.

http://java.about.com/od/beginningjava/l/aa_vars1.htm

lemee know if you are still confused.

Gary
0
 

Author Comment

by:manfredino
ID: 17786267
it works now! thank you for your help, experts
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question