0

The first time this loop iterates works well but after I press the char 'y' in order to repeat, the next time it shows, it won't let me enter another name. I don't know what may be causing this, but it reminds me of C when you need to clear the input buffer.

Any help will be certainly appreciated.

byte counter = 1;
boolean myCondition = false;
List<String> myList = new ArrayList();
BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));

do{
    System.out.println("Enter the #" +counter +" person's name:");
    // low quality validation
    String dude = objReader.readLine();

    while (myList.contains(dude)){
        System.out.println("Man, this dude:" +dude +" is already loaded into the List! Pick another name:\n");
        dude = objReader.readLine();            
    }

    myList.add(dude);
    counter++;
    System.out.println("Would you like to add another name to the list? yes/no");

    char myChar = (char) System.in.read();

    if (myChar == 'y' || myChar == 'Y')
        myCondition = true;
    else
        myCondition = false;

    } while (myCondition);
andersoj
  • 22,406
  • 7
  • 62
  • 73
Cristian
  • 359
  • 3
  • 6
  • 12
  • Seems like your problem indeed. I think using `Scanner` will make it all easier. – MByD May 16 '12 at 17:27
  • Sorry, I'm just learning, but I thought Scanner was intented to read files rather than console input. Am I wrong? I want to use the best method. – Cristian May 16 '12 at 17:31
  • Scanner is used to read input streams, that includes the standard input. – MByD May 16 '12 at 17:32
  • I tried using Scanner and it does the same thing :S – Cristian May 16 '12 at 17:35
  • Your problem is that you are mixing input via the BufferedReader, which buffers, and System.in, which is what the BufferedReader is trying to buffer. Don't do that, it doesn't work. The deleted answer by @Jeremy Heiler leads to an answer, perhaps not immediately, or doing it all via a single something else like a Scanner as several posters have suggested. – user207421 May 17 '12 at 03:19

4 Answers4

2

Look at your code:

  1. You read character 'y'
  2. The char myChar = (char) System.in.read(); waits until you press Enter
  3. myChar is now 'y'. This leaves '\n' (`Enter') in buffer
  4. Next String dude = objReader.readLine(); reads line ended by '\n' that is present already in buffer.

You should read whole line instead simple read()

If you want better resolution of y:

String line = objReader.readLine();
myCondition = line.startsWith("Y") || line.startsWith("y");
Xeon
  • 5,949
  • 5
  • 31
  • 52
  • Interesting, but when I do: char myChar = (char) objReader.readLine(); it wont let me cast string to char. – Cristian May 16 '12 at 17:39
  • I guess I'll have to say goodbye to my char variable and use a String instead. I wanted to use a single char, but I don't know how to do it. – Cristian May 16 '12 at 17:40
  • 1
    If you are certain that in buffer will allways be 1 character followed by `\n' use: String line = objReader.readLine(); char myChar = line.charAt(0); – Xeon May 16 '12 at 17:41
0

Try moving BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in)); inside your do/while loop. Should reinitialize it each time, that way.

ametren
  • 2,186
  • 15
  • 19
0

Try this,

System.out.println("Would you like to add another name to the list? yes/no");
Scanner scan = new Scanner(System.in);
String myChar = scan.nextLine();

if (myChar.equals("y") || myChar.equals("Y"))
    myCondition = true;
else
    myCondition = false;
Jeremy
  • 22,188
  • 4
  • 68
  • 81
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Works fine using a scanner and a couple of other minor code changes.

 byte counter = 1;
            boolean myCondition = false;
            Scanner scan = new Scanner(System.in);
            List<String> myList = new ArrayList();

            do{
                  System.out.println("Enter the #" + counter + " person's name:");
                  // low quality validation
                  String dude = scan.nextLine();
                  while (myList.contains(dude)) {
                    System.out.println("Man, this dude:" + dude + " is already loaded into the List! Pick another name:\n");
                  }
                  myList.add(dude);
                  counter++;
                  System.out.println("Would you like to add another name to the list? yes/no");
                  String choice = scan.nextLine();
                  if (choice.equalsIgnoreCase("Y")) {
                    myCondition = true;
                  } else {
                    myCondition = false;
                  }

             } while (myCondition);

I ran it and entered a total of seven names before I exited the program.

ChadNC
  • 2,528
  • 4
  • 25
  • 39