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.");
}
}
}
}