Hey everyone,

I am trying to convert the members of a string into their integer values, but not ASCII values. For instance, if I have the string "12", I want the following portion of the code to spit out 3, but instead it spits out (49+50=99) for the ASCII values. Any help would be awesome! Thanks!

if(Character.isDigit(s.charAt(j)))
		{
			x=(int)s.charAt(j);
			j++;
			y=(int)s.charAt(j);
			z= x + y;
			System.out.print(z);
		}
if(Character.isDigit(s.charAt(j)))
		{

//x=(int)s.charAt(j);
// Try this and repeat to the other number y

char ch = s.charAt(j);
x = Integer.parseInt( String.valueOf(ch) );

                      
j++;
y=(int)s.charAt(j);
z= x + y;
System.out.print(z);
		}

After you take the char, if you cast to primitive int it will give you the ASCII. But you want the number.
So use: Integer.parseInt( ); But that method takes a String as argument. So you take the char, turn it into a String: String.valueOf(ch) And use the parseInt method:

Integer.parseInt( String.valueOf(ch) );

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.