?
Solved

Java charAT Method

Posted on 2008-11-14
4
Medium Priority
?
1,775 Views
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.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
Comment
Question by:ca1358
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:aaronblum
aaronblum earned 400 total points
ID: 22961845
<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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22962051
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
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1600 total points
ID: 22962152
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
 

Author Closing Comment

by:ca1358
ID: 31516868
Thank you both!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 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