-3

So im pretty new to java and creating something for school. My problem is the last If statements i make compare strings and i understand that != or >= doesnt work but i dont understand what to use in place of it. any help?

I've tried looking up the proper way to use that line but i just didnt really understand what exactly everyone was stating when comparing the 2 letters.

package Secrets_hw2p1;

/**
 *
 * @author secrets
  */
  //importing Scanner
   //import scanner
    import java.util.Scanner;
       public class secrets_hw2p1 {

    /**
     * @param args the command line arguments
     */
       public static void main(String[] args) {
      // TODO code application logic here

    //Creating the Scanner object
    Scanner input = new Scanner (System.in);
    //getting students goal grade
    System.out.println("What is your Goal Letter Grade? ");
    String goalGrade = input.next();

    //Enter assignment scores. and read scores
    System.out.println("Please enter in your two assignment scores followed"
    +"by your two exam scores one at a time followed by enter ");
    float assignment1 = input.nextFloat();
    float assignment2 = input.nextFloat();
    float exam1 = input.nextFloat();
    float exam2 = input.nextFloat();

    //calculations of averages
    double goal = assignment1 * .40 + assignment2 * .40 + exam1 * .30 +
            exam2 * 0.30;
    int grade;
    //Calculate Letter grade
    if (goal >= 90)
        grade = 'A';
    else if (goal >= 80)
        grade = 'B';
    else if (goal >= 70)
        grade = 'C';
    else 
        grade = 'D';



    //prompt the user for how they want there grade
     System.out.println("Press 1 to display letter grade or press 2 to"
     +"see if you met your goal ");  
     double number = input.nextDouble();

     //if user inputed 1
     if (number == 1)
        System.out.println ("Final grade:" + grade);
     //if user inputed 2      
     if (number == 2)  
        if (goalGrade != grade)
                System.out.println("You have not met or exceeded your goal" 
                        +" grade");
        else if (c1.goalGrade >= grade)
                System.out.println("You met or exceded your goal grade !");



    }

}
  • `grade` should be a `String`. And you should compare strings using `equals()`. – shmosel Sep 01 '17 at 03:11
  • 1
    Note that `"ran into a issue with my java program"` is a poor title for a StackOverflow question as it tells us nothing whatsoever of use. Please consider using much more informative question titles, ones that summarize your actual problem since doing this will help you get better help. For more on this, please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. – Hovercraft Full Of Eels Sep 01 '17 at 03:11
  • @shmosel Or possible a `char`, but definitely not an `int` being used to store character literals. – Tim Biegeleisen Sep 01 '17 at 03:12
  • Consider using brackets `{ }` around your `if` and `else` statements. I don't know if what you have now would work as intended, but I would never even try doing that in Java. – Tim Biegeleisen Sep 01 '17 at 03:13
  • @TimBiegeleisen You can nest single control flow statements without braces. Not that I would, just saying. – shmosel Sep 01 '17 at 03:14

2 Answers2

0

Use equals() method instead and change grade type to String.

String grade = ""; // changed datatype to String

Then:

   //if user inputed 2      
             if (number == 2)  
                if (goalGrade.equals(grade))
                        System.out.println("You have not met or exceeded your goal" 
                                +" grade");

Read: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

Awais Syed
  • 51
  • 9
0

Since strings are objects you aren't able to use your standard = or <= you are going to need to use the .equals method. So instead of

if (goalGrade != grade)

you would want

if (!goalGrade.equals(grade))
Dan Hessler
  • 371
  • 1
  • 2
  • 14