0

So the while loop is not working, even though it compiles.  How do I position it correctly??

I tried a few things but they kinda worked. if someone on here can help me out that would be great

import java.util.Scanner;
//This is a Driver program to test the external Class named Student
public class StudentDriver //BEGIN Class Definition
{
   //****************  Main Method*************************
   public static void main (String[] args)
   {Scanner scan = new Scanner(System.in);
   //Data Definitions:
   //Instance Data
   String courseName;
   int courseCredits;

   String name;
   String id;
   String street;
   String city;
   String state;
   String zip;
   String major;

   //Executable Statements:
   //Initialize first Student
   name = "Fred Fergel";
   id = "0123";
   street = "123 Main Street";
   city = "Smalltown";
   state = "NY";
   zip = "12345";
   major = "Computer Science";
   //instantiate the Student object
   Student student1 = new Student(name, id, street, city, state, zip, major);
   //Test toString
   System.out.println("Student 1\n\n" + student1.toString());
   //Print a blank line
   System.out.println();
   //Add a course
   student1.addCourse("CSC111", 4);//NOTE:  DO NOT PUT A SPACE BETWEEN CSC AND 111
   //Print schedule
   System.out.println("Student 1's Schedule:\n\n");
   student1.displaySchedule();//call method

    final String FLAG = "Y";
     String prompt = "Y";
   while (prompt.equals("y"))
   {
   System.out.println("Please enter the name of the course: ");
   courseName = scan.next();
   System.out.println("How many credits is the course? ");
   courseCredits = scan.nextInt();
   student1.addCourse(courseName, courseCredits);
   System.out.println("Do you wish to enter another course? y/n");
   prompt = scan.next();
   }
   //end while


   }//end main
}//end StudentDriver

Here is the student class:

import java.util.Scanner;
public class Student
{Scanner scan = new Scanner(System.in);
   //Instance Data
   String studentName;
   String studentID;
   String streetAddress;
   String city;
   String state;
   String zipCode;
   String major;
   int  totalCredits;
   final int SIZE = 6;
   final int MAX_CREDITS = 18;
   String [ ] schedule = new String [SIZE];
   int courseNumber = 0; //start out with no courses
   //Create Constructor:
   //Initializes the student data at instantiation time.
   //-------------------------------------------------------
   //  Sets up the student's information.
   //-------------------------------------------------------
   public Student (String name, String id, String address, String cityName, String stateName, String zip, String area )
   {
   studentName = name;
   studentID = id;
   streetAddress = address;
   city = cityName;
   state = stateName;
   zipCode = zip;
   major = area;
   }//end Student Constructor
   //Method to Return student information as string:
   //-------------------------------------------------------
   //  Returns the student information as a formatted string.
   //-------------------------------------------------------
   public String toString()
   {
   String studentInfo;
   studentInfo = "Name:\t\t\t" + studentName + "\n" + "ID:\t\t\t" + studentID + "\n" + "Address:\t\t" + streetAddress
   + "\n" + "City:\t\t\t" + city + "\n" + "State:\t\t\t" + state + "\n" + "Zip Code:\t\t" + zipCode
   + "\n" + "Major:\t\t\t" + major + "\n";
   return studentInfo;
}// end toString
   //Method to determine if maximum allowed credits have been exceeded
   //-------------------------------------------------------
   //  Returns true if total credits does not exceed 18.
   //-------------------------------------------------------
   private boolean checkCredits(int numCredits)
   {
   if (numCredits + totalCredits <= MAX_CREDITS)  //make sure max credits not exceeded
   {
   return true;  //return a true if still less than 18 credits
                     }
   else
   {
   return false;  //return a false if 18 credit limit is exceeded
   }//end numCredits
   }//checkCredits
   //Method to add a course to the student’s schedule
   //-------------------------------------------------------
   //  Adds a course to the array if total credits does not exceed 18.
   //-------------------------------------------------------
   public void addCourse(String course, int numCredits)
   {
   if (courseNumber <  SIZE )  //make sure array is not full.  
   {
   if (checkCredits(numCredits) == true) //if we’re under 18 credits
   {
   //add course
   schedule [courseNumber] = course + ":\t\t" + numCredits + "\tCredits\n";
   //increment number of credits
   totalCredits = totalCredits + numCredits;
   //increment number of courses
   courseNumber = courseNumber + 1;
   }
   else  //oops – can’t do more than 18 credits
   {
   System.out.println("You have exceeded the maximum allowed credits.");
   }//end checkCredits
   }
   else  //oops – can’t do more than 10 courses
   {
   System.out.println("You have exceeded 10 courses.");
   }//end courseNumber
   }//addCourse
   //Method to display the schedule
   //-------------------------------------------------------
   //  Will only print out the courses added to the array.
   //-------------------------------------------------------
   public void displaySchedule( )
   {
   for (int index = 0; index < courseNumber; index++)
   {
   System.out.println("Course #" + (index + 1) + " " + schedule[index] + "\n");
   }//end for
   }//end display schedule

}
user156327
  • 1
  • 4
  • 32
  • 56
R.KillA
  • 17
  • 1

1 Answers1

3
String prompt = "Y";
while (prompt.equals("y"))

Y and y are not the same thing. You need to use .equalsIgnoreCase() instead of .equals() if you want it to ignore case.

D M
  • 1,392
  • 1
  • 6
  • 12