954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to int

Could someone help me out
I need to change a String such as "asu"

I'm thinking of using the ascii but I'm not sure how i could do that

vladdy19
Newbie Poster
20 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

and what should the answer for "asu" be?

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

int num = Integer.parseInt(parseString)

piers
Junior Poster in Training
68 posts since Jul 2007
Reputation Points: 10
Solved Threads: 3
 
int num = Integer.parseInt(parseString)

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

stultuske
Posting Sensei
3,110 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 432
 

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".

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 
use the following code to convert any string to integer. ...


That is absolutelynot the way to convert a String to an int.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Could you suggest me any other way of converting a string to an ineger?

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

If you read the posts above, it has already been answered.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

but i have tested every post on my netbeans ide it does not work.
The code which i have posted has been tested and verified on my ide and it does convert "asu" to 97.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

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.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

even if you write Integer.parsrInt("asu"); you will get Number format exception but if you write Integer.parseInt("197") it will give the number 197.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 
even if you write Integer.parsrInt("asu"); you will get Number format exception but if you write Integer.parseInt("197") it will give the number 197.


Of course, which is already mentioned. You are missing the entire point.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
Of course, which is already mentioned. You are missing the entire point.


I would be really gratefull to you if you could tell me the point which i am missing.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

Integer.parseInt() was already explained. The real point is that there is no generic method to make "asu" an int. It is totally dependent upon how you need to encode the string and that is totally dependent on what you intend to use it for.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
Integer.parseInt() was already explained. The real point is that there is no generic method to make "asu" an int. It is totally dependent upon how you need to encode the string and that is totally dependent on what you intend to use it for.

Thanks, but can we write this to convert a string such as "asu" to integer

int x = br.read();
int y=br.read();
int z=br.read();

System.out.println(x+y+z);

Please do reply me.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

You could, but what would the point be?

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

You can write whatever you want if this is the way you want to convert "asu" to a String. As it was said in the first reply what do you want to convert "asu" to?

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You