Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileReader and Text files

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
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

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

 
UPDATE:

While I've still been trying to figure out that Exception error, I've already taken the next step into this program which is the StringTokenizer. I used a for loop to print out the tokens in each line and it almost works, it only prints out partials of each line. The newly updated code is below as well as the results of running the program. Hopefully someone can give me an idea of what's going on?

Thanks,
Jisoo22

Here's the new code:
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
				{

					for (int i = 0; i <= tokenizer.countTokens(); i++)
					{
						System.out.print(word + &quot; &quot;);
						word = tokenizer.nextToken();
					}
					//System.out.println(line);
					System.out.println();
				}

				catch (Exception e)
				{
					System.out.println(e);
				}
				line = inFile.readLine();
			}//while

			inFile.close();

		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}//main
}//class

Here are the results:

Maritime Simulation World
World_size 50
Ship Valdez 2
Ship Dingy Pirate
Island Hawaii 23
java.util.NoSuchElementException
 
Nevermind, I figured it out! =) For those who would like to know, the exception error occurs when there are extra carriage returns after the text in the text file. Just make sure the text file ends flush with the end of the last line of text and it's ok. I also figured out that the countTokens method doesn't work as well as hasMoreTokens(). After that everything ran smoothly.

Jisoo22
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top