stratofortress
stratofortress

Reputation: 453

Abstract class, Number, as my input in Java

I was trying to figure out what to do with the abstract class Number in the context of implementing a method that inputs an entity in such form.

Below is a short java script i wrote that shows my confusion.

In the main method, I haven't been able to figure out how to make my input more generic so that when calling upon whynowork , it can print out a message according to its data type (Double,int,Comparable)

public class PleaseWork{


    public static void main(String[] args) {

  //where i was desperately trying to figure out how to input a number       
        int x= Integer.parseInt(args[0]);
        float a = Float.parseFloat(args[0]);
        whynowork(3);

    }

// this tells you what data type your input is
    public static void whynowork(Number param) {

         if( param instanceof Double) {
            System.out.println("param is a Double");
        }
        else if( param instanceof Integer) {
            System.out.println("param is an Integer");
        }

        if( param instanceof Comparable) {
            System.out.println("param is comparable"); 
        }       

    }

}

Upvotes: 3

Views: 319

Answers (1)

Luqasso
Luqasso

Reputation: 56

You could simplify it to : System.out.println("patam is a "+param.getClass().getSimpleName());

Upvotes: 1

Related Questions