I’m trying to make it so when a user inputs an option its not case sensitive and they don’t have to type the full option. I cant figure out how to do it.
package com.unspoken; import java.util.Scanner; import java.awt.*; public class Main { public static void main(String[] args) { String play = "Play a game"; String internet = "Explore the internet"; String calculator = "Use the calculator"; String quit = "Quit Untouched"; String pickedEvent = "Unpicked"; Scanner scanner = new Scanner(System.in); System.out.println("Hello, My name is Ghost. What's your name?"); String name = scanner.nextLine().trim(); System.out.println("Hello " + name + ". What would you like to do today?"); while (!pickedEvent.equals("Picked")) { System.out.println(play); System.out.println(internet); System.out.println(calculator); System.out.println(quit); pickedEvent = scanner.nextLine(); switch (pickedEvent) { case "Play a game": System.out.println("Okay " + name + ", Loading games."); pickedEvent = "Picked"; break; case "Explore the internet": System.out.println("Okay " + name + ", Loading the internet."); pickedEvent = "Picked"; break; case "Use the calculator": System.out.println("Okay " + name + ", Loading calculator."); pickedEvent = "Picked"; break; case "Quit Untouched": System.out.println("Are you sure you want to quit Untouched " + name + "?"); String quitAnswer = scanner.nextLine().trim(); if(quitAnswer.equalsIgnoreCase("Yes")){ System.out.println("Okay goodbye " + name + ", Have a nice day."); break; }else if(quitAnswer.equalsIgnoreCase("No")){ System.out.println("What would you like to do today " + name + "?"); continue; } } } } }
Advertisement
Answer
You can assign numbers to identify the tasks
String play = "1.Play a game"; String internet = "2. Explore the internet"; String calculator = "3. Use the calculator"; String quit = "4. Quit Untouched";
and ask user to enter number instead of typing in complete string
System.out.println("Hello " + name + ". What would you like to do today?, pick number");
and use the numbers in switch case instead of string, change pickedEvent to int
int pickedEvent = 0; while (pickedEvent != 4) { System.out.println(play); System.out.println(internet); System.out.println(calculator); System.out.println(quit); pickedEvent = Integer.parseInt(scanner.nextLine()); switch (pickedEvent) { case 1: System.out.println("Okay " + name + ", Loading games."); break; case 2: System.out.println("Okay " + name + ", Loading the internet."); break; case 3: System.out.println("Okay " + name + ", Loading calculator."); break; case 4: System.out.println("Are you sure you want to quit Untouched " + name + "?"); String quitAnswer = scanner.nextLine().trim(); if(quitAnswer.equalsIgnoreCase("Yes")){ System.out.println("Okay goodbye " + name + ", Have a nice day."); break; }else if(quitAnswer.equalsIgnoreCase("No")){ System.out.println("What would you like to do today " + name + "?"); continue; } } }