Asharib
Asharib

Reputation: 19

I think I'm using IF statements wrongly

So I'm trying to make a GPA calculator using Java and I'm stuck on one of the first steps. I'm trying to write a code that takes the input of the user and then converts it into a number, I will then use that number later on to calculate the GPA. Here's my code so far:

public class data {

    public static void main(String[] args) {

        String english;
        double v1 = 0;


        Scanner reader = new Scanner(System.in);

        System.out.println("Enter your english grade");
        english= reader.nextLine();

        if (strcmp(english,"A+")==0) {
            v1 = 4.4;
        }

        if (strcmp(english,"A")==0) {   
            v1= 4.0;
        }
        if (strcmp(english,"A-")==0) {  
            v1= 3.6;
        }

        if (strcmp(english,"B+")==0) {  
            v1= 3.4;
        }

        if (strcmp(english,"B")==0) {   
            v1= 3;
        }
        if (strcmp(english,"B-")==0) {  
            v1= 2.6;
        }
        if (strcmp(english,"C+")==0) {  
            v1= 2.4;
        }
        if (strcmp(english,"C")==0) {   
            v1= 2;
        }
        if (strcmp(english,"C-")==0) {  
            v1= 1.6;
        }
        if (strcmp(english,"D+")==0) {  
            v1= 1.4;
        }
        if (strcmp(english,"D")==0){    
            v1= 1;
        }

        if (strcmp(english,"D-")==0) {  
            v1= 0.6;
        }
        if (strcmp(english,"F")==0) {   
            v1= 0.0;
        }
        else {

            v1=0.0;
        }


        System.out.println(v1);

As you can see, Im printing out V1 at the end just for checking, But it keeps giving V1 as 0.0, the value when the user enters "F". If I comment that piece of the code like this:

/*if (strcmp(english,"F")==0) { 
            v1= 0.0;
        }*/

then the output value it gives is 0.6, the value when the user enters D-. I think I'm supposed to put a return system somewhere? Although I really am not sure. Any help would be greatly appreciated.

(If it wasn't established already, I am a beginner to java :P )

EDIT: Guys when I try to use the Switch method (Im using eclipse) it gives me this error: "Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted". The solution it provides then leads to further complications so I'd like to avoid switch statements :/

Upvotes: 1

Views: 215

Answers (10)

BDRSuite
BDRSuite

Reputation: 1612

use switch case, as mentioned. It will be helpful as you may get confused with large number of if statements.

switch(english){
  case "A+":
   v1=4.4
  break;

  case "D-":
   v1=0.6
  break;

  default:
   v1=0.0;
  break;
}

Upvotes: 3

sumanta
sumanta

Reputation: 359

Try this:

String english= reader.nextLine();
if (english.equals("A+")) {
    v1 = 4.4;
}
else if (english.equals("A")) {   
    v1= 4.0;
}
else if (english.equals("A-")) {  
    v1= 3.6;
}
else{
    v1=0.0;
}

Upvotes: 0

Muli Yulzary
Muli Yulzary

Reputation: 2569

You might find this quite useful:

if(english.equalsIgnoreCase("A+"))
 //do stuff...
else if(english.equalsIgnoreCase("A-"))
  //do other stuff and so on...

the equalsIgnoreCase() method ignores case sensitive, if this is not what you're looking for you might try equals() instead.

The new Java version also introduced switch-case for Strings too so your code can become this one:

switch(english){
case "A+":
    //do stuff...
    break;
case "A-":
    //do other stuff... and so on...
    break;
}

Upvotes: 2

Nihar
Nihar

Reputation: 355

Replace the code strcmp(english,"F")==0 with following english.equal ("A+")

Upvotes: 0

user3310334
user3310334

Reputation:

Basically, you are setting the value to zero whether the user enters F, or anything else:

    if (strcmp(english,"F")==0) {   
        v1= 0.0;
    }
    else {

        v1=0.0;
    }

see here, you've set v1 to 0.0 if (strcmp(english,"F")==0), but also if the user didn't enter F. I think you meant to put something else in stead of 0.0 in the if part, like this:

    if (strcmp(english,"F")==0) {   
        v1= /*something else*/;
    }
    else {

        v1=0.0;
    }

because that's what seemed to surprise you.

Upvotes: 0

Harry
Harry

Reputation: 3195

You are saying if string is not F, set the value of v1= 0.0

if(strcmp(english,"F")==0) {   
        v1= 0.0;
    }
    else {

        v1=0.0;
    }

Upvotes: 0

lmm
lmm

Reputation: 17431

An else attaches to the previous if; you do a lot of stuff and then at the end you do if(...) { v1 = 0.0 } else { v1 = 0.0 }, setting the value to 0.0 in any case.

You might want to use else if at every step, which will solve the immediate problem.

Upvotes: 0

progsource
progsource

Reputation: 639

    if (strcmp(english,"F")==0) {   
        v1= 0.0;
    }
    else {

        v1=0.0;
    }

with that else you say - as long as it is not F, put 0.0 in v1.

Upvotes: 7

Bathsheba
Bathsheba

Reputation: 234715

The way you've written it, v1 will be 0.0 unless english is "F".

You need to use else if:

if (strcmp(english,"A+")==0) {
    v1 = 4.4;
} else if (strcmp(english,"A")==0) {  
     // and so on

Some Java folk like to switch on strings. But that's a reasonably new feature in Java and the break statements that you'll need to introduce can obfuscate.

Upvotes: 5

Mahesh Acharya
Mahesh Acharya

Reputation: 11

Remove the last else part. it should be good.It is because no matter what the grade is, if it is not "F", it is going to the else statement and setting it to 0.0

Upvotes: 0

Related Questions