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]);
}


}
}

Recommended Answers

All 3 Replies

Use a Scanner to read the numbers from the text file, and read them in as ints, not as Strings.

Scanner scanner = new Scanner(new File("filePath"));
int next = scanner.nextInt();

Good luck.

PS the reason your current code doesn't work, at first glance, seems to be because you are reading in the numbers one line at a time, not one number at a time. So you have an array of Strings, but if the text file was:

12 11 10 9 8 7 6
5 4 3 2 1 0

The array would have two strings:

1) 12 11 10 9 8 7 6
2) 5 4 3 2 1 0.

Integer.parseInt(String intRepresentation) can only parse something which is an integer. Obviously, that entire line is not an integer - its a whole bunch of integers. Again, this is what I think at a quick glance, I could be wrong, but I'm tired.

Use a Scanner to read the numbers from the text file, and read them in as ints, not as Strings.

Scanner scanner = new Scanner(new File("filePath"));
int next = scanner.nextInt();

Good luck.

PS the reason your current code doesn't work, at first glance, seems to be because you are reading in the numbers one line at a time, not one number at a time. So you have an array of Strings, but if the text file was:

12 11 10 9 8 7 6
5 4 3 2 1 0

The array would have two strings:

1) 12 11 10 9 8 7 6
2) 5 4 3 2 1 0.

Integer.parseInt(String intRepresentation) can only parse something which is an integer. Obviously, that entire line is not an integer - its a whole bunch of integers. Again, this is what I think at a quick glance, I could be wrong, but I'm tired.

-------------------------------------------------------------------------------------
Thanks for the tip........I will use the scanner function and see if it does the job.....
------------------------------------------------------------------------------------

Regarding my original approach.....let me redefine my problem
I have an array words
and word[0]="12"
word[1]="11"
word[2]="10"
I ran a loop and it did print 12 11 10 as desired
but I want another array, array[0]=12 array[1]=11 array[2]=10 so that I can do some manipulations with them
but I am unable to convert (parse)"12" to integer 12

Converting a String like "12" to an integer shouldn't be a problem; just make sure you trim() your strings before parsing them.

By the looks of your stacktrace, it seems that your program is trying to parse an integer out of the string ""0
8 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" which would fail for obvious reasons.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.