Solved

parse an integer within a java class

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now