This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes another silly question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another silly question" Watch "another silly question" New topic

another silly question

Joe Panully

Joined: Oct 10, 2004
Posts: 22
If i have a number, make it a string, and then take a sub string of it
int i=423;
String iVal=""+i;
String subval=iVal.substring(1,2);
I now should have a substring with 4
My question is, i am not sure of the syntax used to "parse" something to something else.
If I want to parse this substring "subval" to an integer I know I do <i>something</i> like this:
int subval=Integer.parseInt(subval);
but I am not sure of the exact syntax
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Joe,

Are you trying to break the value 423?

Here is an example:

[ October 10, 2004: Message edited by: Joyce Lee ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272

subval = "2"

String character positions start at 0.

substr( firstPosition, lastPosition+1 )

So substr(1,2) selects the second character

Your syntax is fine, but is subval an int or a String?

Mike Gershman
SCJP 1.4, SCWCD in process
Joe Panully

Joined: Oct 10, 2004
Posts: 22
I agree. Here's the link:
subject: another silly question
It's not a secret anymore!