Compe
Compe

Reputation: 55

How to Check the Value Associated to String Object in ArrayList?

I have an ArrayList with multiple objects of different types. At one index I have an element with a String value. I know the index location and I know the string is associated with either a integer or double value.

How do I check if the value that object is associated with is a double or an integer?

EDIT: It's not that I'm trying to compare 5.5 to 5, when I try to check the data type of either object, I get a compiler error stating I can't parse an object.

My arraylist is comprised of two String objects. What I'm doing is parsing the String object rather than the String value associated with the object.

EDIT2: Correction!!! The objects are in fact NOT String objects. Instead, they are just objects with no specific class associated with them. Without declaring an object type in your arraylist, the arraylist will automatically assign every element to an object value (rather than one object being a String, one object being a double, one object being an int, etc.)

~~~

Example of my code:

import java.util.ArrayList;
public class idk {

  public static void main(String[] args) {
    ArrayList myList1 = new ArrayList();
    ArrayList myList2 = new ArrayList();

    myList1.add("5.5");
    myList2.add("5");

    //How does one check if the value associated to the String object 
    //in the arrayList (like myList.get(1)) is a double or an integer?
  }
}

I've only tried implementing an isDouble method but its checking if the object is a double rather than the value of the string object.

import java.util.ArrayList;

public class idk {

    //I've attempted to try using this method to check the value but I get
    //a compiler error saying I can't use the isDouble method which checks
    //for a String value, not the value of the actual String object in the arraylist
    boolean isDouble(String str) {
      try {
        Double.parseDouble(str);
          return true;
      }
      catch (NumberFormatException e) {
        return false;
      }
    }


    public static void main(String[] args) {
      ArrayList myList1 = new ArrayList();
      ArrayList myList2 = new ArrayList();

      myList1.add("5.5");
      myList2.add("5");

      if (isDouble(myList1.get(0))==true) {
        System.out.println("The object in pos 0 is a double");
      }else {
        System.out.println("The object in pos 0 is not a double");
      }

      if (isDouble(myList1.get(0))==true) {
        System.out.println("The object in pos 0 is a double");
      }else {
        System.out.println("The object in pos 0 is not a double");
      }

    }

Upvotes: 0

Views: 197

Answers (3)

Rahmat Waisi
Rahmat Waisi

Reputation: 1330

I've attempted to try using this method to check the value but I get
a compiler error saying I can't use the isDouble method which checks
for a String value, not the value of the actual String object in the arraylist

simply convert an object that returns from myList1.get(0) to string , then call isDouble method,

if (isDouble(myList1.get(0).toString()) == true) {

or change your isDouble method argument from String to Object ,so it will be

boolean isDouble(Object obj) {
   try {
       Double.parseDouble(obj); 
       /* 
         obj must be converted to string first and then you can call 
         parseDouble method on it , so you will get an error like this
         error: incompatible types: Object cannot be converted to String
       */
       return true;
   } catch (NumberFormatException e) {
       return false;
   }
}

then try one of this ways to fix the error that i said.

  • 1.use Casting benefits , call parseDouble like below code

    Double.parseDouble((String) obj );
    
  • 2.use obj.toString()

    Double.parseDouble(obj.toString());
    

note : this method always returns true for doubles and also integers
because each integer is a double ,so this method can't be useful
use isInteger method instead, as sbgreene1307 answered.

all things you have to change is :

  • change name from isDouble to isInteger
  • change Double.parseDouble(str); to Integer.parseInt(str);

Upvotes: 0

Stephen Greene
Stephen Greene

Reputation: 69

Be sure to think about what the difference between an integer and a double are. Can all doubles be integers or can all integers be doubles? (One is correct)

If you can answer that question then you will know which way your isDouble method should test first and then second.

Instead test if it is an integer first, because not all doubles are integers.

boolean isInteger(String str) { try{ Integer.valueOf(str); //You should be able to use parseInteger also return true; } catch (NumberFormatException e){ return false; } }

Then you still need to make sure the string is a double and not something else. Your method should suffice here.

Upvotes: 1

rothloup
rothloup

Reputation: 1290

What I would do is try to parse the string as both an Integer and as a Double, then check if the resulting values are equal.

"5" as a double is just 5.0, and as an Integer is 5. They are equal, so your string is an integer.

"5.2" as a double is 5.2, and as an Integer is 5. They are not equal, so your string is not an integer - and by construction of your question, must thus be a double.

EDIT: I just tried it out, and if you pass "5.2" to Integer.parseInt(), it will throw an exception NumberFormatException. So if your string throws an exception, it has a decimal point and represents a double. Else it is an Integer.

Upvotes: 2

Related Questions