0

I've searched around and can't find a solution to my problem. For one of my courses I'm tasked with writing a simple console email system. When the user is prompted for a message body, the string variable that holds the input only takes one word, not the whole message. I read that I should use readLine(), but when the program prompts the user to enter a message body, it completely skips input for that segment. To remedy this, I read that I should place a skip("\n") before I read the line. Now, however, the program doesn't end after the user presses Enter. It just keeps taking input and won't let me do anything.

Here is a bit of the code to help:

            Scanner in = new Scanner(System.in);
            //Declare variables
            String subject, reciepent, message = "";
            //Get subject
            System.out.println("> Enter subject: ");
            subject = in.next();
            //Get reciepent
            System.out.println("> Enter reciepent: ");
            reciepent = in.next();
            //Get message
            System.out.println("> Enter message: ");
            in.skip("\n");
            message = in.nextLine();
            //Print out message to verify user input
            System.out.println("> " + message);

I don't understand how nextLine() works for all the threads I searched, but not for me.

Dev909
  • 75
  • 1
  • 5
  • So right after `recipent = in.next();` put a `in.nextLine();` and see if that helps. Also im quite sure you can get rid of `in.skip("\n");` unless you have that there for a reason. – 3kings Oct 21 '15 at 19:52
  • Its because `next()` does not consume the newline, see : http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Mike R Oct 21 '15 at 20:06

3 Answers3

0

Try using nextLine() which waits for user input and then returns the input. Your code should be like this:

Scanner in = new Scanner(System.in);
//Declare variables
String subject, recipient, message;
//Get subject
System.out.println("> Enter subject: ");
subject = in.nextLine();
//Get recipient
System.out.println("> Enter reciepent: ");
recipient = in.nextLine();
//Get message
System.out.println("> Enter message: ");
message = in.nextLine();
//Print out message to verify user input
System.out.println("> " + message);
0

How about

Scanner s = new Scanner(System.in);

String line = "";

while((line = s.nextLine()) != null) {

   System.out.println("> Enter subject: ");

subject = in.nextLine();

//Get recipient
System.out.println("> Enter reciepent: ");
recipient = in.nextLine();

//Get message
System.out.println("> Enter message: ");
message = in.nextLine();

//Print out message to verify user input
System.out.println("> " + message);

}
AbtPst
  • 6,676
  • 13
  • 69
  • 138
0

The solution to this problem is not to use the .skip() method, but use .nextLine() instead, which removes the trailing '\n' character from the input stream as you desire.

So change the line: in.skip("\n"); to in.nextLine();

Bimde
  • 632
  • 8
  • 19