Search code examples
javaandroidarraylistintdouble

(JAVA) How to convert a double from a list to an int


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())
            }

        }
    });
}
}

Solution

  • 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())