Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Using Long.parseLong(String)

import java.lang.*;
public class why{
public static void main(String[] args){
int why = Integer.parseInt("why");
}
}
this simple code can't work. I got the NumberFormatException.ForInputString exception. Anyone can help?
[262 byte] By [wychkraft] at [2007-11-11 6:49:41]
# 1 Re: Using Long.parseLong(String)
import java.lang.*;
public class why{
public static void main(String[] args){
int why = Integer.parseInt("why");
}
}

this simple code can't work. I got the NumberFormatException.ForInputString exception. Anyone can help?

Because the String "why" is not a number. Of course the NumberFormatException is thrown then. Use the parseInt method like this:
public class ThatsWhy {
public static void main(String[] args){
String s = "10";
int i = Integer.parseInt(s);
System.out.println(i*i);
}
}
prometheuzz at 2007-11-11 22:40:09 >
# 2 Re: Using Long.parseLong(String)
Some simple questions:
What is the integer value of "why"?
What is an integer?
Norm at 2007-11-11 22:41:09 >
# 3 Re: Using Long.parseLong(String)
If there is any sense in a numeric equivalent for a word you can sum up the
characters alphabetic ordinal or their ascii number and take that sum to a numerologists,
or you could make an algorithm that woud yield a unique
(long ?) value for any word it receives. The latter would be rather difficult if
it was to acccept all possible (nonsense) "words", but its feasible for the
real set of words that exists.

An integer in computing is an internal numeric representation that only yields whole
numbers, - by decimal truncation if required. Its size is 2 or 4 bytes depending
on the implementation.
sjalle at 2007-11-11 22:42:13 >
# 4 Re: Using Long.parseLong(String)
In keeping with sjalle's suggestion - - look at the String class's hashCode() method, which creates a polynomial evaluation of the unicode value of the characters (and white space) of the String -- allowing for unique values for sun, snu, uns, usn, nsu, nus.
nspils at 2007-11-11 22:43:18 >
# 5 Re: Using Long.parseLong(String)
Yes indeed, then there is the interpretation of that hash of signs, if any ...:cool:
sjalle at 2007-11-11 22:44:11 >
# 6 Re: Using Long.parseLong(String)
I wonder where we can go to learn that interpretation?
nspils at 2007-11-11 22:45:21 >