CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2010
    Posts
    3

    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

  2. #2
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    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.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: how to use data type char.

    Quote 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'.
    Quote 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(..).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: how to use data type char.

    Quote Originally Posted by keang View Post
    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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured