Is it possible for users to call a method through user input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible for users to call a method through user input?

I am creating a note program for practice on classes, methods, objects, extending, and implementing. I have just started the program and looking to improve it. One of the main features of this program, is to allow users to enter a command and have the computer run the command. However the user will not be using integers to call the method. I want them to use strings. This is a problem because I am unsure of how to use the if or else statements in the case of, If the user enters "Command 1" then output "Your name is " + userFirstName. I may not be very clear here but you might be able to see what I am trying to do by looking at my code: https://code.sololearn.com/comYWVwhLKGV

21st Jul 2018, 7:44 PM
C0D3X
C0D3X - avatar
1 Answer
0
if(scanner.nextLine() == "Command 1") // call your method else // print some error msg If you want to check for more commands, then better use switch.
21st Jul 2018, 10:02 PM
Dev
Dev - avatar