Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading data from a file... getting InputMismatchException?

794365Sep 30 2009 — edited Sep 30 2009
Hi,

The class that I created has a scanner that reads input from a .txt file that follows a specific format. Compiling and running it gives me an InputMismatchException at line 18, so I figure it must be the way I set up the delimiter.

My logic for the code revolves around the fact that I set the scanner delimiter to "|", seperating the text file in to different "categories", while the StringTokenizer's delimiter is white space, which can be used to split strings fed to it by the scanner. Can someone tell me what i'm doing wrong? A link to the text is below:

[Sample Text File|http://www.sendspace.com/file/de086f]
import java.util.Scanner;
import java.util.StringTokenizer;

public class Read
{
	
	public static void main(String[] args) throws Exception
	{
		java.io.File file = new java.io.File("sample_file.txt");
		
		Scanner input = new Scanner(file);
		
		
		while(input.hasNext())
		{
			//Takes the first number in the file as a size for an array, creates the array, then takes the next line (according to the set delimiter) and puts each element in that line in to the array
			input.useDelimiter("|");
			int[] xArray = new int[input.nextInt()];
			StringTokenizer stringTok = new StringTokenizer(input.next());
			
			for(int i=0; i < xArray.length; i++)
			{
				xArray[i] = Integer.parseInt(stringTok.nextToken());
			}
			
			//Takes the next line (according to the set delimiter) and assigns it to a String
			String mapName = input.next();
			
			//Takes the next line (according to the set delimiter) and puts each element in to an array
			stringTok = new StringTokenizer(input.next());
			int[] frameSize = new int[stringTok.countTokens()];
			
			for(int i=0; i < frameSize.length; i++)
			{
				frameSize[i] = Integer.parseInt(stringTok.nextToken());
			}
			
			//Takes the next line as the size of an array, then takes the line after that and puts each element in that line in that array
			int[] startingPos = new int[input.nextInt()];
			stringTok = new StringTokenizer(input.next());
			
			for(int i=0; i < startingPos.length; i++)
			{
				startingPos[i] =  Integer.parseInt(stringTok.nextToken());
			}
			
			//Takes the next line as the size of a 2-dimensional array, then takes each line after that and puts the last two numbers (on each line) and puts them in to the array
			int numStations = input.nextInt();
			int[][] centerCoordinates = new int [numStations][2];
			stringTok = new StringTokenizer(input.next());
			
			for(int i = 0; i < centerCoordinates.length; i++)
			{
				stringTok.nextToken();
				centerCoordinates[0] = Integer.parseInt(stringTok.nextToken());
centerCoordinates[i][1] = Integer.parseInt(stringTok.nextToken());
}

//Takes the next line as the limiting condition for a loop, creates a 2-dimensional array based on numStations, then takes each line, assigns them to appropriate strings, and places the appropriate values in to the array
int numRoutes = input.nextInt();
String[][] map = new String [numStations][numStations];

for(int k = 0; k < numRoutes; k++)
{
stringTok = new StringTokenizer(input.next());

String source = stringTok.nextToken();
String destination = stringTok.nextToken();
String transport = stringTok.nextToken();

if(map[Integer.parseInt(source)][Integer.parseInt(destination)] == "NULL")
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1" + transport;
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1" + transport;
}
else
{
if(transport.equals("T") == true)
{
if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("B") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1W";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1W";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("U") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1X";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1X";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("Y") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1Z";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1Z";
}
}
if(transport.equals("B") == true)
{
if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("T") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1W";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1W";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("U") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1Y";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1Y";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("X") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1Z";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1Z";
}
}
if(transport.equals("U") == true)
{
if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("T") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1X";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1X";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("B") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1Y";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1Y";
}
else if(map[Integer.parseInt(source)][Integer.parseInt(destination)].endsWith("W") == true)
{
map[Integer.parseInt(source)][Integer.parseInt(destination)] = "1Z";
map[Integer.parseInt(destination)][Integer.parseInt(source)] = "1Z";
}
}
}

}



}


}




}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 28 2009
Added on Sep 30 2009
8 comments
2,068 views