Reading a multiple line file with StringTokenizer?
I have a file of integers:
I want to read the file and put 3, 5, and 6 in separate integers. This is my code so far:
Eclipse outputs these warnings:
public class milk2
public static void main(String args) throws IOException
BufferedReader f = new BufferedReader(new FileReader("milk2.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milk2.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
int i1 = Integer.parseInt(st.nextToken());
f.readLine(); // I put this in to skip the space in between the first two integers
int i2 = Integer.parseInt(st.nextToken());
int i3 = Integer.parseInt(st.nextToken());
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
I know the problem is with reading the second line because if I get rid of int i3, the first two integers are read and stored to i1 and i2 like they should be. But I can't read the next line. So how do I skip to the next line and continue reading?
Re: Reading a multiple line file with StringTokenizer?
StringTokenizer is a legacy class that should no longer be used.
You should be using String.split() instead.
That will give you an array of Strings that you can then work with.
What's happening with yours is that you are trying to read in 3 tokens when the first line only has 2.