| Author |
Cannot find symbol parseInt
|
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
hey guys,
when i compile the above program, i get the error that Cannot find symbol method parseInt()..please help me..
|
on job hunt
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
|
What is the JDK version that you are using.
|
Ashwin Sridhar
SCJP | SCWCD | OCA
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
thejwal pavithran wrote:when i compile the above program, i get the error that Cannot find symbol method parseInt()..please help me..
What type is 'args', and what type does Integer.parseInt() take as its parameter?
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
|
in java folder there is jdk1.6.0_18 and jre6 and jre7
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
|
You are trying to pass a array as parameter to Integer.parseInt(), but you should actually pass a String Object.
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
Winston Gutkowski wrote:
thejwal pavithran wrote:when i compile the above program, i get the error that Cannot find symbol method parseInt()..please help me..
What type is 'args', and what type does Integer.parseInt() take as its parameter?
Winston
in the java documentation i have, it says:
it is a static public memeber of Integer class
parseInt(String s)
Parses the string argument as a signed decimal integer.
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
Ashwin Sridhar wrote:You are trying to pass a array as parameter to Integer.parseInt(), but you should actually pass a String Object.
oh i get it... in java, a string array contains references to the string objects and not the strings themselves so i shud use args[0]??let me try it
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5886
|
|
thejwal pavithran wrote:
Winston Gutkowski wrote:
thejwal pavithran wrote:when i compile the above program, i get the error that Cannot find symbol method parseInt()..please help me..
What type is 'args', and what type does Integer.parseInt() take as its parameter?
Winston
in the java documentation i have, it says:
it is a static public memeber of Integer class
parseInt(String s)
Parses the string argument as a signed decimal integer.
But you're not passing it a String. Hence the error.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5886
|
|
thejwal pavithran wrote:
Ashwin Sridhar wrote:You are trying to pass a array as parameter to Integer.parseInt(), but you should actually pass a String Object.
oh i get it... in java, a string array contains references to the string objects and not the strings themselves
No variable ever contains an object in Java. Array variables are not special in that sense. A variable contains a primitive, a reference, or null. A String variable contains a reference to a String object. A String[] variable contains a reference to a String[] object, which in turn contains references to String objects.
so i shud use args[0]??let me try it
If you want to parse the first arg that was passed, yes.
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
|
so how wud i read ints during run time? like scanf or cin?
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
You could have your numbers passed in as a part of arguements in command line. But it would be passed as String and you could convert it into int as you do.
Assume you call your testClass.java and pass 10 as input in command line.
you will do a ,
This 10 would be passed as a String and would be first element in the array args.
So, you convert into int as ,
Remember if you pass anything other than int and try to convert, you will get a NumberFormatException.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
thejwal pavithran wrote:so how wud i read ints during run time? like scanf or cin?
First: Please UseRealWords (←click). This isn't an SMS message.
Second: There's nothing to stop you from piping input to a Java program. On the other hand, things like scanf() are methods, not programs; and cin is a stream, and it's equivalent in Java is System.in.
Winston
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
Ashwin Sridhar wrote:You could have your numbers passed in as a part of arguements in command line. But it would be passed as String and you could convert it into int as you do.
Sir if i want to write like
System.out.println("PLEASE ENTER YOUR NUMBER ");
Then how will I read the value entered by user into an int variable?i am looking for something like cin or scanf
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
|
Then you should use System.in . Look this doc
|
 |
thejwal pavithran
Ranch Hand
Joined: Feb 11, 2012
Posts: 113
|
|
Ashwin Sridhar wrote:Then you should use System.in . Look this doc
thank you.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
thejwal pavithran wrote:Then how will I read the value entered by user into an int variable?i am looking for something like cin or scanf
Class java.util.Scanner would be good for that. Look at the API documentation for that class - it has an example for exactly what you want to do.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Cannot find symbol parseInt
|
|
|