Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"); 
        }       

    }

}
like image 994
stratofortress Avatar asked Dec 31 '25 06:12

stratofortress


1 Answers

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

like image 128
Luqasso Avatar answered Jan 01 '26 18:01

Luqasso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!