Results 1 to 7 of 7
- 02-23-2009, 12:56 AM #1
[SOLVED] Reading an input string?!
I simply want to read an input string and output different messages depending on what I read.
the main problem is since my input is of type String then I cannot treat it as integers. How can I know if the user has input a number between 1 and 9?
Also if I encounter *, I want to output that is an operator.
My code outputs " it's an operand" no matter what I give it as an input!?
can someone help me please?
import java.io.*;
import java.lang.*;
class Test{
public static void main(String[] args) throws IOException
{
String input;
System.out.print("Enter input: ");
input = getString(); // read a string from kbd
for(int j=0; j<input.length(); j++) // for each char
{
char ch = input.charAt(j); // get it
// just so you know i have tried this as well
// int a = new Integer('ch').intValue();
// if (a >= 1 || a <= 9){
if (ch >= 1 || ch<= 9){
System.out.println("it's an opearnd");
}
else if (ch=='+' || ch == '-' || ch=='*' || ch=='/'){
System.out.println("it's a valid operator");
}
else
System.out.println("wrong input!");
}
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
} // end class
- 02-23-2009, 01:41 AM #2
Wouldn't it be easier to use Character's method isDigit() or isLetter() to determine if it's a number or letter?
Character (Java Platform SE 6))
Character (Java Platform SE 6))
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-23-2009, 02:21 AM #3
[QUOTE=CJSLMAN;58248]Wouldn't it be easier to use Character's method isDigit() or isLetter() to determine if it's a number or letter?
Thanks, I didn't know that existed! I love Java :)
I get this error message though: cannot find symbol method isDigit(char)
even though I imported java.lang.Character;
import java.io.*;
//import java.lang.*;
import java.lang.Character; // in order to be able to use isDigit(char ch) method
class Test{
public static void main(String[] args) throws IOException
{
String input;
System.out.print("Enter input: ");
input = getString(); // read a string from kbd
for(int j=0; j<input.length(); j++) // for each char
{
char ch = input.charAt(j); // get it
// just so you know i have tried this as well
// int a = new Integer('ch').intValue();
// if (a >= 1 || a <= 9){
if (isDigit(ch)){
System.out.println("it's an opearnd");
}
else if (ch=='+' || ch == '-' || ch=='*' || ch=='/'){
System.out.println("it's a valid operator");
}
else
System.out.println("wrong input!");
}
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
} // end class
Does it have anything to do with the method being static?!
-
The isDigit(...) method is static, and you need to call it on the Character class:
Java Code:if (Character.isDigit('3')) { //... }
- 02-23-2009, 04:25 AM #5
Fubarable,
I have tried that as well. in the above code if you replace
if (isDigit(ch)){
System.out.println("it's an opearnd");
}
with the following code:
if (Character.isDigit('ch')){
System.out.println("it's an opearnd");
}
I get 4 errors for that line that I added isDigit method!!
unclosed character literal
')' expected
unclosed character literal
';' expected
could you tell me why plz?
-
First you must tell us why you have (ch) in one example and ('ch') in another? Which is right (think on this before answering)?could you tell me why plz?
- 02-23-2009, 04:38 AM #7
Similar Threads
-
Reading data from csv file based on specific input
By jaiminparikh in forum Advanced JavaReplies: 14Last Post: 02-13-2009, 09:07 PM -
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
Making arrays by reading user input
By apfroggy0408 in forum New To JavaReplies: 23Last Post: 04-30-2008, 01:23 AM -
Prompting user input of a string.
By apfroggy0408 in forum New To JavaReplies: 3Last Post: 03-09-2008, 06:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks