Link to home
Start Free TrialLog in
Avatar of ca1358
ca1358

asked on

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.in);
     
     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

SOLUTION
Avatar of aaronblum
aaronblum
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin Cross
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:
igrade.charAt(i)

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("+")) {
    // ...                  
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ca1358
ca1358

ASKER

Thank you both!