package mp1similar;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MP1Similar
{
private static ArrayList arrayList ;
public static void main(String[] args)
{
ArrayList arrayList= null;
try
{
BufferedReader br = new BufferedReader(new FileReader("data/Catalog.txt"));
String line="";
arrayList =new ArrayList();
while((line = br.readLine())!=null)
{
// System.out.println(line);
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens())
{
//System.out.println(st.nextToken());
arrayList.add(st.nextToken());
System.out.println(br.readLine());
}
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
I am trying to pass data from a TXT file to an array of objects. The TXT fil elooks something like this:
4950331 hist
37.5 121.5 — AS – 5.2 1.0
8270000 hist
51.10 12.80 — EU – 4.8 1.0
The data is separated by whitespace’s. All the contents have to be transferred to objects.
This will print everything in the ArrayList to the screen: