0

I have this bit of code here:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a positive integer: ");
    int i = input.nextInt();
    recursiveMethod(i);
    System.out.println();
    System.out.print("Enter a string: ");
    String s = input.next();
    System.out.print("Enter a positive integer: ");
    int j = input.nextInt();
    recursiveMethod(s, j);
}

public static void recursiveMethod(int i) {
    if (i != 0) {
        System.out.print("*");
        i--;
        recursiveMethod(i);
    }
}

public static void recursiveMethod(String s, int j) {
        if (j != 0) {
            System.out.println(s);
            j--;
            recursiveMethod(s, j);
        }
}

When I run this, I get the following error:

Enter a positive integer: 8
********
Enter a string: Run faster.
Enter a positive integer: 
Exception in thread "main" 
java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.company.Main.main(Main.java:16)

Process finished with exit code 1

I'm not exactly sure what's going wrong, though it seems as though when I enter the string as a phrase with spaces it crashes the program.

Daxian
  • 11
  • Scanner has the space character as the default delimiter so it stops consuming the input after reading the first word and the next time you try to consume an integer the scanner try to parse the second word as an integer which causes the error. – Ali Asgari Nov 28 '18 at 22:04
  • Refer this: https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class – jingle123 Nov 28 '18 at 22:13
  • @makoto Wrong duplicate. – Savior Nov 28 '18 at 22:17
  • Geez. I hastily reopened this one since I couldn't reproduce it with a string with no spaces. My bad. – Makoto Nov 28 '18 at 22:20
  • @Savior: It was the *right* duplicate, too. – Makoto Nov 28 '18 at 22:20
  • Here's the previously linked duplicate: https://stackoverflow.com/questions/7946664/scanner-only-reads-first-word-instead-of-line – Savior Nov 28 '18 at 22:21
  • https://stackoverflow.com/q/13102045/1079354 - for reference, Daxian. You need to clear your scanner. – Makoto Nov 28 '18 at 22:21
  • I guess you need them both. – Savior Nov 28 '18 at 22:22
  • @Savior: No, just the `nextFoo` does just fine. This is actually surprisingly common. – Makoto Nov 28 '18 at 22:22
  • @Makoto The duplicate you suggest doesn't make sense _alone_, since they aren't using `nextLine` after a `nextInt`. That's the problem, the `next` only consumes the first word, and then they try to read the next word as an `int`. – Savior Nov 28 '18 at 22:24
  • So I added an input.nextLine() after String s = input.nextLine() and it works until after I enter the second positive integer, and instead of displaying the String phrase I entered that many times, I only get blank spaces followed by the program exiting. – Daxian Nov 28 '18 at 22:39
  • As @Makoto 's link suggests, you need an extra `nextLine` after the `nextInt` if you then plan to consume text with `nextLine`. – Savior Nov 28 '18 at 22:42

0 Answers0