Results 1 to 14 of 14
Thread: Help with char and string
- 11-01-2011, 09:13 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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
- 11-01-2011, 09:36 PM #2
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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.
- 11-01-2011, 09:42 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
Re: Help with char and string
I think it helps :D
I will try it
thanks
- 11-01-2011, 10:28 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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 :)
- 11-01-2011, 10:35 PM #5
Re: Help with char and string
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?
Once again incorrect code and bad advice.switch ('gradeChar')
case A: 100; break;
- 11-01-2011, 10:45 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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
- 11-01-2011, 10:46 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
Re: Help with char and string
Is it possible just to take first letter from
grade = input.next();
and pass it to switch?
- 11-01-2011, 10:47 PM #8
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.
- 11-01-2011, 10:48 PM #9
- 11-01-2011, 10:55 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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-01-2011, 10:57 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
- 11-01-2011, 11:00 PM #12
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.
- 11-01-2011, 11:04 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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?
- 11-01-2011, 11:06 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
check a string char by char
By Sotsiak in forum New To JavaReplies: 2Last Post: 10-23-2010, 09:24 PM -
Char to Bit String
By Krooger in forum New To JavaReplies: 2Last Post: 01-29-2010, 02:26 AM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks