0

To briefly explain, I'm supposed to get the user to enter the name of the recipient of a message, and then get them to enter the message body, which can have multiple lines. If they enter a blank line, then the message body ends and they can select another case in the switch.

case 'S':
case 's':
    if(user.equals(" ")) {
        System.out.println("No user logged in.");
    } else {
        System.out.println("Recipient: ");
        recip = menuScan.next();

        m = new Message(user,recip);

        System.out.println("Enter message. Blank line to quit.");
        mbody = menuScan.nextLine();

        while(!mbody.equals("")) {
            mbody = menuScan.next();
            m.append(mbody);
        }

        ms.deliver(m);

        System.out.println("Messgae sent.");

    }
    break;

But as it is now, the while loop is skipped completely. I've tried changing recip to menuScan.nextLine(), and mbody to menuScan.next() and .nextLine(), but the only other thing that happens is the message body goes on forever.

I've also tried using two different Scanner objects for recip and mbody, but no luck there, either.

aribeiro
  • 3,854
  • 4
  • 27
  • 41
  • Use conditions instead – Gaktan Mar 20 '16 at 19:31
  • 1
    Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) –  Mar 20 '16 at 19:49

2 Answers2

2

You should replace your next by nextLine. next, as the documentation states, returns the next token if it matches the pattern constructed from the specified string, which is not what you want.

Try the following:

System.out.println("Recipient: ");
recip = menuScan.nextLine();

m = new Message(user,recip);

System.out.println("Enter message. Blank line to quit.");
mbody = menuScan.nextLine();

while(!mbody.equals("")) {
    m.append(mbody);
    mbody = menuScan.nextLine();
}

ms.deliver(m);

System.out.println("Messgae sent.");
aribeiro
  • 3,854
  • 4
  • 27
  • 41
  • 1
    This answer is not correct. If you don't do the append before the nextLine inside the loop, you lose the first entered line of text. You have to interchange those lines – RubioRic Mar 20 '16 at 19:56
  • I have detected that in my answer posted before. – RubioRic Mar 20 '16 at 19:58
  • @RubioRic, you're totally right! Updated my answer... On the test I did, I've noticed that also but posting it, made a typo. Thanks for pointing that out! ;) – aribeiro Mar 20 '16 at 19:58
  • Ok but It will not be fair if your answer is accepted instead of mine. :/ – RubioRic Mar 20 '16 at 20:02
0

I've tried this piece of your code

      System.out.println("Enter message. Blank line to quit.");
      mbody = menuScan.nextLine();

      while (!mbody.equals("")) {
           mbody = menuScan.next();
           m.append(mbody);
      }

changing it a bit

      System.out.println("Enter message. Blank line to quit.");
      mbody = menuScan.nextLine();

      while (!mbody.equals("")) {
           m.append(mbody);
           mbody = menuScan.nextLine();          
      }

This way the loop is executed and exited as expected. I have used java.util.Scanner

RubioRic
  • 2,332
  • 4
  • 24
  • 32
  • 2
    You missed the `.next()` on `recip = menuScan.next();`. That's why the `while` loop is never entered. – Erick G. Hagstrom Mar 20 '16 at 20:05
  • Thanks! This works on the back end, but the "Recipient: " and "Enter message: " prompts display at the same time. Is there a way to separate them? – codeslinger19 Mar 20 '16 at 21:51
  • @codeslinger19 Have you tried aribeiro' solution? I can not reproduce the new situation that you describes. I get "Recipient: " first. When I type a line and press enter then a I get "Enter message:" correctly. – RubioRic Mar 20 '16 at 22:07
  • Yes, I've tried both. Would seeing the entire switch help? – codeslinger19 Mar 20 '16 at 22:39
  • @codeslinger19 I don't think so. As I've stated I have used java.util.Scanner. Instantiating it with System.in. Maybe you have used another class or have set specific line separators. – RubioRic Mar 20 '16 at 22:45
  • I'm testing it in windows btw. Line separators may differ in UNIX. – RubioRic Mar 20 '16 at 22:47
  • @codeslinger19 Maybe you have replaced too much .next with .nextLine in the code that we don't see. If you want to read only a character for the different options in your switch, .next is appropiate. – RubioRic Mar 21 '16 at 04:56