I'm trying to write a program that asks a user for their name and number. Then the program should display the name and a random number from 0 to that number. Here is the code:
import java.util.*;
class name {
public static void main(String args[]){
Random rannumber = new Random();
Scanner input = new Scanner(System.in);
int name, number,randomnumber;
System.out.println("What is your name? ");
name = input.nextInt();
System.out.println("Give me a number: ");
number = input.nextInt();
System.out.println("Hi "+name);
randomnumber = rannumber.nextInt(number);
System.out.println("The random number from 0 and "+number+"is "+randomnumber);
}
}
Program output:
What is your name?
test
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at name.main(name.java:9)