Why is the index of my ArrayList index always returning -1?

93 Views Asked by At

I am trying to make a composition percent calculator (mainly to do my Chemistry assignments faster), and I am at the point where the actual calculations are being done. I am trying to get the masses from the elements the user puts in, but it always returns -1 and with this error:

enter image description here

I have tried to debug it, but my IDE (IntelliJ IDEA) won't let me see the variables or try to check anything.

NOTE: The error happens on line 64.

import java.util.ArrayList ;
import java.util.Scanner ;
import java.util.concurrent.TimeUnit ;

import static java.lang.System.in ;
import static java.lang.System.out ;

public class Main {

    static void clearScreen() {
        out.println("\033[H\033[2J") ;
        out.flush() ;
    }

    public static void main(String[] args) {
        String elementID ;
        int totalElements ;
        int elementCount ;
        double elementMass ;
        double totalMass ;
        Scanner sc = new Scanner(in) ;
        ArrayList<String> elementNames = new ArrayList<>() ;
        ArrayList<String> wantedElements = new ArrayList<>() ;
        ArrayList<Integer> wantedElementsCount = new ArrayList<>() ;
        ArrayList<Double> elementMasses = new ArrayList<>() ;

        while (true) {
            out.print("\nEnter amount of elements in compound: ") ; totalElements = Integer.parseInt(sc.nextLine()) ;
            out.println("TOTAL ELEMENTS COUNT: " + totalElements + "\n") ;
            wantedElements.clear() ;
            wantedElementsCount.clear() ;
            
            for (int elms = 1 ; elms <= totalElements ; elms++) {
                out.print("Enter name for element #" + elms + " (it will be remembered if spelt correctly): ") ; elementID = sc.nextLine() ;
                out.println("ENTERED ELEMENT NAME: " + elementID + "\n") ;
                
                if (!(elementNames.contains(elementID))) {
                    elementNames.add(elementID) ;
                    out.print("Enter mass for element #" + elms + ": ") ; elementMass = Double.parseDouble(sc.nextLine()) ;
                    out.println("ENTERED ELEMENT MASS: " + elementMass + "\n") ;

                    if (!(elementMasses.contains(elementMass))) {
                        elementMasses.add(elementMass) ;
                    }
                    
                    wantedElements.add(elementID) ;
                } else {
                    wantedElements.add(elementNames.get(elementNames.indexOf(elementID))) ;
                }

                out.print("Enter the amount of the element there is: ") ; elementCount = Integer.parseInt(sc.nextLine()) ;
                out.println("ENTERED ELEMENT AMOUNT: " + elementCount + "\n") ;
                wantedElementsCount.add(elementCount) ;
            }

            totalMass = 0 ;

            for (int i = 0 ; i < elementMasses.size() ; i++) {
                totalMass += elementMasses.get(i) ;
            }

            for (int i = 0 ; i < wantedElements.size() ; i++) {
                out.print("% MASS OF " + wantedElements.get(i) + ": ") ; 

            // The wantedElements.indexOf() part tries to get the index of the current elements that the user wants to use in the given equation, which will correlate with the elementMasses list

            out.println(elementMasses.get(wantedElements.indexOf(i)) * wantedElementsCount.indexOf(i) / totalMass * 100) ;  // This is where the error happens
            }

            out.println("TOTAL COMPOUND MASS" + totalMass) ;
            
        }
    }
}
1

There are 1 best solutions below

0
Ethan Conrad On BEST ANSWER

The problem here is your line:

out.println(elementMasses.get(wantedElements.indexOf(i)) * wantedElementsCount.indexOf(i) / totalMass * 100)

This part:

elementMasses.get(wantedElements.indexOf(i))

doesn't make sense which is why it's throwing an error.

When using indexOf, you should be passing a wantedElement in there, not integer "i". For example:

If you have a list of wantedElements with 2 values in that list, say "Test1" and "Test2". Then performing wantedElements.indexOf("Test1") would return you an index of 0 and wantedElements.indexOf("Test2") would return you an index of 1.

As I'm not sure exactly what you are trying to do in your example, it's hard to assist any further. Hopefully this gives you a better understanding of how the indexOf() function works to aid you going forward with your project