Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



how to get user input with char value in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-01-2010
Member
 
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.

Reply With Quote
  #2  
Old 07-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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.
Reply With Quote
  #3  
Old 07-01-2010
Member
 
Join Date: May 2008
Posts: 2,383
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 07-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
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.
Reply With Quote
  #5  
Old 07-01-2010
Member
 
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();
Reply With Quote
  #6  
Old 07-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to get user input with char value in java"
Thread Thread Starter Forum Replies Last Post
How to convert string to char array in java? Baazigar Software Development 6 10-01-2011 05:36 PM
Java - scanner package for user input Shaan12 Software Development 4 21-07-2010 02:49 PM
JTextArea for input text from the user liatB Software Development 1 17-03-2010 08:37 PM
How to read user input from console using Scanner class in java? Constantinee Software Development 4 06-02-2010 05:47 PM
How to use Console class to read user input in java? MABON Software Development 4 04-02-2010 09:17 PM


All times are GMT +5.5. The time now is 05:25 AM.