-
October 13th, 2010, 09:01 PM
#1
how to use data type char.
Scanner keyboard = new Scanner(System.in);
float numberMinUsed, totalDue, dayMin, nightMin;
char R, P;
System.out.print("Enter the service code (R or P");
R = keyboard.charAt(). toLowerCase();
*dont know how to use char. i need it to read (r or p) depending on what the user inputs and i want to convert them to lower case so it wont be case sensitive
-
October 14th, 2010, 05:14 AM
#2
Re: how to use data type char.
Instead of using variables named 'R' and 'P', use a single variable to get input. Something like character_from_user. After reading it, compare to your desired values:
Code:
if((character_from_user == 'r') || (character_from_user == 'R')){...code for 'R'...}
else if((character_from_user == 'p') || (character_from_user == 'P')){...code for 'P'...}
else{....error...}
Don't know if you need to verify your input, if so, put it in a while loop.
-
October 14th, 2010, 05:51 AM
#3
Re: how to use data type char.
Originally Posted by guyafe
Instead of using variables named 'R' and 'P', use a single variable to get input. Something like character_from_user.
Definitely use sensible names for variables instead a letters but in Java the convention is use camel case rather than underscores so use 'characterFromUser' rather than 'character_from_user'. Having said that personally I would use something more specific like 'serviceCode'.
Originally Posted by willc1010
and i want to convert them to lower case so it wont be case sensitive
The Character class has methods for dealing with char types such as toLower(..).
-
October 14th, 2010, 09:30 AM
#4
Re: how to use data type char.
Originally Posted by keang
Definitely use sensible names for variables instead a letters but in Java the convention is use camel case rather than underscores so use 'characterFromUser' rather than 'character_from_user'. Having said that personally I would use something more specific like 'serviceCode'.
The Character class has methods for dealing with char types such as toLower(..).
Or just "input" and check if the input equals R or P K.I.S.S. principle.
-
October 14th, 2010, 09:39 AM
#5
Re: how to use data type char.
Or just "input" and check if the input equals R or P K.I.S.S. principle.
Today 11:51 AM
If the variable had a reasonable life expectancy or was valid over a number of lines of code I would stick with the descriptive name but if I was reusing the variable for several inputs or it was only valid in a very confined part of the code I would use 'input' or maybe even 'inp'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|