Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to integer

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I am new to Java and have an assignment where I have to set each character of a 4-character string to an integer (result 4 integers. It seems whatever I try doesn't work or I get (Unicode?) something like this, one position of my String has 0 my result is '48', my string has 1 my result is '49'. All I want is String 1 to become integer 1. I know this must be a snap, but I cannot seem to gleen it out of my text book or JDK documentation.

Thanks
 
Hi,

There are a lot of ways you can change a String to an int value. For exampe:-

String abc = "123";
int temp = Integer.parseInt(abc);

The value of temp would be 123. However, to be sure that the String is always an int value, we will need the try and catch statement. So if your String happens to be "abc" instead of "123", the line "Invalid String." would be printed out to the console.

try
{
String abc = "123";
int temp = Integer.parseInt(abc);
}
catch (NumberFormatException e)
{
System.out.println("Invalid String.");
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top