-1

I have made a list called list and two doubles called Weight and Age

Then I tried getting an element from the list like this list.get((Age * 2) - 1), but it told me I can't put a double in the get() function, so I tried doing this (int)(Age-1) but when I put that chunk of code in an if() command like so if(list.get((int) ((Age * 2) - 1)) > Weight.intValue()) but then for some reason, it gives me this exeption:

operator ">" cannot be applied to "java.lang.Object","int"

WHY, JUST WHY!

in total, here is my code (Note that I wrote it with some extra android libraries)

package com.example.ademapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.Start);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText text = (EditText)findViewById(R.id.Weight);
            EditText text3 = (EditText)findViewById(R.id.Age);
            Double Weight = Double.parseDouble(text.getText().toString());
            EditText text2 = (EditText)findViewById(R.id.Height);
            Double Height = Double.parseDouble(text2.getText().toString());
            Double IMC = Weight/Height/Math.pow(Height/100,2);
            TextView Result=(TextView)findViewById(R.id.Result);
            List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
            Double Age = Double.parseDouble(text3.getText().toString());
            RadioButton Adult = (RadioButton) findViewById(R.id.Adult);
            RadioButton Kid = (RadioButton) findViewById(R.id.Kid);
            if(Adult.isChecked())
            {
                if (IMC<18.5)
                {
                    Result.setText("Tu est Trop Maigre!");
                }
                else if (IMC>30)
                {
                    Result.setText("Tu est Trop Gras!");
                }
                else
                {
                    Result.setText("Bravo! Tu est en bonne santée!");
                }
            }
            else if (Kid.isChecked())
            {

                if(list.get((int) ((Age * 2) - 1)) > Weight.intValue())
            }

        }
    });
}
}
The Cool Man
  • 5
  • 1
  • 3

2 Answers2

2

tl;dr

Declare the type of your list’s content with <Double>.

List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;

Use generics

As khelwood commented, see: What is a raw type and why shouldn't we use it?.

Use the generics feature of Java, where you specify the data type to be held by your List collection. Without specifying the type to be held, < Double >, the list has no idea of the type of a retrieved element.

Without generics, when you call list.get( … ) the compiler sees only an Object object rather than a Double object. Your code > Weight.intValue() is asking to compare an Object object to a int primitive which makes no sense because an Object object is not a number.

Instead of this:

List list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);
        

…add < Double > to your List declaration, like this:

List < Double > list = Arrays.asList(15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25);

You have just told the compiler that this particular List object is a list of Double objects. Code that puts a DayOfWeek, Dog, or Invoice object into that list will not compile. Conversely, when retrieving an element from the list, the compiler knows for sure the retrieved element is the correct expected type, a Double. So no casting needed.

And, by the way, we now have a more convenient literals syntax using List.of. The result is a non-modifiable list.

And generally best to use more descriptive names on your variables, such as inputDoubles rather than list.

List < Double > inputDoubles = List.of( 15,19,14.5,16.5,14,16,13.75,17.5,13.5,17.5,13,17.5,13,18,13,18.5,13.25,19,13.5,20,13.75,20.5,14,21,14.5,22,15,22.5,15.5,23,16,24,16.5,24.5,17,25 ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

you use not generic list.

 List list = Arrays.asList(15,19, ... , 25)

If the list is not generic, it contains Objects.(the most common class in java) You store ints and doubles together so you need to generic it by their parent

List<? extends Number> list = Arrays.asList(15,19, ... , 25)

and then take its double value, while you can't cast double to int, but it's ok to cast int to double

if(list.get((int) ((Age * 2) - 1)).doubleValue() > Weight.intValue())
clueqva
  • 48
  • 5