Results 1 to 14 of 14
  1. #1
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Help with char and string

    I am very new to Java and have problems understating char function


    I have lab in which asks to do:

    Method: calcPoints
    Parameters/Return: One parameter with letter grade of type char. Returns the points for the grade
    Purpose: Converts the grade to its equivalent point value.
    You must use a switch statement in the method calcPoints to convert the grade to its equivalent number of points. If the letter grade is not a valid grade, return a value of -1.

    And I do not get do I need to use string or char, or to you string and later chat to take a first letter?


    I am confused so sorry if I wrote it confusing.

    Thanks

  2. #2
    justinm231 is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help with char and string

    Well assuming the letter grade letter is being passed as a String to your class, then you will need to cast it to a char:

    String grade = (char) gradeChar;

    then you will put the gradeChar as your parameter in the switch statement as:

    switch ('gradeChar')
    case A: 100; break;
    ...
    etc.

    Does this help? I should let you know, I am a beginner as well.

  3. #3
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    I think it helps :D

    I will try it

    thanks

  4. #4
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    The problems is that I need to tell the program if someone enters “Abacus”, the letter grade would be the character ‘A’ in case switch and I don't know how to do it :)

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Help with char and string

    Quote Originally Posted by justinm231 View Post
    String grade = (char) gradeChar;
    Perhaps you should check things before offering incorrect advice. Not only can you NOT cast a String to a char you then assign it back to a String variable. What is the point of that. Plus the instructions say that the value will be passed as a char so why are you bothering with a String anyway?
    switch ('gradeChar')
    case A: 100; break;
    Once again incorrect code and bad advice.

  6. #6
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    The what would be the best way to do it?

    This is my problem :



    XML Code:
              grade = input.next(); // read grade from user
    			  
    			  char cgrade = grade.charAt(0);
    			// case switch
    			  
    			  switch (cgrade) { 
    			  
    			  
    			  
    			                                
                    case "A":              
                    case "a":
                   System.out.println("Points: 4");
                        break;
                    case "b":
                    case "B":
                        System.out.println("Points: 3");
                        break;
                    case "c":
                    case "C":
                        System.out.println("Points: 2");
                        break;
                    case "d":
                    case "D":
                       System.out.println("Points: 1");
                        break;
                   case "f":
                    case "F":
                        System.out.println("Points: 0");
    				       break;
    					 default:
                    System.out.println("Points: -1");
                    break;
                        
                }// end switch

  7. #7
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    Is it possible just to take first letter from

    grade = input.next();

    and pass it to switch?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Help with char and string

    You cannot switch on Strings (at least not in versions 1.6 and lower). You have to switch on chars. By that I mean your case statements.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Help with char and string

    Quote Originally Posted by mehnihma View Post
    Is it possible just to take first letter from

    grade = input.next();

    and pass it to switch?
    Yes, line 3 of the code above does that.

  10. #10
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    is this correct way do do it?


    Java Code:
    grade = input.next(); // read grade from user
    			  
    			  char c = grade.charAt(0);  // from string to Character 
    			  
    			  
    			  
    			  String grade1 = Character.toString(c); // converting to string

  11. #11
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    Quote Originally Posted by Junky View Post
    Yes, line 3 of the code above does that.
    when I do that I get error in switch that it should be char?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Help with char and string

    :headdesk:

    The code you posted in reply 6 is almost correct. The point I was trying to make is that in your case statements you have the letters as Strings (which you cannot do in switches) when they should be chars.

    String grade1 = Character.toString(c); // converting to string
    Delete this line as it is totally unnecessary.

  13. #13
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    This way it's working but whiteout it it is not working?

    how can I write them as chars?

  14. #14
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with char and string

    aha is it 'A' not the "A" for char?

    I think this is new for java 1.7 and that is why it's working

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  2. Char to Bit String
    By Krooger in forum New To Java
    Replies: 2
    Last Post: 01-29-2010, 02:26 AM
  3. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  4. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 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
  •