Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

bufferedreader reads a specific line but line doesn't show up

$
0
0
First of all, hello everyone!
I joined this webiste because I had a problem with the following code, and found half of the answer on this forum
package oben;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BensRoom
{
	public static void BensRoomfromFlur()
	{
		try
		{
			int lines = 0;
			BufferedReader BensRoomdirection = new BufferedReader(new InputStreamReader(System.in));
			BufferedReader Bensroomdata = new BufferedReader(new FileReader("C:\\Jonas\\programs\\hi.txt"));
			String fromBensRoom = BensRoomdirection.readLine();
			String write = null;
			while((write=Bensroomdata.readLine()) != null)
			{
	            if(++lines == 3)
	            {	
	            	System.out.println(write);
	            }
			}
			if(fromBensRoom.equals("back"))
			{
				System.out.println("You just went back into the hallway");
				Hallway.HallwayfromBen();
			}
			else System.out.println("You have to type in back!");
		}
		catch(java.io.IOException ioerror)
		{
			System.out.println("The characters you typed in were invalid!");
		}	
	}
}


The problem is, it reads the 3rd line of the file, but doesn't display it until the user types in "back".
Another problem: I added
Bensroomdata.close();
inside the while statement and it did the same as before, just that it would show the IOException (notice that I didn't close BensRoomdirection, which is the other reader).
thanks for an answer.
-Jonas

Viewing all articles
Browse latest Browse all 51036

Trending Articles