Hello everyone hope all is well. So I'm learning java and been at it all night and this is my last hope. I can't figure out why a certain part of my code is not being executed after the conditions of my if statement is true.

/**
 * @(#)mathsoftware.java
 *
 *
 * @author
 * @version 1.00 2010/7/1
 */
import java.util.Scanner;	//input stuff

public class mathsoftware {

    /**
     * Creates a new instance of <code>mathsoftware</code>.
     */
    public mathsoftware() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner mygrade = new Scanner(System.in);													//input


		System.out.println("How many courses have you taken?");
		int coursecount = mygrade.nextInt();														// number of courses taken

		if (coursecount < 2)	{																	// in number of courses less than 2
			System.out.println("Cannot Compute need at least 2 classes");
		}	else if	(coursecount == 2)	{																	// if courses equal 2
				System.out.println("What is the grade of your first class?");
				String grade001 = mygrade.next();
				double gradenum001;

				if	(grade001 == "A")	{
					gradenum001 = 4.00;
				}	else if	(grade001 == "B"){
					gradenum001 = 3.00;
				}	else if (grade001 == "C"){
					gradenum001 = 2.00;
				}	else if (grade001 == "D"){
					gradenum001 = 1.00;
				}	else	{
					gradenum001 = 0.00;
				}

				System.out.println("What is the grade of your next class?");
				String grade002 = mygrade.next();
				double gradenum002 = 0;

				if (grade002 == "A")	{
					gradenum002 = 4.00;
				}	else if	(grade002 == "B"){
					gradenum002 = 3.00;
				}	else if	(grade002 == "C")	{
					gradenum002 = 2.00;
				}	else if (grade002 == "D")	{
					gradenum002 = 1.00;
				}	else	{
					gradenum002 = 0.00;
				}
				double printedg;
				printedg = gradenum001 + gradenum002 / 2;
					System.out.println(printedg);
						}	else {
					System.out.println("tested");
				}

			}



  }

in the snippet

if	(grade001 == "A")	{
					gradenum001 = 4.00;
				}	else if	(grade001 == "B"){
					gradenum001 = 3.00;
				}	else if (grade001 == "C"){
					gradenum001 = 2.00;
				}	else if (grade001 == "D"){
					gradenum001 = 1.00;
				}	else	{
					gradenum001 = 0.00;
				}

the code is not running at all and I get the value of 0.0


Sorry I know my code is horrible but I am learning just stuck. Everything compiles without errors. I found out it was this section because I inserted a println and some text in the if statement that should have placed a 4.00 value to gradenum001 variable. Thank you for your help.

ola Seanvitalaim,

your mistake is you compare strings with == plz use .equals i.e.

if (grade002.equals("A"))	{
					gradenum002 = 4.00;
				}	else if	(grade002.equals("B")){
					gradenum002 = 3.00;
				}	else if	(grade002.equals("C"))	{
					gradenum002 = 2.00;
				}	else if (grade002.equals("D"))	{
					gradenum002 = 1.00;
				}	else	{
					gradenum002 = 0.00;
				}

hope it helps

krefie

The Krefie is right.

Java use operator "==" to check equality by address. So in your case when Java find "D" literal - it's create new String object with value "D" which will be has different address in memory in runtime.

And method

equals( Object o )

which available in all ancestors of Object type needs to check equality by value. You can overload it and use by own manner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.