Why does Java make my variables become static?
-
I'm supposed to get user input of Integers so I don't want it them to be static but it keeps complaining that I have a static reference. C programs don't do this. I get the same Java Error in at line 21: cannot make static reference to non-static Integer for this line: Integer intResult = Integer.parseInt(result); //error: cannot make static reference to non-static Integer or I tried using the primitive type int: int intResult = result; //error: cannot make static reference to non-static Integer but really I need to get it to be an object Integer and I also tried putting new operator but that gets the same error: Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer but i still get the same error. What I am trying to do is read integers until a user enters Ctrl-C. But I am afraid writing this in Java doesn't seem be the same way as C. (1) I would greatly appreciate some help in getting rid of error and (2) also how to make it stop requesting user input at Ctrl-C without getting exception error. Some example links would also be helpful. I'm not very experience with Java. I know how to do it in a C program but Java seems much complicated to me anyways. view plaincopy to clipboardprint? Java Syntax (Toggle Plain Text) private static void createTree(){ ArrayList arrayOfInts = new ArrayList(); T t = new T(); TreeNode root = t.getRoot(); System.out.println("Please enter a list of integers:"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String result = null; try { while((result = in.readLine()) !=null) { Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer // Integer intResult = result; //error: cannot make static reference to non-static Integer System.out.println("intResult:"+intResul… // insertInOrder(root, intResult); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //this means your done inputing. //this means your done inputing. } /** * Instantiates a new t. */ public T() { super(); root = new TreeNode(); }
-
Answer:
Try Integer intResult = new Integer(Integer.parseInt(result)) Or Integer intResult = new Integer(result); //if you don't care for exceptions
Snowflake C at Yahoo! Answers Visit the source
Related Q & A:
- Does Java have global variables?Best solution by Stack Overflow
- Why does alcohol make you "lose your inhibitions?Best solution by Yahoo! Answers
- Why does helium make your voice change?Best solution by Yahoo! Answers
- Why do antihistamines make you hungry?Best solution by Yahoo! Answers
- Why is it so difficult to become an NGO?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.