Skip to main content
gamedev.net
🔒 Locked

Java String To Int Conversion Problem!!

Started by jhwc1138 Sep 30, 2002 at 4:54 PM 4 replies 550+ views
Original Post
jhwc1138
jhwc1138
Hi All!! When I''m doing homework it seems that type conversions hang me up more than anything! Why can''t I just assign a String to an Int Dammit! Anyway... I''m reading in a .txt file of numbers seperated by \n charecters. My code looks like this:
  
String Temp;
Int IntTemp;

for (int i = 0; i < numLines; i++
{
Temp = inputFile.readLine();
IntTemp = java.lang.Integer.parseInt(Temp)
}
  
This works fine execpt when I call parseInt I only go through the loop once!! If I comment out the parseInt and System.out.println(Temp); It goes through numLines times like it should! What gives? Is there a better way to convert these types! Thanks!
jhwc1138
jhwc1138
Ok... but can I then do this?

  
int a[] = new int[numLines]
for(int i = 0; i < numLines; i++)
{
Integer myInt;
inputLine = file.readLine();
myInt = new Integer(inputLine));
a[i] = myInt;
}
[\source]
Argus
Argus
You can''t assign an Integer object to a variable of type ''int'' as far as I know. If you have some String x, then :

int y = Integer.parseInt(x);

will convert your String to an int. Or to use your previous example :

a = Integer.parseInt(inputLine);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.