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!

Not getting the exact value

Status
Not open for further replies.

viclap

Programmer
Oct 12, 2000
39
PH
Hi To all,

I'm trying to extract the character one by one from a user input and check it if a digit or letter. If digit is found, store the value into a variable. But the problem is i am not getting the exact value (0-9)but the decimal value of the digit. Any help would be greatly appreciated.
Here's the code.

import java.io.*;

class ParseString
{
public static void main(String args[])
{
// prompt the user to enter the String to parse
System.out.println("Enter your String --->");

// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//String InputStr = null;
int NumDigit =0;
char ch;

// read the Input String from the command-line; need to use try/catch with the
// readLine() method

try {
String InputStr = br.readLine();
for (int i=0;i<InputStr.length();i++)
{
ch = InputStr.charAt(i);
if (Character.isDigit(ch))
{
NumDigit = ch;
System.out.println("ch --> "+ ch); //produces decimal value instead of 0 to 9.
}
if (Character.isLetter(ch))
{
System.out.println("Ltter found--> "+ ch);

for(int j=0;j < NumDigit;j++)
{
System.out.print(ch);
}
}
}

}
catch (IOException ioe)
{
System.out.println("IO error trying to read your String!");
System.exit(1);
}

//System.out.println("String "+InputStr.length());
}
}
 
Change to :

Code:
if (Character.isDigit(ch)) {    
   NumDigit = Integer.parseInt("" +ch);
   System.out.println("ch -->  "+ ch +", int is " +NumDigit); //produces decimal value instead of 0 to 9.
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj,

Great! Million Thanks.I'm beginning to love Java....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top