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
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.
Re: Help with char and string
I think it helps :D
I will try it
thanks
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 :)
Re: Help with char and string
Quote:
Originally Posted by
justinm231
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?
Quote:
switch ('gradeChar')
case A: 100; break;
Once again incorrect code and bad advice.
Re: Help with char and string
The what would be the best way to do it?
This is my problem :
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
Re: Help with char and string
Is it possible just to take first letter from
grade = input.next();
and pass it to switch?
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.
Re: Help with char and string
Quote:
Originally Posted by
mehnihma
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.
Re: Help with char and string
is this correct way do do it?
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
Re: Help with char and string
Quote:
Originally Posted by
Junky
Yes, line 3 of the code above does that.
when I do that I get error in switch that it should be char?
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.
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?
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