Hello all,
I'm trying to build a simple program that reads in strings from a text file and then display them within java. The program works for the most part but having one small problem. After all the text is displayed, I get an exception error called "NoSuchElementException" on the next line. The code I have is below, can anyone give me an idea of how to get rid of that exception error (besides putting in "System.out.println("" for the last catch segment, that's a cheap way to do it lol)? The text file I used is right underneath.
Thanks,
Jisoo22
Text file:
Maritime Simulation World State
World_size 50 50
Ship Valdez 2 36
Ship Dingy Pirate 3 37
Island Hawaii 23 42
I'm trying to build a simple program that reads in strings from a text file and then display them within java. The program works for the most part but having one small problem. After all the text is displayed, I get an exception error called "NoSuchElementException" on the next line. The code I have is below, can anyone give me an idea of how to get rid of that exception error (besides putting in "System.out.println("" for the last catch segment, that's a cheap way to do it lol)? The text file I used is right underneath.
Thanks,
Jisoo22
Code:
import java.io.*;
import java.util.StringTokenizer;
public class SimModel_1_1
{
public static void main (String[] args)
{
String inFileName = "world.txt";
String outFileName = "new-world.txt";
String line, word;
StringTokenizer tokenizer;
try
{
FileReader fr = new FileReader (inFileName);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
while (line != null)
{
tokenizer = new StringTokenizer (line);
word = tokenizer.nextToken();
try
{
System.out.println(line);
}
catch (Exception e)
{
System.out.println(e);
}
line = inFile.readLine();
}//while
inFile.close();
}
catch (Exception e)
{
System.out.println(e);
}
}//main
}//class
Text file:
Maritime Simulation World State
World_size 50 50
Ship Valdez 2 36
Ship Dingy Pirate 3 37
Island Hawaii 23 42