user10614851
user10614851

Reputation:

Java list comparison resulting in error I can't pin down

I'm working on this script to compare elements sequentially in a list/array in Java. Essentially the code takes in a CSV, converts it to a list and iterates over the list's size. When I attempt a comparison between the values I run into an error I can't figure out in this case:

Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Float (java.lang.String and java.lang.Float are in module java.base of loader 'bootstrap')
at list.main(list.java:22)

The code is:

public class list{

    public static void main(String[] args){
        System.out.println("Enter your filename");
        Scanner inputValue = new Scanner(System.in);
        String fileLocation = inputValue.nextLine();
        try {
            String checkvalue = new String(Files.readAllBytes(Paths.get(fileLocation)));
            ArrayList<Float> listValues = new ArrayList(Arrays.asList(checkvalue.split("[\\r\\n]+")));
            System.out.println(listValues);
            for (int i = 0; i < listValues.size(); i++){
                System.out.println(listValues.get(i));
                float valueA = listValues.get(i);
                float valueB = listValues.get(i+1);
                if (valueA <= valueB){
                    System.out.println("True");
                }
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 4345

Answers (2)

Sweeper
Sweeper

Reputation: 271355

Replace this line

ArrayList<Float> listValues = new ArrayList(Arrays.asList(checkvalue.split("[\\r\\n]+")));

with this:

ArrayList<Float> listValues = new ArrayList<>(
    Arrays.stream(
        checkvalue.split("[\\r\\n]+"))
            .map(Float::parseFloat).collect(Collectors.toList()
    )
);

Your have two main mistakes

  • new ArrayList creates a raw type. And you should not use raw types. This is why I have added <>.
  • Arrays.asList(checkvalue.split("[\\r\\n]+")) creates a List<String>, which can't be converted to a ArrayList<Float>. This is why I mapped everything in the String[] with Float::parseFloat.

Or more simply, as suggested by littleLouito,

ArrayList<Float> listValues = Arrays.stream(
        checkvalue.split("[\\r\\n]+"))
            .map(Float::parseFloat).collect(Collectors.toCollection(ArrayList::new));

Upvotes: 4

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15180

 ArrayList<Float> listValues = new ArrayList(Arrays.asList(checkvalue.split("[\\r\\n]+")));

checkvalue.split(...) returns a string array. So the right hand side of this expression builds up an ArrayList<String[]>. You've declared listValues as ArrayList<Float>, though, so it's trying to tell you that you have a type mismatch.

To solve, you'll need to parse the Strings into floats. Java's Float class has a nice parseFloat method you can use.

Upvotes: 0

Related Questions