Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with parsing a string from text file.  RSS feed

 
Randy Blankenship
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been beating my head up against a wall on this one. I feel like I have the corect spaces in my substring but obviously somethings wrong. What I am trying to do is read a text file and parse out the pieces like countryName and countryCapital to later be put into an object that gets stored in array for sorting and searching. For some reason I keep getting a string index out of range exception. here is my code for the parsing, I have also attached the text file.
import java.io.*;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
FileReader fis1 = new FileReader("Countries.Summer2012.txt");
BufferedReader br1 = new BufferedReader(fis1);

String inputString;
String countryName;
String countryCapital;
String countryRegion;
int countryRegNbr;
int countryPopulation;

inputString = br1.readLine();//reads one complete line (record) from the input file into inputString.

System.out.println(inputString);

while (inputString != null)
{
countryName = inputString.substring(0,24).trim();
System.out.println (" some Name read in was:" + countryName);
countryCapital = inputString.substring(24,40).trim();
System.out.println (" some cap read in was:" + countryCapital);
countryRegion = inputString.substring(40,64).trim();
System.out.println (" some reg read in was:" + countryRegion);
countryRegNbr = Integer.parseInt(inputString.substring(64,72).trim());
System.out.println (" some regnbr read in was:" + countryRegNbr);
countryPopulation = Integer.parseInt(inputString.substring(72).trim());
System.out.println (" some Name pop in was:" + countryPopulation);
inputString = br1.readLine();
System.out.println(inputString);
}// end while loop
br1.close();
}
}
 
Randy Blankenship
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I don't see the file I attached so here is a couple of samples from the file. The spaces dont show up right and I can't attach a text file so I guess it is going to be hard to help with this one then. but I will try to attach the whole project here.

Norway Oslo Northern_Europe 1 480000
Italy Rome Western_Europe 2 2700000

 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Randy,

my best guess is that not all lines have at least 72 characters. Maybe there are some empty lines in the file?

If you look at the String API, you see that substring(start, end) throws this error when end > length of the string. So before extracting all the fields, check the length of the line.

Also, I've encountered many problems with these so called fixed length files. For instance, after line 1.000.000 the fields after the middle field suddenly shifted a space. So a much better way to get to the individual fields in a line is to use line.split("\\s"). Look at the String and the regex API of what this means. Succes!

Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!