0

I have an ArrayList filled with objects of the class Result. Every Result has an attribute named Category.

I am trying to make method with a for-loop which prints out every result with the input Category from the user.

Currently the for-loop works but the if-statement which separates whether or not to print out a result doesnt work and that is what Im searching help for.

The if statement standing alone is what I think is not working at the moment since ive tried all the other code.

String categoryToPrint;
    System.out.println("Which category would you like to print out results for?");
    categoryToPrint = scanner.nextLine();
    categoryToPrint = normalisera(grenAttVisa); //method making all letters small and first letter capital.
    System.out.println("Resultlist for" + categoryToPrint );
    for (int i = 0; i < resultlist.size(); i++) {
        Athlete matched = null;
        Result res = resultlist.get(i);


        if (res.categoryName().equals(categoryToPrint)) {


        for (int x = 0; x < resultlist.size(); x++) {
            Athlete del = athletes.get(x);
            if (res.athleteStartNumber() == del.startNumber()) {
                matched = del;
                break;
            }
        }
        System.out.println(matched.surName() + " " + matched.lastName() + " has the result: " + res.categoryValue());
    }
    }
A.Bohlund
  • 195
  • 1
  • 10

1 Answers1

1

Change

for (int x = 0; x < resultlist.size(); x++)

to

for (int x = 0; x < athletes.size(); x++)

Let's consider this situation for a while:

resultlist = [result1, result2, result3] // size() == 3

athletes = [athlete1, athlete2] // size() == 2

Relying on your actual code:

for (int i = 0; i < resultlist.size(); i++) {
    for (int x = 0; x < resultlist.size(); x++) {
        Athlete del = athletes.get(x);
        ...

Here is how it goes for the inner loop within the first iteration of the outer loop:

resultlist.size() == 3
i == 0, x == 0 ====> Athlete del = athletes.get(0); // x < 3, good
i == 0, x == 1 ====> Athlete del = athletes.get(1); // x < 3, good
i == 0, x == 2 ====> Athlete del = athletes.get(2); // x < 3, good but the athletes arraylist has only 2 elements, Exception raised
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • Tried it, didnt work :( Could there more than one mistake ive made? I get the error message: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 – A.Bohlund Jan 03 '16 at 11:26
  • Oh I've just noticed it. It's a **StringIndexOutOfBoundsException**. So in deed, there is something you are not showing us. Try debugging your code, to figure out which line is the problematic one. – Mohammed Aouf Zouag Jan 03 '16 at 11:27
  • private String normalize(String s) { return s.trim().substring(0,1).toUpperCase() + s.substring(1).toLowerCase(); } this is a method I have which I use to make to characters small and first letter capital. I use this method on the input text in many of my methods to make them comparable. I havent had a problem with it so far but now it comes up as a error. – A.Bohlund Jan 03 '16 at 11:31