13

I'd like to think it's simple but I just can't understand how to do this :( I've tried both == and equals so far but i got neither of them to work. Here's a small programming calculating grades for the swedish highschool system (so just ignore the swedish and look at the string comparisons. The problem here being that the statistics function variables are still set to 0 at the end of the program.

import javax.swing.*;

public class Uppgift1 {
private String[] subjects = {"Matematik","Svenska","Engelska","Idrott",
            "Bild","Fysik","Biologi","Kemi","Historia","Geografi",
            "Samhällskunskap","Religionskunskap"};`

private String[] grades = new String[subjects.length]; // Lika många element som ämnen

public void setGrades() {
    for(int i = 0; i < grades.length; i++){
        grades[i] = JOptionPane.showInputDialog("Ange betyg i " + subjects[i] + " (IG, G, VG, MVG)");
    }
}

public void statistics() {
    int ig = 0;
    int g = 0;
    int vg = 0;
    int mvg = 0;
    int points = 0;

    for(int i = 0; i < grades.length; i++){
        if(grades[i].equals("IG")){
            ig++;
        }

        else if(grades[i] == "G"){
            g++;
            points += 10;
        }

        else if(grades[i] == "VG"){
            vg++;
            points += 15;
        }

        else if(grades[i] == "MVG"){
            mvg++;
            points += 20;
        }
    }

    String output = new String("BETYGSSTATISTIK\n");
    output += "IG: " + ig + "\n";
    output += "G: " + g + "\n";
    output += "VG: " + vg + "\n";
    output += "MVG: " + mvg + "\n";
    output += "Betygspoäng: " + points;

    JOptionPane.showMessageDialog(null, output);
}

public static void main(String[] args) {
    Uppgift1 prog = new Uppgift1();
    prog.setGrades();
    prog.statistics();
}
}
Sean Owen
  • 66,182
  • 23
  • 141
  • 173
Latedi
  • 303
  • 1
  • 4
  • 11
  • Try again using `equals`. Repost if it still doesn't work. – John B Oct 22 '12 at 17:09
  • Have you tried to print the grades? Also take into consideration that perhaps the alphabet I am Swedish character is not using ascii – Wins Oct 22 '12 at 17:11
  • Yeah I tried now, it works. Also the swedish characters stays out of any calculations so they shouldn't be a problem. – Latedi Oct 22 '12 at 17:17

2 Answers2

44

You have used the first comparison correctly, but later on, you got it the wrong way some how.

Always use equals() method when comparing the content of strings, as you did in the first case.

Also, if its not working somehow, it may be because you have leading or trailing whitespace in your string. Try using it this way: -

if (grades[i].trim().equals("IG"))
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Ooh wow it's working. It's strange though since I messed around with both equals and == but I never had all of the statements be equals. Thanks :) – Latedi Oct 22 '12 at 17:16
  • @Latedi. Yeah, thats good. I saw some spaces in your string, so I thought this might be a problem. – Rohit Jain Oct 22 '12 at 17:16
  • Yeah I will... It says I have to wait though :( 2 more minutes or so and I'll accept it. – Latedi Oct 22 '12 at 17:20
  • The tip about using trim() saved me from a pit of despair and bewilderment, thank you! – SubJunk Mar 30 '20 at 20:16
4

You need to make all the String verifications with the equals method. Change your ifs to do it, instead of comparing with ==

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40