943,708 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 11853
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 20th, 2008
0

String to int

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vladdy19 is offline Offline
20 posts
since Jun 2007
Feb 21st, 2008
0

Re: String to int

and what should the answer for "asu" be?
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 21st, 2008
0

Re: String to int

int num = Integer.parseInt(parseString)
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
piers is offline Offline
68 posts
since Jul 2007
Feb 22nd, 2008
0

Re: String to int

Click to Expand / Collapse  Quote originally posted by piers ...
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
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Feb 27th, 2008
0

Re: String to int

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".
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008
Feb 27th, 2008
0

Re: String to int

Click to Expand / Collapse  Quote originally posted by Parsu7 ...
use the following code to convert any string to integer.
...
That is absolutely not the way to convert a String to an int.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Feb 27th, 2008
0

Re: String to int

Could you suggest me any other way of converting a string to an ineger?
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008
Feb 27th, 2008
0

Re: String to int

If you read the posts above, it has already been answered.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Feb 27th, 2008
0

Re: String to int

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.
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008
Feb 27th, 2008
0

Re: String to int

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: NoClassDefFounderError
Next Thread in Java Forum Timeline: Java Paint Program Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC