Results 1 to 7 of 7
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [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

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    [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?!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The isDigit(...) method is static, and you need to call it on the Character class:
    Java Code:
    if (Character.isDigit('3'))
    {
      //...
    }

  5. #5
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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)?

  7. #7
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Well I changed it because i thought 'ch' is the right one which is NOT!
    ch is the correct one.
    and I can't believe I didn't even try it! I tried 100 other things but this!!
    thanks for the hint Fubarable :)
    Last edited by sfe23; 02-23-2009 at 04:42 AM.

Similar Threads

  1. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  2. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  4. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM
  5. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •