Hi guys,
I need to open a file and extract numbers from it.
The numbers in the file are like this
12 34 23 12 00 10 02 12
22 33 10 12 09 03 03 93
etc
I am able to extract the numbers in string format and store it in a string array but I am not able to print it when I convert it to int(parse)
I am getting this error
Exception in thread "main" java.lang.NumberFormatException: For input string: "0
8 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at WordReader.main(WordReader.java:32)
This is the code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
public class WordReader {
public static void main( String[] args ) {
int h=0;
List<String> wordList = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader( new FileReader( "grid.txt" ) );
String word;
while( ( word = br.readLine() ) != null )
wordList.add( word );
} catch( IOException e ) {
e.printStackTrace();
} finally {
try {
br.close();
} catch( IOException ex ) {
ex.printStackTrace();
}
}
String[] words = new String[ wordList.size() ];
wordList.toArray( words );
for( int i = 0; i < words.length; i++ ){
System.out.println(words[i]);}
int array[] = new int[words.length];
for(int j=0;j<words.length;j++) //IF I COMMENT OUT THIS LOOP THEN IT WORKS FINE AND PRINTS THE STRINGS
{
array[j] = Integer.parseInt(words[j]);
System.out.print(array[j]);
}
}
}