0

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.

John Doe
  • 139
  • 1
  • 1
  • 9
  • Your problem is not the flush() method. It just makes your programm print the content of the buffer into the file, in which your writer should be writing. (also see http://stackoverflow.com/questions/1742361/flush-in-java-io-filewriter) – huidube Nov 07 '14 at 07:53
  • i dont know what you want to do. Could you explain it? – huidube Nov 07 '14 at 08:04
  • If `flush()` really printed twice, which it doesn't, removing it would cure the problem, which it doesn't. Does it? – user207421 Nov 07 '14 at 08:38
  • Flush doesn't print twice, but the "why" statement prints twice and an extra new line is printed, which is what I don't understand. My program is some user interface that just takes in some input and outputs something. Everything works fine until the end with the extra printed lines. – John Doe Nov 07 '14 at 19:47

1 Answers1

0

I guess it's because you have mentioned '\n' in wtr.write(lunchLine.dequeue() + "\n"); inside the loop and also after exiting the loop again you are writing a new line wtr.write("\n"); which cause two new lines - one from the last iteration inside loop and another from wtr.write("\n")

Raj_89
  • 691
  • 6
  • 5
  • that is what I want. It outputs the dequeued string and goes to the next line. Then I wish to have another newline. But on top of that a third new line is printing. Also the sys.out(why) is occuring twice, which I suspect has to do with the fact that my writer is a printwriter(System.out) – John Doe Nov 07 '14 at 19:53