boliviab
boliviab

Reputation: 83

convert arraylist data from object / float to int to use in a method

I have an arraylist in a class that I store number in. The method in the class that adds data to the arraylist takes one float parameter - this is because some of the data is float and some is integer.

I need to extract some of the data with a method and convert it to an Int but cant work out how to. Ive tried various was of casting etc but because it is objects in the arraylist I cant get it working.

(So if this isnt sounding too clear , ive been at this for 8 hours straight so brain is a bit fried now)

For example , the arraylist is called subjectsResults and Ive tried:

numberOfA = (Integer)subjectsResults.get(1);

where numberOfA is an Int (i know where the index is of the data also - that is why I reference 1 directly)

Ive tried using toString and parseInt and things like this:

numberOfA = (Integer.parseInt((String)subjectsResults.get(1)));

How can I achieve this - i need to extract the number from index(1) to use in a switch/case statement to show a menu choice

Upvotes: 1

Views: 1086

Answers (2)

Karan Ashar
Karan Ashar

Reputation: 1400

Similar solution to slartidan except I don't like type checking so will be using generics instead.


 public class FloatAndInt {
    private static <T extends Number> void addItemToArray(final T item,final List numberList) {
        numberList.add(item);
    }

private static void iterateArrayList(final List<Number> numberList) {
    for (Number n:numberList) {
       System.out.println(n.intValue());

    }
 }
 public static void main(String args[]) {
    List<Number> numberList = new ArrayList<Number>();
    addItemToArray(2,numberList);
    addItemToArray(4.3f,numberList);
    //addItemToArray(Long.MAX_VALUE, numberList);
    //addItemToArray(Double.MAX_VALUE, numberList);
    iterateArrayList(numberList);
  }

}

Output:


2
4

Upvotes: 0

slartidan
slartidan

Reputation: 21608

There are several ways of solving this, one straight forward approach would be this:

Object value = subjectsResults.get(1); 
if (value instanceof Integer) {
  numberOfA = (Integer) value;
} else if (value instanceof Float) {
  numberOfA = ((Float) value).intValue();
} else {
  handle this unusual case
}

The even cleverer solution is to use the common superclass Number. Both Integer and Float are subclasses of Number - and they therefore share the method intValue().

import java.util.ArrayList;
import java.util.List;

public class Snippet {

    public static void main(String[] args) {

        // define test data
        List<Number> subjectsResults = new ArrayList<>();
        subjectsResults.add((int) 50);
        subjectsResults.add((float) 33.333f);

        // create target list
        List<Integer> subjectsNumbers = new ArrayList<>();

        // iterate over input
        for (Number number : subjectsResults) {

            // use method "intValue", which is available for all "Number"-objects
            subjectsNumbers.add(number.intValue());
        }

        System.out.println(subjectsNumbers);
    }

}

The output in console:

[50, 33]

Upvotes: 2

Related Questions