0

When I try to get an item from an arraylist, it gives me an error saying

error: incompatible types: Object cannot be converted to double
       double highest = numbers.get(0);

and also this

error: bad operand types for binary operator '<'
        if(highest < numbers.get(i)){

I am new to java, but not programming, so I am not really sure what to do because the numbers are doubles (I have checked) but it still gives me an error when I try to select an item from the array or compare (highest < numbers.get(i))

Here is the full code:

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class Main {
  public static void main (String[] args)throws IOException{
    boolean keepRunning = true;
    ArrayList numbers = new ArrayList();
    ArrayList sortedNumbers = new ArrayList();

    while(keepRunning){
        Scanner input = new Scanner(System.in);
        System.out.print("Do you want to sort the numbers or add a number?");
        String answer = input.nextLine();

        if(answer.equals("sort")){
           double highest = numbers.get(0);
           for(int i = 0; i < numbers.size(); i++){
            if(highest < numbers.get(i)){
              highest = numbers.get(i);
            }
            System.out.print(numbers.get(i));
           }
          // System.out.println(highest);
           System.out.println("Bye Bye.");
           keepRunning = false;
        }else if(answer.equals("add")){
            System.out.print("What number to you want to add?");
            numbers.add(Double.parseDouble(input.nextLine()));
            System.out.println("Added number.");
        }else{
            System.out.println("That is not an option.");
        }
    }
  }
}
Stuart Fong
  • 603
  • 1
  • 13
  • 21

4 Answers4

3

Because you did not use generics when constructing your list ArrayList numbers = new ArrayList(); the JVM will allow that list to contain any type of objects. Thus you need to include generics ArrayList<Double> numbers = new ArrayList<Double>(); or cast when you retrieve values from the list:

double highest = (double) numbers.get(0);
if(highest < (double) numbers.get(i)){
        highest = numbers.get(i);
}
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
1

When creating an ArrayList, you need to identify the objectType whether it be Integer, String, Double, or even custom classes.

ArrayList<ObjectType> list = new ArrayList<ObjectType>();

//In this case, the ObjectType is a double, so the code would be:

ArrayList<Double> list = new ArrayList<Double>();

Note that ArrayList<double> can not be used in Java (currently) since double is a primitive data-type. You therefore need to use its wrapper class Double. However Java is able to automatically convert double to Double and vice versa whenever needed. This process is called auto-boxing (or -unboxing).


What you are currently doing, i.e. not identifying the object type, is called raw-type.

It is an artifact of the time where generics where not available in Java (below version 1.5). The usage is strongly discouraged.

Even if you want to put everything inside you should use at least List<Object> instead of just List.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
JasperHsu
  • 45
  • 11
  • You cannot use keywords for generics, it would be: `ArrayList list = new ArrayList();`. Also, you always have to identify the object type. – Cardinal System Oct 09 '17 at 18:35
  • 1
    The generic usage of **primitive datatypes** is **not** possible in Java (at least currently). You need to use the wrapper classes `Double` etc. instead. – Zabuzard Oct 09 '17 at 18:35
  • Oh ok! Sorry about that! – JasperHsu Oct 09 '17 at 18:38
  • '*you need to identify*' - Well, one can use it without, this is called **raw-type**. However the usage of *raw-types* is **strongly discouraged**, it's just there for downwards compatibility. – Zabuzard Oct 09 '17 at 18:40
  • @Zabuza, just out of curiosity, why would one use a raw-type? – JasperHsu Oct 09 '17 at 18:42
  • @JasperHsu If you need to work with versions **below Java 1.5**, before generics came to Java. Other than that there is no scenario since `List` essentially does the same as `List`. All modern IDEs will warn about the usage of raw-types. – Zabuzard Oct 09 '17 at 18:45
0

You need to specify the type of array list. else it will be a default Object type.

 ArrayList<Double> numbers = new ArrayList();

else cast the retrieved value to double.

double highest = (double)numbers.get(0);
0
double highest = (double)numbers.get(0);
       for(int i = 0; i < numbers.size(); i++){
           int compare = Double.compare(highest, (double)numbers.get(i));
       if(compare < 0){
          highest = (double)numbers.get(i);
        }

        System.out.print(highest+"   ");
       }
puneet
  • 1
  • 2