| | |
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 |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






