This program is a contact book. There is a contact class and contact book class. When I execute case 2, the add method from the contact book class does not let me enter name and phone number individually. "Enter a name" and "Enter an address" appear at the same time.
Here is the switch case in the main method:
while (!done)
{
ContactBook cb = new ContactBook();
System.out.println("1) List all contacts" + "\n" + "2) Add a contact"
+ "\n" + "3) Update a contact" + "\n" + "4) Remove a contact" +
"\n" + "5) Quit");
userChoice = sc.nextInt();
switch (userChoice)
{
case 1:
c.toString();
break;
case 2:
cb.add(sc);
break;
case 3:
cb.update(sc);
break;
case 4:
cb.remove(sc);
}
if (userChoice == 5);
{
done = true;
}
}
}
Here is the add method from the contact book class:
public void add (Scanner sc)
{
Contact c = new Contact(name, address, phone, email);
System.out.println("Enter a name: ");
c.setName(sc.nextLine());
System.out.println("Enter an address: ");
c.setAddress(sc.nextLine());
System.out.println("Enter a phone number: ");
c.setPhone(sc.nextLine());
System.out.println("Enter an email: ");
c.setEmail(sc.nextLine());
entries = Arrays.copyOf(entries, entries.length + 1);
}
You need to change the following:
userChoice = sc.nextInt();
to
userChoice = Integer.valueOf(sc.nextLine());
I think this should fix your problem
System.out.print("Enter a name: ");
c.setName(sc.nextLine());
System.out.println()
Related
//Choice of choosing a dog or a cat
String pet = input.next();
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
}
case 'b' -> {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
}
default -> System.out.println("That is not a valid option. Please choose again.");
}
input.close();
}
I can't find a loop to use that would bring it back to case a and repeatedly until the user answers using one of the choices, Any help would be awesome! Thanks
Using do while loop is a simple way to solve it.
I used a variable repeat to check, If I need to ask for the input again or not
Also, note that now even If I add another case(say case 'c') I don't need to modify condition for my do while loop
boolean repeat;
do {
String pet = input.next();
repeat = false;
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
}
case 'b'-> {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
}
default: System.out.println("That is not a valid option. Please choose again.");
repeat = true;
}
} while(repeat);
input.close();
Looping is good solution but you can also use the Recessive methods in java.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome To The Choices Game... ");
System.out.println("Please enter your name: " );
String playerName = input.nextLine();
System.out.println("What is " + playerName + "'s" + " favorite pet?");
System.out.println("a. Dog \nb. Cat");
//Choice of choosing a dog or a cat
method();
input.close();
}
public void method()
{
String pet = input.next();
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
}
case 'b' -> {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
}
default -> System.out.println("That is not a valid option. Please choose again.");
method();
}
}
To loop the entire switch-case section, you can try putting it in a do-while loop. As for the condition to use with the do-while block you could try:-
do {...}while(pet.charAt(0)!='a' || pet.charAt(0)!='b');
I am providing you with a possible solution below:-
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome To The Choices Game... ");
System.out.println("Please enter your name: " );
String playerName = input.nextLine();
System.out.println("What is " + playerName + "'s" + " favorite pet?");
System.out.println("a. Dog \nb. Cat");
//Choice of choosing a dog or a cat
do{
String pet = input.next();
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
break;}
case 'b' -> {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
break;}
default -> System.out.println("That is not a valid option. Please choose again.");
}
}while(pet.charAt(0)!='a' && pet.charAt(0)!='b');
input.close();
}
I can't find a loop to use that would bring it back to the beginning of the switch case and repeatedly until the user answers using one of the choices, Any help would be awesome! Thanks. (I have also tried using a do-while loop that someone suggested but it just seems to spam the default.) ((which I left in the code))
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome To The Choices Game... ");
System.out.println("Please enter your name: ");
String playerName = input.nextLine();
System.out.println("What is " + playerName + "'s" + " favorite pet?");
System.out.println("a. Dog \nb. Cat");
//Choice of choosing a dog or a cat
String pet = input.next();
do {
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
break;
}
case 'b' -> {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
break;
}
default -> System.out.println("That is not a valid option. Please choose again.");
}
} while (pet.charAt(0) != 'a' && pet.charAt(0) != 'b');
input.close();
}
You need to take the input for Cat/Dog (option a or option b) inside the do loop so that after wrong input code can ask for updated input. As below:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome To The Choices Game... ");
System.out.println("Please enter your name: ");
String playerName = input.nextLine();
System.out.println("What is " + playerName + "'s" + " favorite pet?");
System.out.println("a. Dog \nb. Cat");
// Here is change in code
String pet = null;
do {
// Choice of choosing a dog or a cat
// Here is change in code
pet = input.next();
switch (pet.charAt(0)) {
case 'a': {
System.out.println("What is your dog's name? ");
String dogsName = input.next();
System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
break;
}
case 'b': {
System.out.println("What is your cat's name? ");
String catsName = input.next();
System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
break;
}
default: {
System.out.println("That is not a valid option. Please choose again.");
}
}
} while (pet.charAt(0) != 'a' && pet.charAt(0) != 'b');
input.close();
}
I am writing a menu in java using a switch statement and while loop. I am looking to find a way of ensuring the user completes menu one before proceeding to menu two.
Here is an example piece of code:
(Please note I normally pass data using setters and getters, this is just a quick program I wrote for this question)
package menuOrder;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option = 0;
String Fname = null;
String Sname = null;
int Age = 0;
while(option !=5){
System.out.println("1. Enter Firstname");
System.out.println("2. Enter Surname");
System.out.println("3. Enter Age");
System.out.println("4. Display Details");
System.out.println("5. System Exit");
option = sc.nextInt();
switch(option){
case 1:
System.out.println("Please enter your Firstname >");
Fname = sc.next();
break;
case 2:
System.out.println("Please enter your Surname >");
Sname = sc.next();
break;
case 3:
System.out.println("Please enter your Age >");
Age = sc.nextInt();
break;
case 4:
System.out.println("Firstname = " + Fname + "\nSurname = " + Sname + "\nAge = " + Age);
break;
case 5:
System.out.println("You have chosen to System Exit!!!");
System.exit(0);
break;
default:
System.out.println("Invalid Entry!! \nPlease try again");
}
}
}
}
I am trying to prevent the use from entering their Surname before their Firstname.
Can someone please help?
Thanks
First show only the first name. Then when the user enters first name call surname method from that method.Use method calls instead of switch case.
I'm trying to write a program in which the user will first be shown a menu from which they can select a number of options. I have tried to create a code which, once an option has been selected will return the user to the menu. However, the program simply ends rather than looping the user back to the menu. What do I need to add to the code so that once the selection methods have been run through it will return the user to the selection menu?
Here's an excerpt showing the relevant part of the program:
Scanner scanner = new Scanner(System.in);
int selection = 0;
while (x == 0) {
System.out.println("Please make a selection");
System.out.println("[1] Make a new booking");
System.out.println("[2] View films");
System.out.println("[3] View Screens");
System.out.println("[4] View Customers");
System.out.println("[5] View showing information");
System.out.println("[6] View booking information");
System.out.println("[7] Exit");
while (selection == 0) {
System.out.println("Selection: ");
try {
selection = scanner.nextInt();
} catch (InputMismatchException nfe) {
System.out.println("Input must be a number");
selection = 0;
scanner = null;
scanner = new Scanner(System.in);
}
}
switch (selection) {
case 1:
System.out.println("All available showings:");
x = 1;
for (Showing aShowing : showingList) {
System.out.println(aShowing.toString());
}
System.out.println("Enter the showing number: ");
int show = scanner.nextInt();
Showing userChoice = showingList.get(show - 1);
System.out.println("Enter the number of tickets: ");
int tickets = scanner.nextInt();
Booking booking1 = new Booking(customerA, userChoice, tickets);
System.out.println(booking1);
break;
case 2:
System.out.println("Current films being shown:");
x = 1;
System.out.println("Film one: " + filmA.getTitle()
+ ".\nLength: " + filmA.getDuration() + " minutes."
+ "\nAge Rating: " + filmA.getAgeRating()
+ ".\nDescription: " + filmA.getDescription() + ".");
break;
case 3:
System.out.println("Screen information:");
x = 1;
System.out.println("Screen id:" + screenA.getId()
+ ".\nSeats a maximum of " + screenA.getCapacity() + " people.");
break;
I don't understand what while(x==0) is doing there.
What I have understood from your requirement is, you want to offer user a menu with options and user needs to enter his choice and a operation will be performed according to that and then again ask for another input from user.
If this is the case, then try writing your logic the following way,
do{
System.out.println("Please make a selection");
System.out.println("[1] Make a new booking");
System.out.println("[2] View films");
System.out.println("[3] View Screens");
System.out.println("[4] View Customers");
System.out.println("[5] View showing information");
System.out.println("[6] View booking information");
System.out.println("[7] Exit");
selection = scanner.nextInt();
switch(selection){
case 1:
...
//so on
case 7:
//do nothing
break;
default:
System.out.println("Wrong number entered");
}
}while(selection!=7);
So I have an addItem() method where I can add a book, tape, or cd object to my inventory. To store the book, tape, or cd object, I obtain the title, author, and price of the object from the user. HOWEVER, when I run the program, it skips the "please enter the title: " and goes right to "please enter the author:". Then when I have it show the information, the title field is 'null'.
static void addItem(int type) {
String title;
String author;
Double price;
System.out.println("Please enter the title");
title = keyboard.nextLine()
System.out.println("Please enter the author");
author = keyboard.nextLine();
System.out.println("Please enter the price");
price = keyboard.nextDouble();
if(type == 0){
Book BookStoreItem;
BookStoreItem = new Book(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
if(type == 1){
Tape BookStoreItem;
BookStoreItem = new Tape(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
if(type == 2){
CD BookStoreItem;
BookStoreItem = new CD(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
}//end of addItem()
Here is a sample output:
Press:
0 To add a book to the inventory
1 To add a tape to the inventory
2 To add a Cd to the inventory
Please enter your choice: 0
Please enter the title:
Please enter the author: John Smith
Please enter the price: 55.50
Title: null
Author: John Smith
Price: 55.5
Here's an update with the menu on it:
static void printMenu() {
System.out.println("\nPress:");
System.out.println("\t" + ADD_BOOK + "\tTo add a book to the inventory\n");
System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the inventory\n");
System.out.println("\t" + ADD_CD + "\tTo add a CD to the inventory\n");
System.out.println("\t" + SHOW_ITEM_LIST + "\tTo display a list of available books\n");
System.out.println("\t" + SEARCH_ITEM + "\tTo search for a specific book\n");
System.out.println("\t" + QUIT + "\tTo exit\n");
}
and with the choices:
static void runBookStoreApplication() {
int userChoice = QUIT;
String quitMessage = "\nThank you for using the BookStoreApplication!";
String invalidOptionMessage = "\nInvalid option!";
do{
printMenu();
userChoice = getUserChoice();
switch (userChoice) {
case ADD_BOOK:
addItem(0);
break;
case ADD_TAPE:
addItem(1);
case ADD_CD:
addItem(2);
case SHOW_ITEM_LIST:
showItemList();
break;
case SEARCH_ITEM:
searchItem();
break;
case QUIT:
System.out.println(quitMessage);
break;
default:
System.out.println(invalidOptionMessage);
}
} while(userChoice != QUIT);
}//end of runBookStoreApplication
with getUserChoice:
static int getUserChoice() {
int choice;
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();
return choice;
}
You didn't show us the code where you ask "Please enter your choice:", but I bet that you are using keyboard.nextInt() or something. This method does not consume all the line, just the integer 0, so when you call nextLine() afterwards it actually returns immediately with the rest of the line (that is, with an empty string).
To solve this, call keyboard.nextLine() before your method.
Edit:
now that you have put your getUserChoice() method, you can see that there is a call to keyboard.nextInt(), as I presumed. Just call nextLine() after it so you consume all the line and the scanner is ready for your next input:
static int getUserChoice() {
int choice;
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt(); // consume just the integer
// consume the rest of the line
keyboard.nextLine();
return choice;
}