I have a chicken and egg issue of sorts. I'm use to dynamic typing (python) so please be gentle on me if this is a basic thing.
I have a scanner, but I want to allow the user to enter either a string or an integer (1 or 'option1'). Because it's a user input, I don't know the resulting type (int or string). I need to get the user input from the scanner, assign it to a variable, and then pass that variable to an overloaded method.
The problem is that I need to declare the variable type. But, it can be either. How do I handle this?
EDIT
To clarify, I was thinking of doing something like this (Below is pseudo code):
public static float methodname(int choice){
//some logic here
}
public static float methodname(String choice){
//some logic here
}
Scanner input = new Scanner( System.in );
choice = input.nextLine();
System.out.println(methodname(choice));
The problem that i'm having is the declaration of 'choice'. What do i put for the type?
Stringand check if it´s only numeric or not afterwards. If it is parse it as anint."1"contains only digits so it could be parsed as an int. The string"option1"contains letters so you can't convert it to a string.