Java charAT Method

I working on this problem in Java.  Got it to work one way and now I need to know how to do a charAt method.

Which I no nothing about.

When the  User assigns a letterGrade string return a value but add 0.3 for + and -.03

Any guidance or help would greatly be appreciated.

package singlegradeprinter;
import java.util.Scanner;
public class SingleGradePrinter 
    public static void main(String[] args) 
     Scanner in = new Scanner(;
     System.out.println("Enter a letter grade:");
     String input = in.nextLine();
     Grade g = new Grade(input);
     double grade = g.getNumericGrade();
     System.out.println("Numeric value: " + grade);      
package singlegradeprinter;
public class Grade 
    public Grade(String initGrade)
        letterGrade = initGrade;
    public double getNumericGrade()
          //double grade = 0;  
        //    if (letterGrade.equals("A+")){
          //          return (4.0);
          //  }else if (letterGrade.equals("A")){
            //        return (4.0);         
         //   }else if (letterGrade.equals("A-")){
           //         return (3.7);        
         //   }else if (letterGrade.equals("B")){
           //         return (3.0);
           // }else if (letterGrade.equals("C")){
             //        return (2.0);
            //}else if (letterGrade.equals("D")){
              //       return (1.0);
          //  }else if (letterGrade.equals("F")){
           // }     return (0.0);            
double grade;
for( int i=0; i > letterGrade.length(); i++){
    // System.out.println("char = " + igrade.charAt(i));
    if(i = (+) )
        grade = grade + 0.3;
    else if (i = (-))
            grade = grade - 0.3;
    private String letterGrade;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

<String>.charAt(i) refers the the ith character of the string.  Think of the string as if it were an array with the first character in the string being referenced by <String>.charAt[0] and the next having index of 1, and so on.  You can use the charAt() method to directly reference any character within the string without modifying the string you are looking at.

Hope this helps.
Kevin CrossChief Technology OfficerCommented:
And you usually have to make the comparison like this using == as = will assign a value instead of test it for equality.

if ("A+".charAt(1) == '+') {
    // ...

So in your case:

One difficulty with this that you are overcoming by using a loop is that the index 1 may not be valid for the letter grades.  Another means to this would be:

if ("A+".endsWith("+")) {
    // ...                  
Kevin CrossChief Technology OfficerCommented:
If you have proper validation in place, you should be certain that each letter grade has at least one character A-F at the first char of string; therefore, this kind of logic would work:

switch(letterGrade.charAt(0)) {
case 'A': // set grade to 3.7; break;
case 'B': // set grade to 2.7; break;
case 'C': // set grade to 1.7; break;
case 'D': // set grade to 0.7; break;
default: // set grade to 0.0

// if grade is not zero
// check if letterGrade.endsWith("+") and add 0.3 to grade if true
// check if letterGrade.endsWith("-") and subtract 0.3 to grade if true

Since you are trying to learn I didn't put in actual code here, but just wanted to illustrate point that charAt(0) safe whereas charAt(1) not always possible as in "A", "B", etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ca1358Author Commented:
Thank you both!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.