-1

Hello I am fairly new to java and I have a problem with entering long numbers

So my code is

import java.util.*;
public class Test {
public static void main(String []args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter int: ");
    int a = scan.nextInt();
   }
}

and my output is

Enter int: 
12312312312
Exception in thread "main" java.util.InputMismatchException: For input string: "12312312312"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Test.main(Test.java:6)

can you help me with this problem

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 2
    Hint: What's the largest possible value you can store in an int? – Voo Jun 10 '20 at 07:18
  • Looks like your taking an input longer than the primitive ```int``` can hold and ```nextInt()``` can only take an integer. Change the type to a ```long``` and it should work fine. – kshitij86 Jun 10 '20 at 07:18
  • Nice hint Voo! This is a fairly simple problem, you should consider the maximum value for an `int` and then choose another type for your variable. Check this out: [max int](http://ice-web.cc.gatech.edu/ce21/1/static/JavaReview-RU/VariableBasics/minAndMax.html) – Marian Simonca Jun 10 '20 at 07:20
  • Also, this SO answer contains more details: [max value of integer](https://stackoverflow.com/a/15005226/5048820) – Marian Simonca Jun 10 '20 at 07:21
  • Does this answer your question? [Scanner for long integer, Exception in thread "main" java.util.InputMismatchException](https://stackoverflow.com/questions/19698025/scanner-for-long-integer-exception-in-thread-main-java-util-inputmismatchexce) – azro Jun 10 '20 at 07:27

5 Answers5

2

You need to use long data type instead of int.

long a = scan.nextLong();

For more info, refer https://www.javatpoint.com/java-data-types

ProGamer
  • 420
  • 5
  • 16
0

The reason you getting this exception it because the number you enterred is begger than the size int can represent : max value of integer

the reason long can work for you is becauese it have bigger size in memoery to represent higer number.

try to read more about variable types and max values

Michael Gabbay
  • 413
  • 1
  • 4
  • 20
0

The maximum value for an int is 2147483647 and you have 12312312312. Try a long instead.

Martin Z
  • 238
  • 2
  • 5
0

You can read it as a string and convert it to a big integer since it has "no limit".

BigInteger a = new BigInteger(scan.nextString());
Kev
  • 21
  • 4
  • Np :). If this answer is of any use can you upvote and accept so that I can actually use SO? – Kev Jun 10 '20 at 07:57
0

The value 12312312312 is too large to fit into an int.

Many people suggest to switch to long in order to fix your problem. That indeed fixes this specific problem, but if your values are much larger, than even long may be insufficient.

If you want to be able to input very large values, then you may want to switch to BigInteger. It can hold an arbitrary large number, and your only upper limit is your computer's memory.

The Scanner class even has methods for it:

int a = scan.nextBigInteger();
MC Emperor
  • 22,334
  • 15
  • 80
  • 130