0

I want to count the occurrence of every number that appears in the array. I've seen other answers, but I do not understand why my method does not work. I have an Array with random numbers:

int[] fält = new int[20]

This is what I did:

public static String statistik(int[] fält) {
    String poäng[] = new String[20];
    String output = "";

    //Clear out the array:
    for (int i = 0; i < poäng.length; i++) {
        poäng[i] = "";
    }

    //Add a star for each time a number appears
    for (int i = 0; i < fält.length; i++) {
        for (int t = 0; t < fält.length; t++) {
            if (fält[t] == i) {
                poäng[i] += "*";
            }
        }
    }

    //Print it out
    for (int i = 0; i < fält.length; i++) {
        output += (i + 1) + ": " + poäng[i] + "\n";
    }
    JOptionPane.showMessageDialog(null, output);
    return "";
}

Not all numbers get a star and it all ends up weird. Why?

6
  • 2
    Dis you tried putting if (fält[t] == fält[i]) ? Commented Oct 29, 2015 at 10:56
  • 1
    Whilst Java does have support for unicode variable names, I would strongly recommend that you stick to ASCII as this will avoid platform specific encoding problems. Commented Oct 29, 2015 at 10:56
  • As to the concrete issue - fält[t] == i is wrong. I leave it as an exercise to the you to work out why. I would also note that sorting the array first would give you the possibility to make the code much faster - again I leave how up to you. Commented Oct 29, 2015 at 10:58
  • @BoristheSpider Why is it wrong? I failed your exercise. Commented Oct 29, 2015 at 11:01
  • 1
    @Habbo your need to learn not be spoonfed. Take a pencil and paper and write out exactly what this code does, on each of the n^2 iterations with an array of size, say, 3. Hint: don't use any numbers less that 3 in your array. Commented Oct 29, 2015 at 11:02

4 Answers 4

2

You are checking if (fält[t] == i) please change it to if (fält[t] == fält[i])

Sign up to request clarification or add additional context in comments.

Comments

1

This line :

output += (i + 1) + ": " + poäng[i] + "\n";

this will start to print "1 :" BUT you count from 0 that's why the output seems weird.

public static String statistik(int[] fält) {
  String poäng[] = new String[fält.length];
  String output = "";

  //Clear out the array:
  for (int i = 0; i < poäng.length; i++) {
      poäng[i] = "";
  }

  //Add a star for each time a number appears
  for (int i = 0; i < fält.length; i++) {
    for (int t = 0; t < fält.length; t++) {
      if (fält[t] == i) {
        poäng[i] += "*";
      }
    }
  }

  //Print it out
  for (int i = 0; i < fält.length; i++) {
    output += i + ": " + poäng[i] + "\n";
  }
  System.out.println(output);
  return "";
}

Comments

1

1)Hi, @Habbo, it is because the flat[] array only consists 0's 2) only in first iteration i value will be 0 and then it never be zero again.

if (fält[t] == i) {
     poäng[i] += "*";
  }

4)so poäng[i] inside this "i" will only zero and never increases 5) that is why you are having weird result

Comments

0

you should change the output message

from output += (i + 1) + ": " + poäng[i] + "\n";

to output += i + ": " + poäng[i] + "\n";

at all i think you could optimize your code by sort it first this will cost 2 Loops then loop upon the sorted array like this

    int temp= fält[0];
    output = temp + ": *";
    for (int i = 1; i < fält.length; i++) {
        if(fält[i] >temp){
            temp = fält[i];
            output +="\n"+ temp + ": *";
        }
        else{
            output +="*";
        }
    }

you will have only 3 Loops In your Code not 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.