0

I am currently having a problem with my loop. After I inputted a string once, it prompts the user and when the loop conditions were met, it just keeps asking the user "do you want to continue?" and was unable to enter another string.

public static void main(String[] args) throws IOException
{
    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
    LinkedList<String> strList = new LinkedList();
    char choice;

    do
    {
        System.out.print("Add Content: ");
        strList.add(bfr.readLine());
        System.out.print("Do you want to add again? Y/N?");
        choice = (char)bfr.read();

    }
    while(choice == 'Y');

}
steveee26
  • 27
  • 3
  • When you enter the `Y`, you also enter a newline after it. Because you only read the one character, you don't read the newline. Just use `readLine` to read your choice as well. – RealSkeptic Feb 10 '20 at 16:08
  • 1
    You can read some good explanations from this [question](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) which is related to `Scanner` instead, but the issue is caused by the same thing. – Nexevis Feb 10 '20 at 16:10

5 Answers5

1

You need to get the newline character out of the keyboard buffer. You can do this like this:

do
{
    System.out.print("Add Content: ");
    strList.add(bfr.readLine());
    System.out.print("Do you want to add again? Y/N?");
    //choice = (char)bfr.read();

    choice = bfr.readLine().charAt(0); // you might want to check that a character actually has been entered. If no Y or N has been entered, you will get an IndexOutOfBoundsException
 }
 while(choice == 'Y');
ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
0

Usually the terminal only sends the data once you hit enter. So you get an empty line when you perform readLine again. You have to read a line, then check if it contains Y instead. Or read the empty line afterwards, whichever you think is less error prone.

I tend to use the earlier and read a full line, then check what it contains.

Maarten Bodewes
  • 80,169
  • 13
  • 121
  • 225
0

Don't forget that for something simple as this program you can use the convenient java.io.Console class.

        Console console = System.console();
        LinkedList<String> list = new LinkedList<>();
        char choice = 'N';

        do {
            System.out.print("Add Content: ");
            list.add(console.readLine());
            System.out.print("Do you want to add again? Y/N?\n");
            choice = console.readLine().charAt(0);

        } while (choice == 'Y' || choice == 'y');
Themelis
  • 2,929
  • 1
  • 11
  • 31
0

This work (don't forget library):

public static void main(String[] args) throws IOException
{
    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
    List<String> strList = new ArrayList<String>();
    String choice;

    do {
        System.out.println("Add Content: ");    
        strList.add(bfr.readLine());
        System.out.println("Do you want to add again? Y/N?");
        choice = bfr.readLine();

    } while(choice.equals("Y"));

    System.out.println("End.");

}
0

Try this :

    public static void main(String[] args) throws IOException
        {
        Scanner scan=new Scanner(System.in);
        LinkedList<String> strList = new LinkedList();
        String choice;`

        do
        {
            System.out.print("Add Content: ");
            strList.add(scan.nextLine());
            System.out.print("Do you want to add again? Y/N?");
            choice = scan.nextLine();

        }
        while(choice.equals("Y"));

    }
Pandey Praveen
  • 95
  • 1
  • 11