I am working through some programming interview questions. Got a couple of books to go over. Figured it would help me for interviews in the near future.
I came across a bug really fast, which makes me think I am rusty as the past semester I had mostly theory classes.
I have a text file saved where it has something like this in it. 1 1 1 2 3 3 3 5 5 7 and I am simple trying to read in the file and turn the string values into Ints. But I keep getting this error.
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1 1 4 4 5 6 6 6 7 8 8 8 8"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at testing.main(testing.java:29)
showing code below
I came across a bug really fast, which makes me think I am rusty as the past semester I had mostly theory classes.
I have a text file saved where it has something like this in it. 1 1 1 2 3 3 3 5 5 7 and I am simple trying to read in the file and turn the string values into Ints. But I keep getting this error.
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1 1 4 4 5 6 6 6 7 8 8 8 8"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at testing.main(testing.java:29)
showing code below
Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class testing {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> resultCount = new ArrayList<Integer>();
ArrayList<Integer> sourceNum = new ArrayList<Integer>();
int value = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\Documents D\\testCase1.txt"));
// read the first line from the text file
String fileRead = br.readLine();
while(fileRead != null) {
String[] eachNumber = fileRead.split(" ");
for(int i = 0; i < eachNumber.length; i++) {
value = Integer.parseInt(eachNumber[i]);
}
fileRead = br.readLine();
}
}//end of try catch statement
catch (FileNotFoundException ex)
{
System.out.println("file not found");
}//end of catch statement
catch (IOException ioe)
{
ioe.printStackTrace();
}//end of catch statement
}
}