I'm a beginner to Java, the code is for a homework assignment regarding files and loops. When I to run this code to check for any errors, when hitting a number, it doesn't go forward past the menu. However, if I hit 5, it then start's with the choice that's meant for 1. I'm not exactly sure what's happening that's resulting in the error.
I did try to switch the code to a switch method instead of a while loop, but that resulted in just the code not moving forward regardless of what option was selected. I also tried moving scanners around but that didn't also effect it.
The following is the code for reference
Import java.util.Scanner;
import java.io.*;
public class Homework3A {
public static void main(String[] args) throws IOException
{
//Homework3A
//Lance Torres, completed on DATE
Scanner userInput = new Scanner(System.in);
System.out.println("1. Add student\n"
+ "2. Add course \n"
+ "3. Display student\n"
+ "4. Display Course.\n"
+ "5. Exit"); //the message that will display to start the menu
int menuChoice = userInput.nextInt(); //changing the scanner type into a int type for the menu loop
//will be using a while loop for this since it is a loop i am more familiar with when typing menus
PrintWriter StudentOPFile = new PrintWriter("Students.txt");
PrintWriter CoursesOPFile = new PrintWriter("Courses.txt");
while (menuChoice != 5); //this loop will keep happening until the user hits the int 5
{
if (menuChoice == 1); //for adding a student
{
Scanner studentInput = new Scanner(System.in);
System.out.println("Please enter the new Student ID. ");
System.out.println("Student ID: ");
int studentID = studentInput.nextInt();
//the system message and the string that will collect the user ID
System.out.println("Please enter the Student's first name: ");
String firstName = studentInput.nextLine();
//code above is for when the user enters the first name of the student
System.out.println("Please enter the Student's last name: ");
String lastName = studentInput.nextLine(); //for the last name of the student
System.out.println("Please enter the address: ");
String address = studentInput.nextLine();
//for the address
System.out.println("Please enter the city: ");
String city = studentInput.nextLine();
//for the city
System.out.println("Please enter the state");
String state = studentInput.nextLine();
//for the state
FileWriter StudentWriter = new FileWriter("Students.txt", true); //adding a file writer in order to append information to the file
PrintWriter outputFile = new PrintWriter(StudentWriter);
outputFile.println(studentID + firstName + lastName + address + city + state); //adds all the information as a line
outputFile.close(); //closes the file
System.out.println("Student information added successfully."); //system display message when the system finishes writing it to file
}
}
if (menuChoice == 2);
{
Scanner courseInput = new Scanner(System.in) ;
System.out.println("Please enter the course ID: ");
int courseID = courseInput.nextInt();
//for when the user adds the courseID
System.out.println("Please enter the course name: ");
String courseName = courseInput.nextLine();
//when the user adds the course's name
System.out.println("Please enter a decription for the course: ");
String courseDescrption = courseInput.nextLine();
//when the user adds the descrption, decided to make this a string as well since it could be a long sentence as opposed to a single world
FileWriter CourseWriter = new FileWriter("Courses.txt", true);
PrintWriter outputFile = new PrintWriter(CourseWriter);
outputFile.println(courseID + courseName + courseDescrption);
outputFile.close();
System.out.println("Course information added successfully.");
}
if (menuChoice == 3);
{
File studentFile = new File("Students.txt");
Scanner inputFileusingStudentID = new Scanner(studentFile);
while (inputFileusingStudentID.hasNext())
{
String studentInformation = inputFileusingStudentID.nextLine();
System.out.println(studentInformation);
}
inputFileusingStudentID.close();
}
if (menuChoice == 4);
{
File coursesFile = new File("Courses.txt");
Scanner inputFileusingCourseID = new Scanner(coursesFile);
while (inputFileusingCourseID.hasNext())
{
String courseInformation = inputFileusingCourseID.nextLine();
System.out.println(courseInformation);
}
inputFileusingCourseID.close();
} System.out.println("Thank you for using the program. Now Exiting...");
System.out.println("Goodbye.");
}
}