0

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();
        }
    }
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 4
    String split returns a string. so `listValues` is actually an ArrayList but you've declared it an `ArrayList`. – Roddy of the Frozen Peas Nov 06 '18 at 18:48
  • Ah, ok, that makes sense. I'm coming from Python if that clarifies the mistake, type conversions are very straightforward compares to here. In this case, I guess I generate the list, then convert the list elements into floats afterward? Or is there a doc where I can read up on how to convert directly to a float by this method? –  Nov 06 '18 at 18:50
  • `new ArrayList` instantiates a raw type. You should have a warning on that line. You want `new ArrayList` or just `new ArrayList<>`. – Daniel Pryden Nov 06 '18 at 18:52
  • @DanielPryden: Technically true but there's an issue with conversion between the type of lists which would be better explained in an answer. – Makoto Nov 06 '18 at 18:52
  • @Makoto: Yes, I just saw that as well. – Daniel Pryden Nov 06 '18 at 18:53

2 Answers2

4

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));
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 3
    The creation of the `ArrayList` is pointless when you create a list with `toList`, since the list is never modified.' – Lii Nov 06 '18 at 19:11
  • @Lii Without creating a `new ArrayList` it won't compile. – Roshana Pitigala Nov 06 '18 at 19:19
  • 2
    you can use Collectors.toCollection(ArrayList::new) to collect directly into an ArrayList – EduSanCon Nov 06 '18 at 19:38
  • 1
    @RoshanaPitigala: When you are saying that something doesn't compile it is helpful to say *what* is and what error message it gives. It this case the problem probably is that the type of the `listValues` variable must be changed to `List`. But that's okay in the posters code, because they doesn't need the list to be an `ArrayList`. – Lii Nov 06 '18 at 19:39
0
 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.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99