How to create a program that ask for your name and only says hello when your name is Alice or Bob | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to create a program that ask for your name and only says hello when your name is Alice or Bob

im using java

25th Nov 2017, 1:28 PM
eternity 1992
eternity 1992 - avatar
5 Answers
+ 3
That's right Shamima. Also you should use equalsIgnoreCase() just incase the user inputs one of the names in small casing. Should be something like: if (name.equalsIgnoreCase("Alice") || name.equalsIgnoreCase("Bob")) { System.out.println("Hello"); }
25th Nov 2017, 8:47 PM
Thato Mokhemisa
Thato Mokhemisa - avatar
+ 15
Use equals method to compare Strings instead of == if(name.equals("Alice") || name.equals("Bob")){ // do your stuff }
25th Nov 2017, 2:14 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 3
Scanner sc = new Scanner(System.in)! String name = sc.nextLine(); if(name == "Alice" || name == "Bob") { System.out.print("Hello"); } put that code into your public static void main don't forget to import java.util.Scanner;
25th Nov 2017, 2:00 PM
Momo Belia
Momo Belia - avatar
+ 2
#python3 name = input("What is your name?") print(' ') if name == 'Alice' or name == 'Bob': print('Hello ')
25th Nov 2017, 1:33 PM
Amir Galanty
Amir Galanty - avatar
+ 2
grab the user input via system.in, wrap it in an if statement like this if(input=="Alice"||input=="Bob"{ under greet message here } now you might want to make all letters lowercase in case input is like aLiCE and you still want to execute code
25th Nov 2017, 1:35 PM
Dmitrii
Dmitrii - avatar