Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have String which cannot be converted to corresponding Long number using Long.getLong(id). The value of string seems to be valid. Why returned longId is null?
Code:
Long longId = Long.getLong(id);
Use Long.valueOf(..). Long.getLong(...) is for different purpose:
getLong(String nm)
Determines the long value of the system property with the specified name.
Use Long.parseLong(id) to convert from a String to a Long.
This has already been answered on StackOverflow. Please check the following URL:
How to convert String to long in Java?
Credit to Mike Christensen and Josh Pinter for the original answer.
Long.parseLong("0", 10) // returns 0L
Long.parseLong("473", 10) // returns 473L
Long.parseLong("-0", 10) // returns 0L
Long.parseLong("-FF", 16) // returns -255L
Long.parseLong("1100110", 2) // returns 102L
Long.parseLong("99", 8) // throws a NumberFormatException
Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
Long.parseLong("Hazelnut", 36) // returns 1356099454469L
This will do it: Long.parseLong("100")
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a function that removes all spaces in the given string. This is my code:
fun nonSpaceString(s: String): String {
var result = " "
for (index in 0..s.length-1) {
if (s[index] != ' ') {
result += s[index]
}
}
return result
}
fun main(){
nonSpaceString("abc d e")
}
But the result is holding "a" and then holding "ab" but the end result holds nothing. Does anyone know what I'm doing wrong here?
You have initialized the result with a space character. So the result starts with a space that may confuse you. Apart from that, your function is working fine.
Additionally, you can use the replace function to remove all spaces from a string
"abc d e".replace("\\s+".toRegex(), "")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to dynamically set an integer variable using a hexadecimal value, but when I use Integer.parse(hexValue, 16) it gets a different value from setting as int a = 0x04A7D488
For example:
int a = 0x04A7D3B8;
System.out.println("a = " + a); // prints 78107576
int b = Integer.parseInt("04A7D3B8", 16);
System.out.println("b = " + b); // prints 78107784
Why do I get different values? How can I dynamically set variable a with value 0x04A7D3B8?
Note: I've discovered that this error is only happening with Java SDK 1.8.0_171.
Solution
I was trying to convert a negative integer value using Long.parseLong or Integer.parseInt, but the correct solution is using Integer.parseUnsignedInt("FD8914EC");
In my tests, the value FD8914EC was converting to -41347860 (declaring as long a = 0xFD8914EC) or 4253619436 (declaring as long b = Long.parseLong("FD8914EC", 16);), but you have always to use Integer.parseUnsignedInt (the result will be negative if the hexadecimal value starts with F).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a character array as
char[] bitsString = new char[16];
bitsString = {'1','1','1','1','1','1','0','1','1','1','1','0','0','1','1','0'};
Then I converted it to the corresponding integer as follows:
int givenNumber = Integer.parseInt(new String(bitsString), 2);
The above logic works fine when number bitstring array length is less than 10. But when it is increased to 11 or more, it is showing me java.lang.NumberFormatException why ?
After some hit and trials I found that, it was the case since the 16 bit value is crossing the Integer limit.
But when it is changed from int to long
long givenNumber = Long.parseLong(new String(bitsString), 2);
it is working perfectly fine. Since in this case long has 64 bits length.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char".
I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char values in Java. Instead, both operands are promoted to int (via binary numeric promotion), and the result of the subtraction is an int as well. So you'll need to cast the result back to char:
test[1] = (char) (characterArray[1] - ASCII_SUB);