user4766730
user4766730

Reputation:

Looping with User Input

I am a newbie to Java programming and I'm working on this self project. I'm wondering how do i loop a user's input then write to a file, but upon entering a text test to see if the input is correct. So here is where I get confused.

 System.out.println(" enter what you want to teach me"); // data
        while (!Keyboard.hasNext()) {

            String lines = Keyboard.nextLine();
            System.out.print(" is this correct ? ");
            System.out.println("\n");
            String go = input.nextLine();

            if ( go == "yes"){
                wr.write(lines);
            wr.write("\n");  }

            wr.newLine();
            wr.close();
        }

The loop is never reached and I'm lost to why this is happening.

Upvotes: 2

Views: 122

Answers (3)

Adam Fendley
Adam Fendley

Reputation: 66

A couple pointers:

  1. Assuming "Keyboard" is a Scanner(System.in), Keyboard.hasNext() will never return false - that is, your while loop will never end. This is because the Scanner will just wait for more input; scanning the console is not like scanning a file where there is a finite number of lines.
  2. You cannot compare Strings with ==, you must use the equals method. Look this up if you don't understand why, but you must use if (go.equals("yes")) { ... }

Upvotes: 1

Paul Warnick
Paul Warnick

Reputation: 933

Try removing the ! in your while statement. If I'm understanding this correctly you want the loop to run until Keyboard has been iterated through. With the ! being there the loops condition is never true.

Upvotes: 2

Imran Shahid
Imran Shahid

Reputation: 1

debug it and you will find your answer. If someone gives you a correct code now, you will be stuck like forever in this loop.

Upvotes: 0

Related Questions