0

So I'm using reflection to construct a class that the user defines; I ask the user for the name of the class, and how many parameters the preferred constructor of the class has.

Then, the program finds the correct constructor to use. That all works fine, and here is the relevant part of my code:

Object[] params = new Object[numParams];

Class<?>[] paramTypes = ctor.getParameterTypes();

for (int i = 0; i < numParams; i++) {
    System.out.format("Enter a value of type %s.\n", paramTypes[i].getName());
    params[i] = sc.nextLine();
    paramTypes[i].cast(params[i]);
}

numParams is the amount of parameters in the specified constructor, ctor.

So, as you can see, I ask the user to input a variable of type that's specified by the type of the parameter in the constructor.

The type outputs fine (for instance, if the first argument of the constructor is an int, the program says, Enter a value of type int..

However, once a value is entered and the enter key is pressed, I usually get a class cast exception. My end goal is to use the array params to construct a new object of the specified class.

How would I appropriately cast the user input to the expected type?

2
  • Entered parameter by user is String type. So you cannot cast it directly because for instance you need int but user entered String. I think you should implement a helper method for converting user String to needed type. This is my opinion Commented Mar 11, 2015 at 22:00
  • If number of types is limited I can use a simple switch ( type ) than cast that to desired type then return it. Method signature should be like Object castHelper(String type,String val) then you can call castHelper in paramTypes[i].cast(castHelper(paramTypes[i].getName(),params[i])) Commented Mar 11, 2015 at 22:07

1 Answer 1

1

When you read from the input:

params[i] = sc.nextLine();

You put an Object which is a String (the return type of nextLine()) into params[i]. If what you expect from the input is an int, then just casting won't do it. Casting don't change the nature of a value, it just fool the compiler.

You need to parse the input to compute the right value in the right type. At some point you will have to test the different input types to create the right object type, e.g.

if (paramType[i] == Integer.class) {
   Integer parInt = Integer.valueOf(params[i]); // params[i] is a String
   params[i] = parInt; // params[i] is now an Integer
}
else if (paramType[i] == String.class) {
   // 
}
else if ...

Or you can test the type from the Scanner with hasNextInt() and read the right type with nextInt(). But anyway, you will must have some sort of if () else if stuff or switch case on the different input types.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.