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.