Java charAT Method

Posted on 2008-11-14
Last Modified: 2013-12-14
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

Question by:ca1358
    LVL 2

    Assisted Solution

    <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.
    LVL 59

    Expert Comment

    by: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:

    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("+")) {
        // ...                  
    LVL 59

    Accepted Solution

    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.

    Author Closing Comment

    Thank you both!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now