java - ArrayList is not properly printing User's inputs (ArrayList of a Class) -


i've been tasked create practice database programming assignment.

it consists of asking user data of course such as: instructor name, section number, , room number.

then ask user information student: student's name, gpa, major, , hours.

after getting information course, must create arraylist of student class, , allow user input multiple students. basic prompt go such:

 please enter section number (1 15)  5  please enter instructor section  frankie  please enter room number.  a1230  please enter name. press enter quit  tyler  please enter major  frank  please enter grade point average  3.34  please enter hours  13  please enter name. press enter quit   5    frankie   a1230   tyler  13  3.34  frank 

the sentinel exit loop asking student information exits once user inputs enter name prompt, information printed below.

my problem when user tries add more 1 student arraylist. here's example of happens:

please enter section number (1 15) 4 please enter instructor section jimmie please enter room number. b1230 please enter name. press enter quit adam please enter major itsd please enter grade point average 3.45 please enter hours 5 please enter name. press enter quit tonny please enter major business please enter grade point average 3.64 please enter hours 13 please enter name. press enter quit  4   jimmie  b1230  tonny  13  3.64  business tonny  13  3.64  business 

the correct output should be:

tonny 13  3.64  business adam  5   3.45  itsd 

it seems when add student inputs arraylist, the previous values of inputs overwritten new values user enters next student. i'm thinking there's wrong tostring() method in student class. here 3 classes: have driver (tester), student (validates inputs student info), , course (validates inputs course info , returns tostring printed in driver main method).

driver class - holds main method, asks user input information prompts, adds inputs student information getadd method in course method

import java.util.scanner; public class driver  {       student stud = new student();     course cour2 = new course();       public static void main(string[] args)      {            driver driv = new driver();          driv.getcourseinfo();          boolean another;                 {         = driv.getstudentinfo();          }while(another);         system.out.println(driv.cour2.tostring());     }      public void getcourseinfo()     {         int sectionvalid = 0;         int instructorvalid = 0;         int roomvalid = 0;         scanner input = new scanner(system.in);           system.out.println("please enter section number (1 15)");         sectionvalid = cour2.setsectionnumber(input.nextint());         while(!(sectionvalid == 1))         {                     system.out.println("your input invalid. please enter section number (1 15)");                 sectionvalid = cour2.setsectionnumber(input.nextint());         }           input.nextline();          system.out.println("please enter instructor section");         instructorvalid = cour2.setinstructor(input.nextline());          while (instructorvalid == 0)         {             system.out.println("your input cannot blank. please enter instructor.");             instructorvalid = cour2.setinstructor(input.nextline());         }           system.out.println("please enter room number.");         roomvalid = cour2.setroom(input.nextline());          while (roomvalid == 0)         {                 system.out.println("the room number cannot blank. please enter room number.");                 roomvalid = cour2.setroom(input.nextline());         }     }        public boolean getstudentinfo()     {         boolean = true;         int namevalid = 0;         int majorvalid = 0;         int gradepointaveragevalid = 0;         int hoursvalid = 0;         scanner input = new scanner(system.in);           system.out.println("please enter name. press enter quit");         namevalid = stud.setname(input.nextline());          if (namevalid == 1)         {              = true;             system.out.println("please enter major");             majorvalid = stud.setmajor(input.nextline());               while (majorvalid == 0)             {                     system.out.println("the major cannot blank. please enter major");                     majorvalid = stud.setmajor(input.nextline());             }               system.out.println("please enter grade point average");             gradepointaveragevalid = stud.setgradepointaverage(input.nextdouble());              while (gradepointaveragevalid == 0)             {                 system.out.println("the grade point average has between 0.00 , 4.00. please enter grade point average");                 gradepointaveragevalid = stud.setgradepointaverage(input.nextdouble());             }               system.out.println("please enter hours");             hoursvalid = stud.sethours(input.nextint());              while (hoursvalid == 0)             {                     system.out.println("hours cannot greater 17. please enter hours");                     hoursvalid = stud.sethours(input.nextint());             }               cour2.addstudent(stud);         }         else         {             = false;         }           return another;      }     } 

the student class: holds validations inputs student information , has tostring() method returns sectionnumber, room, etc. variables. tostring() called in course class printed each instance of arraylist students. i believe area have problem (tostring())

        import java.util.arraylist;   public class course  {  private int sectionnumber;  private string instructor;  private string room;  private arraylist<student> students = new arraylist<student>();  student studs = new student();     public int getsectionnumber()  {   return sectionnumber;  }   public int setsectionnumber(int sectionnumber)  {   int sectionvalid = 0;    if (sectionnumber >= 1 && sectionnumber <= 15)   {    this.sectionnumber = sectionnumber;    sectionvalid = 1;   }   return sectionvalid;  }   public string getinstructor()  {   return instructor;  }   public int setinstructor(string instructor)  {   int instructorvalid = 0;    if (instructor.length() != 0)   {    this.instructor = instructor;    instructorvalid = 1;   }   return instructorvalid;  }   public string getroom()  {   return room;  }   public int setroom(string room)  {   int roomvalid = 0;    if (room.length() != 0)   {    this.room = room;    roomvalid = 1;   }   return roomvalid;  }       public void addstudent(student stud)     {      students.add(stud);     }        public string tostring()     {     string results = " ";         (student s  : students)     {      results += "\n" + s.tostring();     }      return this.sectionnumber + "\t" + this.instructor + "\t" + this.room + results;     }   } 

and finally, the course class: holds similar properties student class, contains validations user inputs course information, contains arraylist students variable (which causing me problems). tostring() method within class called driver class, so tostring() method 1 being printed user. believe correctly use forreach loop add student inputs string, , return string printed. believe might wrong too.. i'm not sure.

import java.util.arraylist;   public class course  {  private int sectionnumber;  private string instructor;  private string room;  private arraylist<student> students = new arraylist<student>();  student studs = new student();     public int getsectionnumber()  {   return sectionnumber;  }   public int setsectionnumber(int sectionnumber)  {   int sectionvalid = 0;    if (sectionnumber >= 1 && sectionnumber <= 15)   {    this.sectionnumber = sectionnumber;    sectionvalid = 1;   }   return sectionvalid;  }   public string getinstructor()  {   return instructor;  }   public int setinstructor(string instructor)  {   int instructorvalid = 0;    if (instructor.length() != 0)   {    this.instructor = instructor;    instructorvalid = 1;   }   return instructorvalid;  }   public string getroom()  {   return room;  }   public int setroom(string room)  {   int roomvalid = 0;    if (room.length() != 0)   {    this.room = room;    roomvalid = 1;   }   return roomvalid;  }       public void addstudent(student stud)     {      students.add(stud);     }        public string tostring()     {     string results = " ";         (student s  : students)     {      results += "\n" + s.tostring();     }      return this.sectionnumber + "\t" + this.instructor + "\t" + this.room + results;     }      } 

sorry overload of information, wanted present full scope of problem see happening everything. i'm still novice in java programming, i'm trying best learn. tips or advice appreciated! past due, grade has suffered, i'd learn i'm doing wrong.

thanks! please comment if have question regarding scope, tried explain problem , show sample output.

to summarize:

i don't understand why arraylist printing final student information entered, rather printing each individual student information (if makes sense).

please note: have validated program run , print out inputs, if try copy , paste own eclipse , there errors, might copy-paste error i've made own code, or other compiler specific jargon.

you haven't copied students class... solution is:

the problem not arraylist prints false. problem use objects wrong...

you should create new objects of student if create new student. , create new object of course. if change variables in 1 class override first input...

just add getcourseinfo: cour2 = new course(); , getstudentinfo: stud = new student();

code:

import java.util.scanner; public class driver  {   student stud = new student(); course cour2 = new course();   public static void main(string[] args)  {        driver driv = new driver();      driv.getcourseinfo();      boolean another;         {     = driv.getstudentinfo();      }while(another);     system.out.println(driv.cour2.tostring()); }  public void getcourseinfo() {     int sectionvalid = 0;     int instructorvalid = 0;     int roomvalid = 0;     cour2 = new course();     scanner input = new scanner(system.in);       system.out.println("please enter section number (1 15)");     sectionvalid = cour2.setsectionnumber(input.nextint());     while(!(sectionvalid == 1))     {                 system.out.println("your input invalid. please enter section number (1 15)");             sectionvalid = cour2.setsectionnumber(input.nextint());     }       input.nextline();      system.out.println("please enter instructor section");     instructorvalid = cour2.setinstructor(input.nextline());      while (instructorvalid == 0)     {         system.out.println("your input cannot blank. please enter instructor.");         instructorvalid = cour2.setinstructor(input.nextline());     }       system.out.println("please enter room number.");     roomvalid = cour2.setroom(input.nextline());      while (roomvalid == 0)     {             system.out.println("the room number cannot blank. please enter room number.");             roomvalid = cour2.setroom(input.nextline());     } }    public boolean getstudentinfo() {     boolean = true;     int namevalid = 0;     int majorvalid = 0;     int gradepointaveragevalid = 0;     int hoursvalid = 0;     stud = new student();     scanner input = new scanner(system.in);       system.out.println("please enter name. press enter quit");     namevalid = stud.setname(input.nextline());      if (namevalid == 1)     {          = true;         system.out.println("please enter major");         majorvalid = stud.setmajor(input.nextline());           while (majorvalid == 0)         {                 system.out.println("the major cannot blank. please enter major");                 majorvalid = stud.setmajor(input.nextline());         }           system.out.println("please enter grade point average");         gradepointaveragevalid = stud.setgradepointaverage(input.nextdouble());          while (gradepointaveragevalid == 0)         {             system.out.println("the grade point average has between 0.00 , 4.00. please enter grade point average");             gradepointaveragevalid = stud.setgradepointaverage(input.nextdouble());         }           system.out.println("please enter hours");         hoursvalid = stud.sethours(input.nextint());          while (hoursvalid == 0)         {                 system.out.println("hours cannot greater 17. please enter hours");                 hoursvalid = stud.sethours(input.nextint());         }           cour2.addstudent(stud);     }     else     {         = false;     }       return another;  } } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -