Results 1 to 6 of 6

Thread: how to get user input with char value in java

  1. #1
    Join Date
    Dec 2009
    Posts
    139

    how to get user input with char value in java

    I am trying to make a simple java program for calculating two number, Where the user inputs a number then inputs a operator which may be "+,-,*,/" and after that he inputs the second number so that he gets a answer. But i don't know how can i get my user input for the operator as a caharacter or char value so that i can test whether i have to add or subtract both the numbers.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,010

    Re: how to get user input with char value in java

    Hi bearer you can do this but it will require you to know JNI. Because the command prompt is simply designed for communicating with the user. You can use the text component instead command window in order that if you want a complex communication. This type of communication is not possible with the help of command prompt itself.

  3. #3
    Join Date
    May 2008
    Posts
    2,393

    Re: how to get user input with char value in java

    Hey firstly i would like to know whether you wantto create this simple program with just the command prompt or you are using the advanced window toolkit that is the awt component or the swing component. Please specify that in your further replies so i can help you in a better manner.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,949

    Re: how to get user input with char value in java

    You are trying to say that the user inputs a string which looks like this "12+34". You can use the Character.isdigit() method to iterate over the whole string and to test whether the whole string is did it. If the string is not digit then its obvious that it is a symbol or the operator. You cannot directly test whether the operator is "+,-,* or /". Then use the switch case to calculate the output.

  5. #5
    Join Date
    Dec 2009
    Posts
    139

    Re: how to get user input with char value in java

    Although i may be a beginner in the java i have made this program in C++ and i cannot get what you said but in the c language if you want to make this program you just have to assign your operator as a char and then with the help of the if clause or switch statement you can easily tell the compiler to add or subtract both the numbers. Implement something in relation to the code below:
    System.out.println("Input the operator(+ - / *).");
    char oper = sc.next();

  6. #6
    Join Date
    Oct 2005
    Posts
    2,394

    Re: how to get user input with char value in java

    I think that you are almost on the right path and you can just make your code working if you have just use the charAt method of java and it will do the rest for you so just implement your code with the code which i have mentioned below. I think you must get the result with this piece of code.

    char oper = sc.next().charAt(0);

Similar Threads

  1. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 05:36 PM
  2. Java - scanner package for user input
    By Shaan12 in forum Software Development
    Replies: 4
    Last Post: 21-07-2010, 02:49 PM
  3. JTextArea for input text from the user
    By liatB in forum Software Development
    Replies: 1
    Last Post: 17-03-2010, 08:37 PM
  4. How to read user input from console using Scanner class in java?
    By Constantinee in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 05:47 PM
  5. How to use Console class to read user input in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 09:17 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
  •