Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parse an integer within a java class

Posted on 2006-10-19
7
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 100 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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

660 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