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());
}
}
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());
}
}