Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Getting User Input With Char Value.

user input

  • Please log in to reply
14 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 14 December 2009 - 03:12 PM

Hi I am making a simple calculator were you just type a number then the operator and the second number to get the answer but I need to know how to get my user input for the operator to be char so I can test if I need to minus or plus ect... Please Help.
Thanks.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 14 December 2009 - 04:06 PM

User inputs a string. Something like "34+56".
You will iterate over the whole string and test if the current symbol is digit, by using Character.isdigit() method. if it's not digit, then you'll know it must be the operator. There's no direct conversion from char to operator, so you'll need to test if the operator is '+' '-' '*' '/' etc, and use if or switch-case to perform actual calculation.
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 14 December 2009 - 04:22 PM

I'm a beginner and not sure what that meant. I have played with c++ and in it you assign your operator as a char then you use the if clause to see if its "+"-"*"/" then tell it to "+"-"*"/" from that number.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 14 December 2009 - 05:44 PM

You would so the same thing thing with Java.
  • 0

#5 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 14 December 2009 - 06:51 PM

Heres the code. What do I need to make it take it as char?
System.out.println("Type operator(+ - / *).");
char oper = sc.next();
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 15 December 2009 - 12:51 PM

Why not do this?
char oper = sc.next().charAt(0);
?
  • -1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 15 December 2009 - 01:19 PM

@ orjan Thanks it works great. I didn't use it because I've never seen that code before I just started to learn Java about two days ago.:)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#8 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 15 December 2009 - 02:51 PM

ah. okey. do you understand what it really is doing?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#9 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 15 December 2009 - 02:54 PM

I think it's taking the next user input as a char value right.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#10 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 15 December 2009 - 08:11 PM

well, yes, but if you break it down, part by part?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#11 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 15 December 2009 - 08:13 PM

No I guess not.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#12 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 15 December 2009 - 08:21 PM

char oper = sc.next().charAt(0); is the statement.

char oper is the declaration of the variable oper etting ti to type char.
sc is an object of type Scanner which you have connected to the Standard In.
sc.next() is when you call the method next() on sc it will return an object of type String.
what you add avter the parenthesis is a method to perform on that string object
[returned value].charAT(0)
the method is chatAt, which if you look it up, is the char on position given as parameter, we gove 0, as it's the first position we want.

therefore, all together, the line makes:

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

it could have been much easier to read if you cut it into these pieces:
char oper;
String result;
result = sc.next();
oper = result.charAt(0);
which does the very same things, but more readable and understandable.
but to give a simple line for it, we crush it together into the line
char oper = sc.next().charAt(0);

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Also tagged with one or more of these keywords: user input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download