3

I am trying to create a simple console program that asks a user if they want to create a list and if 'yes', allow them to enter the name of the list. It should then 'print' the name of the list before exiting the program.

My code allows the user to say y or n, for the first part but in my conditional statement it does not let the user input the name of the list; it just completes the program. There is no error message; it just isn't functioning as I expected. Here is my code:

public static void main(String[] args) throws IOException     
{
    getAnswers();
}

public static void getAnswers()throws IOException{
    char answer;
    String listName;        
    BufferedReader br = new BufferedReader 
            (new InputStreamReader(System.in));        
    System.out.println ("Would like to create a list (y/n)? ");

    answer = (char) br.read();        
    ***if (answer == 'y'){
        System.out.println("Enter the name of the list: ");
        listName = br.readLine();
        System.out.println ("The name of your list is: " + listName);}***
    //insert code to save name to innerList
    else if (answer == 'n'){ 
        System.out.println ("No list created, yet");
    }
    //check if lists exist in innerList
    // print existing classes; if no classes 
    // system.out.println ("No lists where created. Press any key to exit")
    }

Thank you in advance for your time and help in this!

1
  • I started trying to do some debugging and for my vairable "String listName", it is not be recognized as a variable; therefore nothing is being passed to it. When I hover over listName is says "listName => "listName" is not a known variable in the current context.< Commented Feb 10, 2013 at 15:46

3 Answers 3

4

Change

answer = (char) br.read();

To

answer = (char) br.read();br.readLine();

In order to read the newline after the user presses y or n

Full Code:

import java.io.*;

class Test {

    public static void main(String[] args) throws IOException {
         getAnswers();
    }

    public static void getAnswers() throws IOException {
         char answer;
         String listName;        
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        
         System.out.println ("Would like to create a list (y/n)? ");
         answer = (char) br.read(); 
         br.readLine(); // <--    ADDED THIS 
         if (answer == 'y'){
             System.out.println("Enter the name of the list: ");
             listName = br.readLine();
             System.out.println ("The name of your list is: " + listName);
         }
         //insert code to save name to innerList
         else if (answer == 'n') { 
             System.out.println ("No list created, yet");
         }
         //check if lists exist in innerList
         // print existing classes; if no classes 
         // system.out.println ("No lists where created. Press any key to exit")
    }
}

Ouput:

Would like to create a list (y/n)? 
y
Enter the name of the list: 
Mylist
The name of your list is: Mylist

Is this the output you are expecting?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes that is exactly what I was trying to do! Thank you :)
I had to wait 5 minutes before I could mark it as the answer. One thing though, user000001, you're missing the semi-colon after br.readLine(), idk if there's a way I can just add it for you or if have to edit it.
Oh ok didn't know that limitation... Fixed the bug
2

the Problem is read(). it does not read newLine which comes because of Enter.

 answer = (char) br.read();        // so if you enter 'y'+enter then -> 'y\n' and read will read only 'y' and the \n is readed by the nextline.
    br.readLine();    // this line will consume \n to allow next readLine from accept input.

1 Comment

Arpit, thank you for the added insight in how it's working, technically, I appreciate your time!
0
...
String answer = br.readLine ();
if (answer.startsWith ("y")) {
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.