You cannot cast from String to Integer. However, if you are trying to convert string into integer and if you have to provide an implementation for handling null Strings, take a look at this code snippet:
String str = "...";
// suppose str becomes null after some operation(s).
int number = 0;
try
{
if(str != null)
number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
number = 0;
}
Answer from Juvanis on Stack OverflowYou cannot cast from String to Integer. However, if you are trying to convert string into integer and if you have to provide an implementation for handling null Strings, take a look at this code snippet:
String str = "...";
// suppose str becomes null after some operation(s).
int number = 0;
try
{
if(str != null)
number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
number = 0;
}
If you're using apache commons, there is an helper method that does the trick:
NumberUtils.createInteger(myString)
As said in the documentation:
"convert a String to a Integer, handling hex and octal notations; returns null if the string is null; throws NumberFormatException if the value cannot be converted.
null is not a valid representation of integer number. Integer.parseInt() requires that the string be parsed is a vaild representation of integer number.
Integer.parseInt()
public static int parseInt(String s, int radix)
440 throws NumberFormatException
441 {
442 if (s == null) {
443 throw new NumberFormatException("null");
444 }
Integer.valueOf(str)] // which inviokes Integer.parseInt(Str) to return an Integer instance.
public static Integer valueOf(String s) throws NumberFormatException
569 {
570 return new Integer(parseInt(s, 10));
571 }
The folks at Sun who implemented Integer (a long time ago :) ) probably were not thinking of databases when they wrote that method. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. In general, it's a good idea to raise an exception as soon as there is a problem. (a Fail fast design)
If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong.
Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle.
Basically, what you observed is the way it is for now, and someone will have to pay you to code around it :)
here is a solution :
int tryParseInt(String value) {
try {
return Integer.parseInt(value);
} catch(NumberFormatException nfe) {
// Log exception.
return 0;
}
}
you should catch NumberFormatException instead of exception.
I'd consider using NumberUtils.toInt from Apache Commons Lang which does exactly this:
public static int NumberUtils.toInt(java.lang.String str, int defaultValue)
The implementation uses the already mentioned Integer.parseInt with an additional null check.
See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)
It is reasonable to expect the same exceptions to be thrown for null; however, these api's are very old and may not be able to be changed at this point.
And:
Since the exception behavior is long-standing and specified in the JavaDoc, it is impractical to change either method's behavior at this time. Closing as will not fix.
As taken from: Bug Report: Integer.parseInt() and Double.parseDouble() throw different exceptions on null.
Like others have stated: It's likely made by different authors.
Note: everything in this post is in the source of Java7-b147
Double.parseDouble() goes into a Sun library (in sun.misc.FloatingDecimal) the first important thing that happens is:
in = in.trim(); // don't fool around with white space.
// throws NullPointerException if null
Integer.parseInt() is done manually in the Integer class. The first important thing that happens is:
if (s == null) {
throw new NumberFormatException("null");
}
I would guess there are two different authors.
public void sortHighscore(){
for(int i = 0; i < players.length; i++) {
int min_index = i;
for (int j = i + 1; j < players.length; j++) {
if(players[j] != null && players[i] != null) {
if (Integer.parseInt(players[j].getScore()) < Integer.parseInt(players[i].getScore())) {
min_index = j;
}
}
}
Player temp = players[min_index];
players[min_index] = players[i];
players[i] = temp;
}
}
Please if someone can see what the problem may be