reading string in from text file
Hey Guys... im getting an error could you help?
Exception in thread "main" java.lang.NumberFormatException: For input string: "bogus"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at lab10.main(lab10.java:47)
Heres my .txt file
100
200
bogus
300
150
weasel
400
600
250
3.5
800
50
500
450
600
550:confused:
Code Java:
import java.util.*;
import java.io.*;
public class lab10
{
public static void main(String [] args)
{
Scanner inScan;
Scanner fScan = null;
inScan = new Scanner (System.in);
String nextItem;
int nextInt = 0;
int i = 0;
int [] A = new int [5];
boolean check = true;
while (check = true){
System.out.println("Please enter the file to read from: ");
String fName = inScan.nextLine();
try {
fScan = new Scanner (new File(fName));
while (fScan.hasNextLine())
{
nextItem = fScan.nextLine(); // Stores first number into nextItem
// nextLine READS STRINGS
// something going on here.... ^ v reason for this
// TAKES String and makes it INT!
nextInt = Integer.parseInt(nextItem);
A[i] = nextInt; // array
i++; // increments the counter
// System.out.println( " is not an integer -- ignored");
}
System.out.println("Here are your " + i + " items:");
for (int j = 0; j < i; j++)
{
System.out.println(A[j] + " ");
}
} catch (FileNotFoundException e) {
System.out.println("Your file is invalid ");
}
}// end while
}
}
Re: reading string in from text file
You are trying to convert each line of the file into an integer,
Code java:
nextInt = Integer.parseInt(nextItem);
So when you hit a text string containing non numeric characters you are going to have a problem. The method parseInt() throws an exception NumberFormatException when it tries to parse a string that contains non numeric data, you need to find a way of handling this. Either use a try/catch block to catch the exception and deal with it appropriately or parse the strings yousrself beforehand to check if they are inetegers or not.
Integer (Java Platform SE 6)
Chris
Re: reading string in from text file
Hello basketball8533.
Welcome to the Java Programming Forums.
The clue is in the exception. java.lang.NumberFormatException: For input string: "bogus"
The String "bogus" in the txt file is causing the issue because you are adding the values to an integer array.
You cannot add Strings to an integer array.
I notice there is also a double value in the txt file.
Re: reading string in from text file
Quote:
Originally Posted by
Freaky Chris
You are trying to convert each line of the file into an integer,
Code java:
nextInt = Integer.parseInt(nextItem);
So when you hit a text string containing non numeric characters you are going to have a problem. The method parseInt() throws an exception NumberFormatException when it tries to parse a string that contains non numeric data, you need to find a way of handling this. Either use a try/catch block to catch the exception and deal with it appropriately or parse the strings yousrself beforehand to check if they are inetegers or not.
Integer (Java Platform SE 6)
Chris
Ah you just beat me to it!
Re: reading string in from text file
nextItem = fScan.nextLine(); // Stores first number into nextItem
// nextLine READS STRINGS
// something going on here.... ^ v reason for this
// TAKES String and makes it INT!
nextInt = Integer.parseInt(nextItem);
You could just have
int x = fScan.nextInt();
However, bogus is a String.
How can you turn it into an int?