import java.util.*;
public class test
{
public static void main(String[] args)
{
int i = 123;
String s = Integer.toString(i);
int Test = Integer.parseInt(s, s.charAt(0)); // ERROR!
}
}
I want to parse the input string based on char position to get the positional integer.
Error message:
Exception in thread "main" java.lang.NumberFormatException: radix 49 greater than Character.MAX_RADIX
at java.lang.Integer.parseInt(Unknown Source)
at test.main(test.java:11)
That method you are calling parseInt(String, int) expects a radix; something that denotes the "number system" you want to work in, like
parseInt("10", 10)
(10 for decimal)! Instead, use
Integer.parseInt(i)
or
Integer.parseInt(i, 10)
assuming you want to work in the decimal system. And to explain your error message - lets have a look at what your code is actually doing. In essence, it calls:
Integer.parseInt("123", '1')
and that boils down to a call
Integer.parseInt("123", 49) // '1' --> int --> 49!
And there we go - as it nicely lines up with your error message; as 49 isn't a valid radix for parsing numbers.
But the real answer here: don't just blindly use some library method. Study its documentation, so you understand what it is doing; and what the parameters you are passing to it actually mean.
Thus, turn here and read what parseInt(String, int) is about!
Integer.parseInt(parameter) expects the parameter to be a String.
You could try Integer.parseInt(s.charAt(0) + ""). The +"" is to append the character to an empty String thereby casting the char to String and this is exactly what the method expects.
Another method to parse Characters to Integers (and in my opinion much better!) is to use Character.getNumericValue(s.charAt(0));
Check this post for further details on converting char to int
Need to convert String.valueOf(s.charAt(0)) to String.valueOf(s.charAt(0)) i.e. Char to String.
import java.util.*;
public class test
{
public static void main(String[] args)
{
int i = 123;
String s = Integer.toString(i);
int Test = Integer.parseInt(String.valueOf(s.charAt(0)));
}
}
Let use what we have here.
To parse one digit from a String into an integer. Use getNumericValue(char)
In your case, to get the first character into a number :
int n = Character.getNumericValue(s.charAt(0));
Be aware that you should take the absolute value if you integer can be negative.
Related
public int reverse(int x) {
String xString=String.valueOf(Math.abs(x));
StringBuffer reverseX=new StringBuffer (xString);
if (x>=Integer.MIN_VALUE & x<=Integer.MAX_VALUE) {
reverseX=reverseX.reverse();
if (x<0)
reverseX=reverseX.insert(0,"-");
return Integer.parseInt(reverseX.toString());
}
else
return 0;
}
Runtime Error Message:
Line 12: java.lang.NumberFormatException: For input string:
"9646324351"
Last executed input:
1534236469
What's wrong? help plz Orz!!!
if you try to call your method with the value:
reverse(9646324351);
You get an Compiler error, which leads you to the Problem:
The literal 9646324351 of type int is out of range
So i do not understand why you can get an error in your method.
Use a long/Long or a BigInterger in your program
Here you can rad about the data types and which range they cover
That number is too large to be parsed as an Interger, it exceeds Integer.MAX_VALUE.
Rather use Long.parseLong
Use biginteger class instead of Integer
Read the docs https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
public long reverse(int x) {
String xString=String.valueOf(Math.abs(x));
StringBuffer reverseX=new StringBuffer (xString);
if (x>=Integer.MIN_VALUE & x<=Integer.MAX_VALUE) {
reverseX=reverseX.reverse();
if (x<0)
reverseX=reverseX.insert(0,"-");
return Long.parseLong(reverseX.toString());
}
else
return 0L;
}
You can try above code.
As this 9646324351 value is out of range of int type.You need to provide larger datatype for this String to numeric conversion.
As we know
double's range > long's range >int's range
Also you can try BigInteger
Hope this will help you.
I am trying to write code for a problem that involves a function, that takes input numbers as Strings, and returns the remainder of that input when divided by 7 as an int.
Although the code works for smaller numbers, it prompts a runtime error when handling large numbers as input, shown as follows ..
Runtime error time: 0.04 memory: 711168 signal:-1
Exception in thread "main" java.lang.NumberFormatException: For input string: "5449495"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.math.BigInteger.<init>(BigInteger.java:470)
at java.math.BigInteger.<init>(BigInteger.java:597)
at Ideone.remainderWith7(Main.java:13)
at Ideone.main(Main.java:22)
My code is as follows ..
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
int remainderWith7(String num) {
// Your code here
java.math.BigInteger bg = new java.math.BigInteger(num);
//System.out.println(num);
Integer n = bg.intValue();
return (int) n % 7;
}
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
Ideone id = new Ideone();
System.out.println(id.remainderWith7("56495654565052555054535456545355495650575755555757575350505449495254525056535655494951545354515152515050575749545453535549545551575652535149494949515551545554565555575452555157505555574950505649525149505150575254515549565156515750555450545355495355515251495352565452555453515451505251575251494956515352555154505155535151565754505450535753555654575549575349565351575054"));
}
}
How'd you paste that string in? It looks like it contains a lot of zero-width spaces and zero-width non-joiners.
I say "looks like"; in actuality, you'd only ever be able to see those if you printed out the contents of the string's array, either with Arrays.toString and encapsulating that long string in a variable, or if you inspected it with a debugger.
Ultimately, that's what's leading you astray; Java is attempting to convert those Unicode characters to numbers, and since they're not numbers, they convert to -1. This is why your code breaks, and this is also why it's not immediately apparent as to why it breaks. There are more characters in that string than you're immediately led to believe.
The fix is to remove these characters from the string.
String num = ""; // enter your long number here; not repeating it for brevity's sake
num = num.replace("\u200C", "").replace("\u200B", "");
Now you can get back to other issues with the code, such as not using BigInteger.mod when you want to do a modulo (because trust me, using % ain't gonna give you the right answer with an integer as large as that).
I am trying to convert to int like this, but I am getting an exception.
String strHexNumber = "0x1";
int decimalNumber = Integer.parseInt(strHexNumber, 16);
Exception in thread "main" java.lang.NumberFormatException: For input string: "0x1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)
It would be a great help if someone can fix it.
Thanks.
That's because the 0x prefix is not allowed. It's only a Java language thing.
String strHexNumber = "F777";
int decimalNumber = Integer.parseInt(strHexNumber, 16);
System.out.println(decimalNumber);
If you want to parse strings with leading 0x then use the .decode methods available on Integer, Long etc.
int value = Integer.decode("0x12AF");
System.out.println(value);
Sure - you need to get rid of the "0x" part if you want to use parseInt:
int parsed = Integer.parseInt("100", 16);
System.out.println(parsed); // 256
If you know your value will start with "0x" you can just use:
String prefixStripped = hexNumber.substring(2);
Otherwise, just test for it:
number = number.startsWith("0x") ? number.substring(2) : number;
Note that you should think about how negative numbers will be represented too.
EDIT: Adam's solution using decode will certainly work, but if you already know the radix then IMO it's clearer to state it explicitly than to have it inferred - particularly if it would surprise people for "035" to be treated as octal, for example. Each method is appropriate at different times, of course, so it's worth knowing about both. Pick whichever one handles your particular situation most cleanly and clearly.
Integer.parseInt can only parse strings that are formatted to look just like an int. So you can parse "0" or "12343" or "-56" but not "0x1".
You need to strip off the 0x from the front of the string before you ask the Integer class to parse it. The parseInt method expects the string passed in to be only numbers/letters of the specified radix.
try using this code here:-
import java.io.*;
import java.lang.*;
public class HexaToInteger{
public static void main(String[] args) throws IOException{
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the hexadecimal value:!");
String s = read.readLine();
int i = Integer.valueOf(s, 16).intValue();
System.out.println("Integer:=" + i);
}
}
Yeah, Integer is still expecting some kind of String of numbers. that x is really going to mess things up.
Depending on the size of the hex, you may need to use a BigInteger (you can probably skip the "L" check and trim in yours ;-) ):
// Convert HEX to decimal
if (category.startsWith("0X") && category.endsWith("L")) {
category = new BigInteger(category.substring(2, category.length() - 1), 16).toString();
} else if (category.startsWith("0X")) {
category = new BigInteger(category.substring(2, category.length()), 16).toString();
}
I get this but yet I don't get it:
package com.example.bugs;
public class ParseLongTest {
public static void main(String[] args) {
long l = -1;
String s = Long.toHexString(l);
System.out.println(s);
long l2 = Long.parseLong(s, 16);
}
}
This fails with the following:
ffffffffffffffff
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffffffffffffffff"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.parseLong(Long.java:468)
at com.example.bugs.ParseLongTest.main(ParseLongTest.java:8)
presumably because if you literally interpreted 0xffffffffffffffffL, it would not fit in the Long number space, which is signed.
But why does Long.toHexString() produce a string that cannot be parsed by Long.parseLong(), and how do I work around this? (I need a way of getting long values to their hex string representation, and back again)
Long.parseLong(String s, int radix) doesn't understand two's complement. For negative number it expects minus sign. As bestsss already mentioned you should use Long.toString(long l, int radix) to make hex string compatible with this parsing method.
This is a known bug, it is fixed in 1.8, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4215269
You can use guava's UnsignedLongs.parseUnsignedLong(String s, int radix) if Java 1.8 is not an option.
While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt()
We have several constant values defined as hex values, for example:
CHARACTER_RED = 0xFFAAA005;
During the game the value is serialized and is received through a network connection, coming in as a string representation of the hex value. In order to parse it back to an int we unsuccesfully tried the following:
// Response contains the value "ffAAA005" for "characterId"
string hexValue = response.get("characterId");
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
Then I ran some tests and tried this:
string hexValue = Integer.toHexString(0xFFAAA005);
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
This is the exception from the actual code:
java.lang.NumberFormatException: ffaaa005
at java.lang.Integer.parseInt(Integer.java:462)
at net.triadgames.acertijo.GameMIDlet.startMIDlet(GameMIDlet.java:109)
This I must admit, baffled me. Looking at the parseInt code the NumberFormatException seems to be thrown when the number being parsed "crosses" the "negative/positive boundary" (perhaps someone can edit in the right jargon for this?).
Is this the expected behavior for the Integer.parseInt function? In the end I had to write my own hex string parsing function, and I was quite displeased with the provided implementation.
In other words, was my expectation of having Integer.parseInt() work on the hex string representation of an integer misguided?
EDIT: In my initial posting I wrote 0xFFFAAA005 instead of 0xFFAAA005. I've since corrected that mistake.
The String you are parsing is too large to fit in an int. In Java, an int is a signed, 32-bit data type. Your string requires at least 36 bits.
Your (positive) value is still too large to fit in a signed 32-bit int.
Do realize that your input (4289372165) overflows the maximum size of an int (2147483647)?
Try parsing the value as a long and trim the leading "0x" off the string before you parse it:
public class Program {
public static void main(String[] args) {
String input = "0xFFFAAA005";
long value = Long.parseLong(input.substring(2), 16);
System.out.print(value);
}
}
I'm not a java dev, but I'd guess parseInt only works with integers. 0xFFFAAA005 has 9 hex digits, so it's a long, not an int. My guess is it's complaining because you asked it to parse a number that's bigger than it's result data type.
Your number seems to be too large to fit in an int, try using Long.parseLong() instead.
Also, the string doesn't seem to get parsed if you have 0x in your string, so try to cut that off.