0
package GC;

import java.util.Scanner;

public class main {
public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    System.out.println("Calculate exam grade needed(e) or final grade received(f)?");
    String x = input.nextLine();
    if (x == "e"){
        double q1, q2, f, e;
        Scanner inputa = new Scanner(System.in);
        Scanner inputb = new Scanner(System.in);
        Scanner inputc = new Scanner(System.in);

        System.out.print("Quarter 1: ");
        q1 = inputa.nextInt();
        System.out.print("Quarter 2: ");
        q2 = inputb.nextInt();
        System.out.print("Final grade wanted: ");
        f = inputc.nextInt();

        e = 5*(f-0.4*(q1)-0.4*(q2));
        if(e == (int)e){
            System.out.println((int)e);
        }
        else{
            e = 0.01*((int)(e*100));
            System.out.println(e);
        }
    }


    else if (x == "f"){

    }
    else{
        System.out.println("ERROR");
    }



}
}

Even if I input e at the beginning, it always goes to the else statement. I may have done something wrong with getting a String from user input or with my if parameters. What am I missing?

Wilson
  • 8,570
  • 20
  • 66
  • 101
  • try reading http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-types-to-string and http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html – ShinTakezou Mar 31 '12 at 22:49

4 Answers4

3
if (x == "e"){

You should compare String objects with equals() and not operator==. You should also be aware of equalsIgnoreCase(), which checks for equality of Strings, while ignoring which case each character is - it might be useful sometimes.

operator== checks for identity - if the two objects are actually the same object [the variables referencing the same object], while equals() is checking for equality - if their content is the same.

change

if (x == "e"){

to

if ("e".equals(x)) {

and do the same for else if (x == "f"){

amit
  • 175,853
  • 27
  • 231
  • 333
  • If there is an error in the Java VM. In general though, this is the proper convention to perform the method on the constant. –  Mar 31 '12 at 22:27
  • @Legend: If there is an error in the JVM - nothing is predictable. I approved your edit, because it may be a *better practice* but it is definetly not *wrong*. – amit Mar 31 '12 at 22:31
1

You need to use the "equals" compare method. X is a string and there is no overloading operator for "==" on it to compare values.

Always take a look at the javadocs for an object if you're unsure.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

if(x.equals("e")){

}
amit
  • 175,853
  • 27
  • 231
  • 333
DavidB
  • 2,064
  • 3
  • 17
  • 17
0

Because e is double and when you cast it to int you lose some bits.

ren
  • 3,843
  • 9
  • 50
  • 95
  • that makes no sense, e is a variable from inside the if – Diego Mar 31 '12 at 22:15
  • 1
    No, the statement you are refering to: `if(e == (int)e){` is most likely unreachable with this piece of code. – amit Mar 31 '12 at 22:16
  • pieces like `e == (int)e` should sound strange. The intention is "if e has no fractional part, then do A, else do B". A will print `e` which is expected to have no fractional part, while the intention of B, it seems it's to have 2 digits after the decimal point. It is the *wrong* way to do it. First, being `e` the result of several floating point operations, it is likely always != from `(int)e`. Then, you have to learn how to format numbers to string, or round them properly with the desidered "precision". – ShinTakezou Mar 31 '12 at 22:45
0

try (maybe you are getting a blank space:

String x = input.nextLine().toString().trim()

or

x.equals("e")
Diego
  • 34,802
  • 21
  • 91
  • 134