Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

entering integer in program

Status
Not open for further replies.

MarkJ2

Technical User
Sep 25, 2001
17
0
0
US
New to Java here.....
I am writing a program which I would like the user to enter an integer. The integer will then be used for some other calculations. My code so far is:

//read in input, cast to char.
n = (char)System.in.read();

//assign 'n' to new String.
s = new String[n];

//using parseInt, change to an integer.
num = Integer.parseInt(s);

However when I run this, I get the error...incompatible type for = can't convert java.lang.String[] to java.lang.String.

Not really sure how to correct the problem.

Thanks for any help.

Mark
 
You need to wrap the InputStream in a BufferedReader and use read it a line at a time. Then you can use parseInt in the input.
 
You get the error because the line
s = new String[n];
tries to assign a String array of size n to s, and you can assign a String array to a String.
The String constructor you are trying to use takes a char array as a parameter.
But why don't you cast into a String in the first place?
 
I have to correct myself i have discovered a spelling mistake, it should say:
".....and you can't assign a String.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top