+ 2

Overriding a variable

Can I somehow override a string variable, for example, to int variable? Something like that String a = "5"; if (true) { @Override int a = Integer.valueOf(a); } if (a instanceof Integer) System.out.print(" override done"); --- Basically I need to make clear whether user's input is a string or integer and use it afterwards.

28th Apr 2017, 1:59 PM
Alex Snaidars
Alex Snaidars - avatar
1 Réponse
+ 1
No, this is not a way how overriding works in Java. But you can use Scanner class to parse the input. Scanner sc = new Scanner(System.in); if (sc.hasNextInt()) { doSomething(); } else { doSomethingElse(); }
28th Apr 2017, 2:16 PM
Jeth
Jeth - avatar