I'm getting this error
java.lang.NumberFormatException: For input string: "7708166193"
From this line of code
String[] tmp = in.nextLine().replace("-","").split(" ");
String phoneNumber = tmp[2]+tmp[3];
int number = Integer.parseInt(phoneNumber);
And I cannot quite figure out why it's throwing this error.
The largest int (+- 2billion) is smaller than 7708166193. Use:
long number = Long.parseLong(phoneNumber)
Because, Your input value (i.e 7708166193) is greater than Integer.MAX_VALUE (i.e 2147483647). Use long instead of int
long number = Long.parseLong(phoneNumber);
It seems that you tried to use a Integer but you reached the max value that is 2^31, for 7708166193 you should use Long instead.
Related
The following code throws NumberFormatException and I don't understand why,
String sku = "008949679851";
System.out.println(Integer.valueOf(sku));
Interestingly, if I remove the first three digits and the keep the input string as "949679851", then this exception is not thrown. Is there a limit in length when converting a string to an integer value..? How can I make it work with the full string..?
Because the max value of an Integer is Integer.MAX_VALUE = 2147483647 and your number is greater than this 8949679851. Instead use Long.valueOf(sku) or BigInteger for example:
Long l = Long.valueOf(sku);//Max value equal to 9223372036854775807
BigInteger b = new BigInteger(sku);
In Java the maximum value for int and Integers is 2^31-1 (2147483647) so your number exceeds that value.
Java integer size is 32 bits (range -2,147,483,648 to +2,147,483,647). "008949679851" is too long, while "949,679,851" is within the range.
I'm having trouble trying to find the length of a int inputted. I converted the int value into a string and it works perfectly up until the user inputs any int that has more than 10 digits. The program will print out how many digits were inputted but it gives some boundary error once I put anything >10 digits
answer = in.nextInt();
answerString = String.valueOf(answer);
answerLength = answerString.length();
System.out.println(answerLength);
The reason for this is because the max value for an Integer is 2147483647. If you need more digits than that consider using long.
take input as String even if it is integer value, see below code.
String answer = in.nextLine();
System.out.println(answer.length());
Using more than 10 digits goes over Integer.MAX_VALUE. BigInteger would be more suitable in your case, but be aware of its memory restrictions as mentioned here.
Just use Long answer = in.nextLong(); instead
I have declared the variable for the double I'm using:
z= 345.876;
Now I want to display the number of digits that come before the decimal point. I tried to do the following:
String string_form = new Double(z).toString().subString(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
I get an error saying: 'The method subString(int, int) is undefined for the type String'
Then a quick fix shows to change it small case s as: substring. However the error then changes to the string, 'string_form' which says it's not initialized. Any ideas on what to do?
And also how would I modify that to find the number of digits that come after a number? I know in the part
.indexOf('.')
I'd replace the decimal point with a number but how would i change it so that it displays how many digits come AFTER the number, not before? thanks. and yes I have imported the decimalformat text lang.
You're trying to use string_form before you have actually created it.
If you break
String string_form = new Double(z).toString().substring(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
into
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(0,string_temp.indexOf('.'));
double t = Double.valueOf(string_form);
Then it should work.
To get the numbers after the decimal point just take the digits from period until the end of the number.
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(string_temp.indexOf('.'), string_temp.length());
double t = Double.valueOf(string_form);
As others have pointed out though, there are many better ways than converting to string and checking for period and reconverting.
The number of decimal digits before the decimal point is given by
(int)Math.log10(z)+1
The number of decimal digits after it is imprecise and depends on how much precision you use when converting to decimal. Floating-point values don't have decimal places, they have binary places, and the two are incommensurable.
just convert the double to a string. find the index of . with indexOf. get the length of the string. subtract the index of . from the length and you should have the count.
String string_form = Double(z).toString();
int index = string_form.indexOf('.');
double d = Double.parse(string_form.substring(0, index+1));
If the numbers are small, you could try
int i = (int) z;
or
long l = Math.round(z);
You're using string_form in the expression string_form.indexOf('.'), but it's not initialized yet, because it's only set after the call to substring(). You need to store the value of toString() in a temporary variable so you can access it in the call to substring(). Something like this
String stringForm = new Double(z).toString();
stringForm = stringForm.substring(stringForm.indexOf('.'));
There are other and better ways to do this, however. Math.floor(z) or even just a cast (long)z will work.
Could do this, for examble:
Integer.parseInt(String.valueOf(possibleBirths).split(".")[0]);
You can do the next thing:
Double z = 345.876;
int a = t.intValue();
int counter = 0;
while(a != 0) {
counter++;
a = a / 10; }
System.out.println(counter);
I have to convert a String "1392298553937999872" into an int. The string is a timestamp. Normally this should be possible to convert it into a int by using:
Integer i = Integer.valueOf(1392298553937999872);
But I receive the following exception:
java.lang.NumberFormatException: For input string:
"1392298553937999872"
If I use double it works, but the number is wrong. So how can I convert a timestamp into an int?
convert the string to long.
String a="1392298553937999872";
long b= Long.parseLong(a);
The number is greater than the max value of Integer use Long
Long l = new Long("1392298553937999872");
The number you're trying to convert exceeds the Integer.MAX_VALUE value. You'd better use BigInteger.
BigInteger bigInteger = new BigInteger("1392298553937999872");
The value you're trying to convert is greater than the Integer.MAX_VALUE (2,147,483,647), you should use one of the alternative types Long, BigInteger:
Long bigValue = Long.parseLong("1392298553937999872");
// ...
Long bigValue = new Long("1392298553937999872");
// ..
Long bigValue = Long.valueOf("1392298553937999872");
// ...
BigInteger bigValue = new BigInteger("1392298553937999872");
If adding an additional library and the values might vary, you can also use apache commons' NumberUtils. The method NumberUtils.createNumber(String) will adapt based on the provided input:
Number bigValue = NumberUtils.createNumber(inputString);
In my java code, I have a string 9632580147, and when I convert it into a int, using this code:
try{
sNumberInt = Integer.parseInt(sNumber);
} catch(NumberFormatException nfe) {
Log.d("NUMBER", nfe.getMessage());
return;
}
It goes into the catch block saying Invalid int: "9632580147"...
Does anyone know how to fix this?
Thanks
When you type
int sNumber = 9632580147;
into your code, the compiler will tell you:
The literal 9632580147 of type int is out of range
The reason is that your number is too big to fit into an int, use a long instead.
Max value of int is 2147483647 and you are trying to pass 9632580147 which is greater. Try maybe Long.parseLong(sNumber)
It seems you are passing value larger than 32 bits:
9632580147 = 1000111110001001011000001000110011 (34 bits)
Integer max value is 2147483647. If you want to part that number, you need to parse it into a Long.
The maximum value of the integer in Java is 2147483647 while your input 9632580147 is greater. Instead, use a long data type:
long sNumberLong = Long.parseLong(sNumber);