| | |
String to int
![]() |
which would be correct, if the String to parse was, for instance,
String toParse = "97";
int num = Integer.parseInt(toParse);
but with an input-String like "asu", like in the example he gives us, this will lead to the necessary exceptions, since "asu" is not recognized as an Integer value
String toParse = "97";
int num = Integer.parseInt(toParse);
but with an input-String like "asu", like in the example he gives us, this will lead to the necessary exceptions, since "asu" is not recognized as an Integer value
•
•
Join Date: Feb 2008
Posts: 28
Reputation:
Solved Threads: 1
use the following code to convert any string to integer.
import java.io.*;
public class Strng_to_intg{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int s=br.read();
System.out.println(s);
}
}
try this code to convert any string to int even the string "asu".
import java.io.*;
public class Strng_to_intg{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int s=br.read();
System.out.println(s);
}
}
try this code to convert any string to int even the string "asu".
97 is the byte value of 'a'. Your read() call just got a single byte and converted it to int.
In any case, it's irrelevant. Without a stated encoding expectation, "asu" cannot be converted to int. Integer.parseInt() will convert a valid integer string to an int value. If you want to convert some other string to an int, there are many different ways to do so, but they are specific encoding methods - not just a general api call or statement.
In any case, it's irrelevant. Without a stated encoding expectation, "asu" cannot be converted to int. Integer.parseInt() will convert a valid integer string to an int value. If you want to convert some other string to an int, there are many different ways to do so, but they are specific encoding methods - not just a general api call or statement.
![]() |
Similar Threads
- Problems from string to int array (C)
- string to int conversion (C++)
- String to Int (C++)
- "String class" (Java)
- Return Length of a string? (C++)
Other Threads in the Java Forum
- Previous Thread: NoClassDefFounderError
- Next Thread: Java Paint Program Question
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying openjavafx pearl problem program programming project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver state string superclass swing system thread threads tree variablebinding windows xor






