-1

I've got an array with Strings in it. For this example, I'll use an array filled with classes. And there's an array with grades getting filled by user interaction.

Now, I've got everything working except for the part where the last two classes which give more Study points then the rest get printed double and I can't figure out where this goes wrong.

Some code has dutch names so here's a list of the translations:
Cijfer is grade
vakken is classes

public static void main (String[] args) {

    Scanner input = new Scanner(System.in);
    final int MAX_ECTS = 39;
    int ects = 0;
    final int ECTS_VAK = 3;
    final int ECTS_PROJECT = 12;
    String[] vakkenArray = {"Programming", "Business", "Infrastructure", "Databases", "User Interaction", "Project Fasten Your Seatbelts", "Project Agile Developement"};
    double[] cijferArray = new double[7];

    for (int i = 0; i < cijferArray.length; i++)
    {
        System.out.println("Voer A.U.B het cijfer in voor " + vakkenArray[i]);
        cijferArray[i] = input.nextDouble();
    }

    for (int i = 0; i < vakkenArray.length; i++)
    {
        if (i < 5)
        {
            if (cijferArray[i] >= 5.5)
            {
                ects = ects + 3;
                System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + ECTS_VAK);

        }

        if (cijferArray[i] <= 5.5)
        {
            System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + 0);
        }

    }
    else 
    {
        if (vakkenArray[i].equals("Project Fasten Your Seatbelts") && (cijferArray[i] >= 5.5))
        {
            System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + ECTS_PROJECT);
            ects = ects + 12;
        }
        else 
        {
            System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + 0);
        }

        if (vakkenArray[i].equals("Project Agile Developement") && (cijferArray[i] >= 5.5))
        {
            System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + ECTS_PROJECT);
            ects = ects + 12;
        }
        else 
        {
            System.out.println("Vak/Project:" + vakkenArray[i] + " Cijfer:" + cijferArray[i] + " ECTS Behaald: " + 0);
        }
    }


    System.out.println("Totaal behaalde ECTS: " + ects + "/39");     

}

This is the output of my code:

Vak/Project:Programming Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Business Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Infrastructure Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Databases Cijfer:5.0 ECTS Behaald: 0
Vak/Project:User Interaction Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Fasten Your Seatbelts Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Fasten Your Seatbelts Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Agile Developement Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Agile Developement Cijfer:5.0 ECTS Behaald: 0
Totaal behaalde ECTS: 0/39

This is how it should be:

Vak/Project:Programming Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Business Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Infrastructure Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Databases Cijfer:5.0 ECTS Behaald: 0
Vak/Project:User Interaction Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Fasten Your Seatbelts Cijfer:5.0 ECTS Behaald: 0
Vak/Project:Project Agile Developement Cijfer:5.0 ECTS Behaald: 0
Totaal behaalde ECTS: 0/39

See how the Projects get printed double.

msrd0
  • 7,816
  • 9
  • 47
  • 82
B-W
  • 57
  • 6
  • 5
    do not compare strings via `==` operator. Use `equals` method. `==` compares object references (address in the computer memory), not the string object values. –  Sep 19 '14 at 17:02
  • Alright will do. I'm not used to java just yet so... – B-W Sep 19 '14 at 17:03
  • check this tutorials for example: http://www.javabeginner.com/ –  Sep 19 '14 at 17:04
  • Updated it in the thread above – B-W Sep 19 '14 at 17:08
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – takendarkk Sep 19 '14 at 17:17

1 Answers1

0

I've fixed it by changing the else-statement to the following:

 if (vakkenArray[i].equals("Project Fasten Your Seatbelts")
         && (cijferArray[i] <= 5.5))
msrd0
  • 7,816
  • 9
  • 47
  • 82
B-W
  • 57
  • 6