-1
import java.util.Scanner;  

public class Exercise5 {  

 public static void main(String[] args) {  
  Scanner in = new Scanner(System.in);  

  System.out.print("Input first number: ");  
  int num1 = in.nextInt(5);  

  System.out.print("Input second number: ");  
  int num2 = in.nextInt(25);  

  System.out.println(num1 + " x " + num2 + " = " + num1 * num2);  
 }  

}  

**Here is my java code, the expect output should be like this:

Input first number: 25
Input second number: 5
25 x 5 = 125

After I plug in my code and run it, the output is too different with the answer

Here is the output:

Input first number: Exception in thread "main"   java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at Exercise5.main(Exercise5.java:27)

How can I fix my code?

Allen Li
  • 405
  • 1
  • 3
  • 10
  • 2
    What input are you giving it? Why are you expecting a number in Base-5 and then one in Base-25? – David Mar 29 '17 at 17:37
  • 2
    Please refer: http://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner – kani Mar 29 '17 at 17:38
  • 1
    The documentation for the `nextInt` method tells you everything you need to know to answer this. I haven't used this method before but from reading the documentation I know what is wrong with your code. See here: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – petehallw Mar 29 '17 at 17:40

6 Answers6

2

"5" in in.nextInt(5); means radix. Do you need it? Does the program work if you use nextInt() method with no parameters?

Also, for some reason both of versions work for me with no error:

// output of nextInt(radix) version
java Excercise5
Input first number: 32
Input second number: 23
17 x 53 = 901

// output of nextInt() version
java Excercise5
Input first number: 5
Input second number: 25
5 x 25 = 125
Mikhail Antonov
  • 1,187
  • 3
  • 18
  • 23
2

There are 2 nextInt() methods:

nextInt() alone allows you to read an int in base 10 number

nextInt(radix) reads the next number in base N where N is 5, or 25 in your case

So, call the first one and you're done

Frakcool
  • 10,088
  • 9
  • 41
  • 71
1

You shouldn't put 5 and 25 inside brackets of in.nextInt, you will write value inside console line and scanner will read it.
So instead

 int num1 = in.nextInt(5); 

it should look like

 int num1 = in.nextInt(); 
FilipRistic
  • 2,261
  • 4
  • 20
  • 26
1

No need to specify the radix to nextInt.

Just use the no args version of nextInt.

Replace

int num1 = in.nextInt(5);

with

int num1 = in.nextInt();

And

int num2 = in.nextInt(25);

with

int num2 = in.nextInt();

VHS
  • 8,698
  • 3
  • 14
  • 38
1

FIXED CODE

import java.util.Scanner;  

public class Exercise5 {  

public static void main(String[] args) {  
Scanner in = new Scanner(System.in);  

 System.out.print("Input first number: ");  
 int num1 = in.nextInt();  

 System.out.print("Input second number: ");  
 int num2 = in.nextInt();  

 total = num1*num2

System.out.println(num1 + " x " + num2 + " = " + total);  
}  

}  

Your nextInt() will catch the user input so there is no need to put in values. You will manually put them in when prompted on the output window in your IDE.

I would also put the num1 and num2 into separate variables (Ex. int total = num1*num2) then print out the total in the println :)

0

EDIT: Didn't even notice you were supplying an argument to nextInt(). As the other mentioned, this is not what you want to do.

Also, the new line character is still in the buffer after you call nextInt(). You'll need to fix this as well before your program will operate as you intend.

Understanding Scanner's nextLine(), next(), and nextInt() methods

Community
  • 1
  • 1
Coop
  • 309
  • 1
  • 8