java - Menu Interface Student Record System -
i have partially working record system, i'm having trouble menu interface. basically, menu should allow to:
- add student
- delete student
- print students
- quit
now menu works fine i'm having trouble add student section. when add student require:
- a prompt first name
- a prompt surname
- a prompt id number
when have done this, asks if want add another; if "y" entered, loops again, , if "n" entered goes main menu.
now on mine when press "n" nothing happens, , when press y, loops again won't let enter values first name.
here's code system:
registryinterface.java
/** * @version 1.0 * @author parry2411 */ package student; import java.util.scanner; public class registryinterface { private registry theregistry = new registry(); scanner input = new scanner(system.in); public registryinterface(registry theregistry) { } public void domenu() { boolean done = false; while (!done) { try { system.out.println("\nregistry main menu\n*******************\n"); system.out.println("1. add student \n2. delete student" + "\n3. print registry\n0. quit"); system.out.println("select option [1, 2, 3, 0] :>"); string userdecission = input.nextline(); int decission = integer.parseint(userdecission); switch (decission) { case 0: system.out.println("system closing down.."); break; case 1: doaddstudent(); break; case 2: dodeletestudent(); break; case 3: doprintregistry(); break; default: system.out.println("\nplease enter in valid" + " menu option"); domenu(); break; } done = true; }catch(exception e) { system.out.println("incorrect value entered, try again...."); } } } private void doaddstudent() { string addmore; { system.out.println("\nadd new student\n***********\n"); try { system.out.println("enter students forename :>"); string fname = input.nextline(); system.out.println("enter student surname :>"); string sname = input.nextline(); system.out.println("enter student id number :>"); int idnum = input.nextint(); theregistry.addstudent(new student(fname, sname, idnum)); } catch (exception e) { system.out.println("\nerror occured: incorect value entered" + "\ntry again... \nstudent not added"); } system.out.println("\nadd student (y/n) : >"); addmore = input.next(); } while (!"n".equals(addmore)); } private void dodeletestudent() { string another; { system.out.println("\ndelete student\n***********\n"); system.out.println("enter student id delete :>"); try { int studid = input.nextint(); theregistry.deletestudent(studid); } catch (exception e) { system.out.println("\nerror occured: incorect value entered" + "\ntry again...\n"); } system.out.println("\ndelete another? (y/n)"); = input.next(); } while (!"n".equals(another)); } private void doprintregistry() { system.out.println("\nprinting registry\n***********\n"); if(theregistry.studentlist.size() == 0) { system.out.println("the student record system contains no student" + " records, please add students\n\n"); } else { system.out.println(theregistry.format()); system.out.println("\n****printing finished........"); } domenu(); } }
registry.java
/** * @version 1.0 * @author parry2411 */ package student; import java.util.iterator; import java.util.linkedlist; public class registry { public linkedlist<student> studentlist = new linkedlist<>(); public iterator<student> iter = studentlist.iterator(); public registry() { } public void addstudent(student astudent) { iterator<student> additerator = studentlist.iterator(); while (additerator.hasnext()) { student ob = additerator.next(); if (ob.getstudentid() == astudent.getstudentid()) { system.out.println("this student id " + ""+ astudent.getstudentid()+ " used" + "\n try adding again......"); return; } } system.out.println("student "+ astudent.getforename() + " " + "" + astudent.getsurname() +" " + "successfully added system....."); studentlist.addlast(astudent); } public void deletestudent(int studentid) { iterator<student> deleteiterator = studentlist.iterator(); boolean removed = false; while(deleteiterator.hasnext()) { student ob = deleteiterator.next(); if(ob.getstudentid() == studentid) { deleteiterator.remove(); removed = true; system.out.println(ob.getforename() + " " + ob.getsurname() + " succesffully removed system. \n"); } } if(!removed) { system.out.println("student id not found"); } } public string format() { stringbuilder sb = new stringbuilder(); iterator<student> formatiterator = studentlist.iterator(); while(formatiterator.hasnext()) { student ob = formatiterator.next(); sb.append(ob.format()); } return sb.tostring(); } @override public string tostring() { iterator<student> tostringiterator = studentlist.iterator(); stringbuilder sb = new stringbuilder(); while(tostringiterator.hasnext()) { student ob = tostringiterator.next(); sb.append(ob.tostring()).append("\n"); } return sb.tostring(); } }
registry app
package student; public class registryapp { public static void main(string[] args) { registry theregistry = new registry(); registryinterface areginterface = new registryinterface(theregistry); areginterface.domenu(); } }
student.java
/** * @version 1.0 * @author parry2411 * date created: 18-mar-2013 */ package student; public class student { private string forename; private string surname; private int studentid; public student(string forename, string surname, int studentid) { this.forename = forename; this.surname = surname; this.studentid = studentid; } public string getforename() { return forename; } public void setforename(string forename) { this.forename = forename; } public string getsurname() { return surname; } public void setsurname(string surname) { this.surname = surname; } public int getstudentid() { return studentid; } public void setstudentid(int studentid) { this.studentid = studentid; } @override public string tostring() { return getclass().getsimplename()+"{" + "forename=" + forename + ", surname=" + surname + ", studentid=" + studentid + '}'; } public string format() { return string.format("%-5s %-5s \t\t %-5d \n",forename,surname,studentid); } }
thanks
change doaddstudent
method way:
private void doaddstudent() { string addmore; { system.out.println("\nadd new student\n***********\n"); try { system.out.println("enter students forename :>"); string fname = input.next();//use next() instead of nextline() system.out.println("enter student surname :>"); string sname = input.next();//use next() instead of nextline(); system.out.println("enter student id number :>"); int idnum = input.nextint(); theregistry.addstudent(new student(fname, sname, idnum)); } catch (exception e) { system.out.println("\nerror occured: incorect value entered" + "\ntry again... \nstudent not added"); } system.out.println("\nadd student (y/n) : >"); addmore = input.next(); } while (!"n".equals(addmore)); }
you getting error because of scanner.nextline()
using .now question why?
scanner.nextline()
described in official document is:
advances scanner past current line , returns input skipped. method returns rest of current line, excluding line separator @ end. position set beginning of next line.
scanner.next()
doesn't terminates allocated line of memory. when nextline()
called via string fname = input.nextline();
after addmore = input.next();
after user inputs y
terminating previous line had value in -- entered via next()
rather taking in new string
value.that's why skipping entry user . , in order carry on reading entered value rather previous blank line (because of non-termination of value returned next()
) can use scanner.next()
according official document states that:
finds , returns next complete token scanner.
update
should replace :
string userdecission = input.nextline();
to
string userdecission = input.next();
Comments
Post a Comment