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

Recommended Answers

All 16 Replies

and what should the answer for "asu" be?

int num = Integer.parseInt(parseString)

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

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

use the following code to convert any string to integer.
...

That is absolutely not the way to convert a String to an int.

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

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

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.

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.

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.

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.

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.

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.

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.

You could, but what would the point be?

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.