//Phonebook Project - ver3 import java.util.Scanner; class PhoneInfo { String name; private String phoneNumber; private String birthDay; private String birthYear; private int choice; public PhoneInfo(String name, String phoneNumber, String birthDay) { this.name = name; this.phoneNumber = phoneNumber; this.birthDay = birthDay; } public PhoneInfo(String name, String phoneNumber, String birthDay, String birthYear) { this(name, phoneNumber, birthDay); this.birthYear = birthYear; } public void showPhoneInfo() { System.out.println("name: " + name); System.out.println("phone no.: " + phoneNumber); System.out.println("birthday: " + birthDay); if(birthYear == null) System.out.println("birth year: no record"); else System.out.println("birth year: " + birthYear); System.out.println("-----------------------------"); } } class Manager //save, search, delete data { PhoneInfo[] pBook = new PhoneInfo[100]; Scanner input = new Scanner(System.in); public PhoneInfo inputData() { System.out.print("name: "); String name = input.nextLine(); System.out.print("phone no.: "); String phoneNumber = input.nextLine(); System.out.print("birthday: "); String birthDay = input.nextLine(); System.out.print("birth year: "); String birthYear = input.nextLine(); PhoneInfo pData = new PhoneInfo(name, phoneNumber, birthDay, birthYear); System.out.println("-----------------------------"); return pData; } public void saveData() { for(int i=0; i<100; i++) { if(pBook[i] == null) { pBook[i] = inputData(); //new entry break; } else { if(i == 99) System.out.println("database full"); else continue; } } } public void searchData() { System.out.print("name: "); //tried putting System.out.flush(); here as well... didn't work String name = input.nextLine(); // do not want to use input.next() if possible System.out.println("\nSearching data...\n"); for(int i=0; i<100; i++) { if(pBook[i] != null) { if(pBook[i].name.equals(name)) { pBook[i].showPhoneInfo(); break; } else continue; } else { System.out.println("no results found"); break; } } } public void deleteData() { int idx; System.out.print("name: "); String name = input.nextLine(); idx = getIndex(name); if(idx == -1) System.out.println("no matching name"); System.out.println("Are you sure you want to delete " + name + "?"); System.out.print("1. yes\n2. no\n> "); int choice = input.nextInt(); if(choice == 1) { for(int i=idx; i<99; i++) pBook[i] = pBook[i+1]; System.out.println(name + " deleted..."); System.out.println("-----------------------------"); } if(choice == 2) return; } public int getIndex(String name) { int index = -1; for(int i=0; i<100; i++) { if(pBook[i] != null) if(pBook[i].name.equals(name)) //error --> worked when I added if(pBook[i] != null). why?? what difference did it make? index = i; else continue; else continue; } return index; } } class PhoneBookVer3 { static Scanner input = new Scanner(System.in); public static void showMenu() { System.out.println("[MENU]"); System.out.println("choose..."); System.out.println("1. save data"); System.out.println("2. search data"); System.out.println("3. delete data"); System.out.println("4. exit program"); System.out.print("> "); } public static void main(String[] args) { PhoneBookVer3 pb = new PhoneBookVer3(); int choice; Manager mng = new Manager(); while(true) { pb.showMenu(); //choice = Integer.parseInt(input.nextLine()); // why this doesn't work either.. choice = input.nextInt(); //input.nextLine(); //why this doensn't consume the \n??? // if(input.hasNextInt()) //exception // { // while(!(choice>=1 && choice<=4)) // { // System.out.print("enter again\n> "); // choice = input.nextInt(); // } // } // else // { // input.next(); // } switch(choice) { case 1: System.out.println("-----------------------------"); mng.saveData(); break; case 2: System.out.println("-----------------------------"); mng.searchData(); break; case 3: System.out.println("-----------------------------"); mng.deleteData(); break; case 4: System.out.println("-----------------------------"); System.out.println("end of program"); return; } } } }
deleteData() { instead int choice = input.nextInt(); Try this int choice = Integer.parseInt(input.nextLine()); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)