0

The program is supposed to take a sentence inputted by the user such as "I am very hungry" and then ask a number of words to rotate. If the number was 2 the new output would be "very hungry I am". This is my code so far but I seem to have an error. When I run the code, this appears: java.util.InputMismatchException. There is also some other information that appears under it but I am not sure what it means. This is my code so far.

import java.util.*;
public class WordRotation
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a sentence");

     if(input.hasNext());
    {
      String s = input.next();

      String[] words = s.split(" ");

      System.out.println("Enter number of words to be rotated");
      int rotation = input.nextInt();

      for (int i = 0;i < words.length;i++)
      {
        System.out.print(words[(i + words.length - rotation)%words.length] + " ");
      }
    }
  }
}

I tried to change my if statement to a while statement but the code never outputs anything, it just keeps loading.

user3015645
  • 31
  • 1
  • 5

2 Answers2

2

Change String s = input.next(); to String s = input.nextLine();


Yeah, it is actually a solution because after we hit enter, there is a \n still left in stream after we hit Enter and .nextInt() reads that \n and your number. If we use input.nextLine() instead of input.next() we will capture that \n and input.nextInt() will read next integer without \n.

There is more info https://stackoverflow.com/a/7056782/1366360

Community
  • 1
  • 1
mdolbin
  • 936
  • 4
  • 8
  • That won't change anything. – Anirban Nag 'tintinmj' Nov 21 '13 at 21:24
  • 1
    @tintinmj - This actually is the solution because `input.next()` reads the next _token_, not the entire sentence (or line). That leaves the input at the second word (assuming the default delimiter of whitespace), which then causes an `InputMismatchException` in the call to `input.nextInt()`. – Ted Hopp Nov 21 '13 at 21:38
  • @lancemanfv Ok in this case it's true. But OP should know this is a hack and may not be useful in future. – Anirban Nag 'tintinmj' Nov 21 '13 at 21:40
  • @tintinmj - Why is it a hack? The change makes the program read according to what's expected. – Ted Hopp Nov 21 '13 at 21:41
  • @tintinmj no, it's not a hack, check out updated post for explanations. Everything is right - we press `Enter`, Scanner reads `I am hungry` and input stream receives new line symbol `\n`. The next thing we will read, no matter which `.nextXXX()` we invoke, will be that `\n` but we will crash if we will try to return that `\n` as Integer, Double or whatever except string. Invoking `input.next()` once more to skip that `\n` would be a hack:) – mdolbin Nov 21 '13 at 21:48
  • I upvoted it long time ago, i was just suggesting you to make your explanation clear. – Anirban Nag 'tintinmj' Nov 21 '13 at 21:53
  • @tintinmj No offence, I didn't say that that was you but that -1 upset me. – mdolbin Nov 21 '13 at 22:02
  • @user3015645 try to always wrap your `input.nextXXX()` with `if(input.hasNextXXX())` and set some log or `system.out.print` message for `else` so you can easily find the issue. – mdolbin Nov 21 '13 at 22:05
0

You need to read again from input after your

  System.out.println("Enter number of words to be rotated");

add this line

Scanner input2 = new Scanner(System.in);
  int rotation = input2.nextInt();
constantlearner
  • 5,157
  • 7
  • 42
  • 64
  • Why not use `input` as OP is already doing? The problem is that OP isn't reading the entire first line, just the first word of the sentence. – Ted Hopp Nov 21 '13 at 21:40