I have code that prints an extra new line that I don't need. Also if i add in a system.out.println(str), then that str is printed twice which I don't understand.
The code is
public void run() throws IOException
{
boolean zeroEntered = false;
int scenario = 1;
//while a zero hasn't been entered
while(!zeroEntered)
{
boolean stop = false;
String input;
int numTeams = rdr.nextInt();
Teams teams = new Teams(numTeams);
//if there are 0 teams, then stop
//and set zeroEntered to true
if(numTeams == 0)
{
stop = true;
zeroEntered = true;
}
else
{
//else write the scenario number
wtr.write("Scenario #" + scenario + "\n");
wtr.flush();
scenario++;
}
//for the number of teams entered index
for(int index = 0; index < numTeams; index++)
{
int numPlayers = rdr.nextInt();
//add the number pNum of players
for(int pNum = 0; pNum < numPlayers; pNum++)
{
//adds the player scanned
int player = rdr.nextInt();
teams.addPlayer(player, index);
}
}
AthleteQueue lunchLine = new AthleteQueue(teams);
//while there is still entry, and stop hasn't been entered
while(rdr.hasNextLine() && !stop)
{
input = rdr.next();
//if enter , then enqueue the next read player
if(input.equals("ENTER"))
{
lunchLine.enqueue(rdr.nextInt());
}
//else serve, enqueue and print
else if(input.equals("SERVE"))
{
wtr.write(lunchLine.dequeue() + "\n");
wtr.flush();
}
//stop if there is no entry
else
{
stop = true;
}
}
//two lines after? why?
wtr.write("\n");
}
wtr.flush();
System.out.println("WHY");
}
This method is within a class that has a Scanner rdr and Writer wtr instance variable. In this case, a bufferedReader and a printWriter was passed in. The beginning code is somewhat irrelevant. I just included it just in case the error may be there. In the last few lines, the an extra new line is printed and an extra why is printed. I believe this is because of the flush method. Could someone explain why this is happening and how I could fix it. I appreciate the help.