Solved

parse an integer within a java class

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries email authority 6 112
Selenium and Interactive Data Language 3 76
Data Analysis 7 59
How to properly export data from two SAS datasets to one XML data file? 5 46
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…

739 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