I'm trying to read in from a .txt file with a Scanner (see code). For some reason this line of code will never execute even though it can open the file and there is, in fact, data in it as such:

Name
Name
Number
Number

File playerFile = new File(fileName);
			Scanner dataFile = new Scanner(playerFile);
			while(dataFile.hasNextLine())

Why would datafile.hasNextLine() return false?

its not open from another application is it?

also wrap a try catch around and check and make sure it is accessing it properly

No other application has hte file open.

try
		{
			File playerFile = new File(fileName);			//new File playerFile
			Scanner dataFile = new Scanner(playerFile);		//new Scanner dataFile
			while(dataFile.hasNextLine())					//while there is a next line
			{	
				Player temp = new Player(dataFile.nextLine(), dataFile.nextLine(), dataFile.nextFloat(), dataFile.nextFloat(), dataFile.nextInt(), dataFile.nextInt());
				thePlayers[numPlayers] = temp;
				numPlayers++;
			}
			dataFile.close();
		}
		catch (IOException e) {}

and is an exception caught or no?

No exception is caught when this loop executes. I'm really stumped! :(

just by chance, try moving the bracket after the while loop first comment, to be on the same line as the while statement

also did you initialize your array thePlayers?
trying taking everything out, and in the while loop just do a println on dataFile.nextLine()

think there might be something with player in there possibly affecting it

No those changes didn't affect it's ability to read from the file. I'm going to try changing some other pieces of code though.

Ok it definitely is not finding the first line in the file. I simply removed the test for the first line and it chocked up and died a spectacular java ioexception death. The Players.txt file is in the same directory so I'm not sure why it can't find the first line.

for some reason the file doesn't start with a eof character does it?

try recreating the file and make sure its closed

I'd be willing to bet that if you changed your code to this, then your code would work. I've never actually seen somebody use "File" as an argument to a Scanner, so excuse me if I'm wrong, but I don't think it should be used. File is a class that represents a File, is it not? A File and a FileInputStream are not the same thing, I don't think. Try this..

Scanner whatever = new Scanner(new FileInputStream(yourFileHere));

> for some reason the file doesn't start with a eof character does it?

EOF isn't a character, this is one of the reasons why the read method in Java and the getchar / fgetc function in C returns an int instead of a char [hint: since char is unsigned, there would be no way of differentiating between a valid character and an EOF being reached]. An EOF is signaled when no more bytes could be read from the underlying stream / file. EOFException , -1 and null are some of the ways used to signal an EOF in Java.

In my experience, hasNextLine( ) goes crazy when the Scanner is created with new Scanner (new File(...)) and there are non-ascii characters in the file (such as á, ñ, ..). As suggested, by creating the Scanner new Scanner (new FileInputStream(...)) the problem disappears.

Interesting...
A quick look at the source code for Scanner shows that the File constructor immdiately creates a FileInputStream from the file and proceeds with that, so just passing the stream rather than the file shouldn't make any difference.
Problems with non-ascii chars are much more likely to be a problem with the default charset not matching the actual file contents. The best practice is always to specify the appropriate charset in the constructor call.
It's just another example of why Scanner is a total fail as a quick solution for novices (eg getting the next line after geting an integer).
You may find this amusing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.