Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Re-convert a String to an int....simple computer code I can't seem to understand!?!?
String s contains an abbreviated ordinal number such as "3rd", "99th", "973rd". Write code that sets the int value to the corresponding int value...like 3, 99, etc.
Lets test "2nd"
String s = "2nd";
int value;
Now...what would be the proper code to place here?
I was thinking of simply putting:
value = int s;
but that does not work =/ ...I am a beginner..
2 Answers
- 1 decade ago
You have to read in only the integer values in that string. The string contains letters and simply trying to cast it into an int won't work. (btw, to correctly cast you have to enclose int in paranthesis like so: (int) s )
Anyways, are you working with java? If so, you want to scan that string and pick out the actual integers. To do this, create a new scanner and pass the string into it as a parameter:
Scanner scan = new Scanner(s);
Now, you can use the methods of the scanner class to parse that string. The one you want is hasNextInt and nextInt()
int value;
if(scan.hasNextInt())
value = scan.nextInt();
So what this does is that your only taking in the 3 in 3rd into value, or the 99 from 99th into value and so on.
- 1 decade ago
I have no idea what language you are using so an exact answer is not possible. You need to remove the "letter" part of the string, which will leave you with the "number" part. Then you can simply typecast.
Tell me which language you are using and I will give you pointers.