Solved

parse an integer within a java class

Posted on 2006-10-19
7
234 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

791 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