Question: I am trying to make a menu application and I can't get it to print out exactly what I want. It is supposed to search
I am trying to make a menu application and I can't get it to print out exactly what I want. It is supposed to search and print out specific parts of another class but is is printing out the whole class. I have selection 4 working correctly and printing out the specific dates I want but the other options are just printing out everything instead of searching. What am I missing? Thank you
import java.util.Scanner;
public class CourseSearchApplication {
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); menu(); int choice = getChoice(keyboard,"Enter your choice: "); while(choice != 5){ process_choice(keyboard, choice); menu(); choice = getChoice(keyboard,"Enter your choice: "); } }
public static void process_choice(Scanner keyboard, int choice) { String value; keyboard.nextLine(); switch (choice) { case 1: System.out.println("Enter all or part of the course name to search for: "); value = keyboard.nextLine(); search_by_course_name(value); break; case 2: System.out.println("Enter all or part of the course number to search for:"); value = keyboard.nextLine(); search_by_course_number(value); break; case 3: System.out.println("Enter all or part of term to search for s -> Spring;" + "f -> Fall; r -> Summer:"); value = keyboard.nextLine(); search_by_course_term(value); break; case 4: System.out.println("enter year to search for:"); value = keyboard.nextLine(); search_by_course_year(value); break; case 5: System.out.print("goodbye!"); keyboard.close(); break; default: System.out.println("Invalid input!"); break; } } public static void search_by_course_year(String value){ boolean found = false; int year = Integer.parseInt(value); for(int i = 0; i < CourseSchedule.COURSE_ARRAY.length; i++){ if(CourseSchedule.COURSE_ARRAY[i].getYear() == year){ System.out.println(CourseSchedule.COURSE_ARRAY[i]); found = true; } } if(!found){ System.out.println("No courses were found."); } } public static void search_by_course_number(String value){ boolean found = false; String course_number = " "; for(int i = 0; i < CourseSchedule.COURSE_ARRAY.length; i++){ course_number = CourseSchedule.COURSE_ARRAY[i].getCourseNumber(); course_number =course_number.toLowerCase(); value = value.toLowerCase(); if (course_number.contains(value)); { System.out.println(CourseSchedule.COURSE_ARRAY[i]); found = true; } if(!found){ System.out.println("No courses were found."); } } } public static void search_by_course_term(String value){ boolean found = false; char term = value.charAt(0); for(int i = 0; i < CourseSchedule.COURSE_ARRAY.length; i++){ term = CourseSchedule.COURSE_ARRAY[i].getTerm(); if(CourseSchedule.COURSE_ARRAY.length == term);{ System.out.println(CourseSchedule.COURSE_ARRAY[i]); found = true; } } if(!found){ System.out.println("No courses were found."); } } public static void search_by_course_name(String value){ boolean found = false; String course_name = ""; for(int i = 0; i < CourseSchedule.COURSE_ARRAY.length; i++){ course_name = CourseSchedule.COURSE_ARRAY[i].getCourseName(); course_name = course_name.toLowerCase(); value = value.toLowerCase(); if(course_name.contains(course_name)){ System.out.println(CourseSchedule.COURSE_ARRAY[i]); found = true; } } if(!found){ System.out.println("No courses were found."); } } public static int getChoice(Scanner keyboard, String prompt){ System.out.print(prompt); return keyboard.nextInt(); } public static void menu(){ System.out.println("1. Search by Course Name " + "2. Search by Course Number " + "3. Search by Term " + "4. Search by Year " + "5. Exit"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
