Hello and thanks in advance,
I'm having a problem with the java.util.Scanner Here is my code:
public static void ScanInput(String Choice) {
if(scanner.hasNextLong()){
long input = scanner.nextLong();
long length = String.valueOf(input).length();
if(length <= 10){
if(Choice == Choice1){
Converter.Decimal(input);
} else if(Choice == Choice2) {
Converter.Binary(input);
}
} else {
System.out.println(error);
scanner.close();
ScanInput(DecimalToBinary.choice);
}
} else {
System.out.println(error);
scanner.close();
ScanInput(DecimalToBinary.choice);
}
scanner.close();
}
the use of this does not really matter. The problem is that when i enter an value bigger than 10 digits it crashes. So it seems that i used an int or so??
(Because of the maximum size of an int).
BUT I used long....
My debug instructor points at
if(scanner.hasNextLong()){
And that is also the place where things go to shit... Please help!
If the value is bigger than 9223372036854775807(MAX value for long) then use BigInteger, something like this:
if (scanner.hasNextBigInteger()) {
BigInteger big = scanner.nextBigInteger();
}
Right now the compiler is treating your input as integer as apparently your long input doesn't have suffix L. In java long ends with L, e.g:
long lg = 24863512789L;
Fix this and it'll work fine.