• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1787
  • Last Modified:

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

0
ca1358
Asked:
ca1358
  • 2
2 Solutions
 
aaronblumCommented:
<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.
0
 
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:
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("+")) {
    // ...                  
}
0
 
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.
0
 
ca1358Author Commented:
Thank you both!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now