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:


File playerFile = new File(fileName);
			Scanner dataFile = new Scanner(playerFile);

Why would datafile.hasNextLine() return false?

9 Years
Discussion Span
Last Post by ~s.o.s~

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.

			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;
		catch (IOException e) {}

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.