| | |
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 |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






