-1
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
       String input = "";
       Scanner in = new Scanner(System.in);    
      
       System.out.println("math");
       input= in.nextLine();
       math(input);
       System.out.println("end");

public static void math (String input)
    {
        if (input=="a" ||  input=="A")
        {
        System.out.println("4.0");
        }
        else if (input== "A-" || input== "a-")
        {
        System.out.println("3.7");
        }
       //etc
    }
}    

What is being printed out is this:

math

a (I entered "a" as input)

end

My method section is being skipped completely! I know that I am calling my method right cuz I did it for a different project last week and legit just copy and pasted the format over!

  • you entered `a` as input for math and it's supposed to get double as a parameter so differently, it will not work. and pay attention to the syntax error that you have, you missed `}` before the declaration of `public static void math(Double grade){..}`. – odaiwa Oct 03 '22 at 05:36
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Tim Moore Oct 03 '22 at 08:29

1 Answers1

0

Try this snippet of code that I had just written for you below.

Ultimately, you should compare strings using variableName.equals(object).

As a tip, I would also recommend that you indent your code so that it looks more cleaner and to improve readability.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Math Letter Grade! \nPlease enter Letter Grade:");
    String input = sc.next();

    // This calls the method
    // Passing what was entered into the Math Parameter.
    Math(input);

    System.out.println("Program Terminates.");
}

public static void Math(String input) {
    if (input.equals("A") || input.equals("a")) {
        System.out.println("Your GPA is 4.0");
    } else if (input.equals("A-") || input.equals("a-")) {
        System.out.println("Your GPA is 3.7");
    } else {
        System.out.println("More information to be outputted..." +
                " But You did well!");
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Skyhigh
  • 39
  • 9
  • I appreciate your help, but we are not allowed to use things like "input.equals" and "sc.next". But still, thank you for helping! :) – The lol frog Oct 05 '22 at 03:48
  • `Sc` is just the name of the Scanner, a common name in the Java world. You can also substitute `nextLine()` too. You can rename the name of the Scanner to `in` as shown in your src. When you are using the `==` sign to compare two strings, you are only comparing the object reference. I appreciate the reply, hope I was able to assist you in some way. You can even run the program to help understand what's happening in the process. @Thelolfrog – Skyhigh Oct 05 '22 at 22:07