2013-03-21 2 views
0

좋아, 메신저 학생 데이터베이스 시스템을 괴롭 히고 있지만 인터페이스 메뉴로 고생하고있다. 옵션 1을 위해 1을 누르면 아무 일도 일어나지 않고 인쇄와 같은 많은 다른 오류가 발생한다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?자바 메뉴 인터페이스 학생 시스템

는 RegistryInterface에 대한 Heres는 코드 :

package student; 

import java.util.Scanner; 

public class RegistryInterface 
{ 

    private Registry theRegistry = null; 
    Scanner input = new Scanner(System.in); 

    public RegistryInterface(Registry theRegistry) 
    { 
    } 

    public void doMenu() 
    { 
     String decission; 

     System.out.println("Registry Main Menu\n*******************\n"); 
     System.out.println("1. Add a Student \n2. Delete a Student" 
       + "\n3. Print Registry\n4. Quit"); 

     System.out.println("Select Option [1, 2, 3, 4] :>"); 
     decission = input.nextLine(); 

     while (decission != "4") 
     { 
      if ("1".equals(decission)) 
      { 
       doAddStudent(); 
      } 

      if ("2".equals(decission)) 
      { 
       doDeleteStudent(); 
      } 


      if ("3".equals(decission)) 
      { 
       doPrintRegistry(); 
       break; 
      } 

      if ("4".equals(decission)) 
      { 

       System.out.println(" System Closing Down........."); 
       break; 
      } 
     } 
    } 

    private void doAddStudent() 
    { 
     String addMore = null; 

     String foreName; 
     String surName; 
     int iDNumber; 

     while ("y".equals(addMore)) 
     { 
      System.out.println("Add New Student\n***********\n"); 

      System.out.println("Enter forename  :>"); 
      foreName = input.nextLine(); 
      System.out.println("\nEnter surname  :>"); 
      surName = input.nextLine(); 
      System.out.println("Enter ID for Student :>"); 
      iDNumber = input.nextInt(); 

      theRegistry.addStudent(new Student(surName, foreName, iDNumber)); 

      System.out.println("Add Another Student(Y/N) :>"); 
      addMore = input.nextLine(); 
     } 
    } 

    private void doDeleteStudent() 
    { 
     int studID; 
     String another = null; 

     System.out.println("Add New Student\n***********\n"); 

     while("y".equals(another)) 
     { 
      System.out.println("Enter Student ID To Delete :>"); 
      studID = input.nextInt(); 

      theRegistry.deleteStudent(studID); 

      System.out.println("\nDelete Another? (Y/N)"); 
      another = input.nextLine(); 
     } 
    } 

    private void doPrintRegistry() 
    { 
     System.out.println("Printing Registry\n***********\n"); 
     theRegistry.format(); 
    } 
} 

그리고 heres는 코드 registryApp에 대한 :

public class RegistryApp 
{ 

    public static void main(String[] args) 
    { 
     Registry theRegistry = new Registry(); 

     RegistryInterface aRegInterface = new RegistryInterface(theRegistry); 

     aRegInterface.doMenu(); 

    } 
} 

그리고 내 레지스터 클래스 :

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()+ " Is Already Used"); 
       return; 
      } 
     } 
     System.out.println("Student "+ aStudent.getForeName() + " " 
       + "" + aStudent.getForeName() +" " 
       + "Successfully Added To System.....\n"); 

     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() + " Was Succesffully Removed from 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(); 
    } 



} 

답변

0

이 루프 내부 ...

while (decission != "4") 

... 새 값을 decission으로 설정하지 않았습니다. 따라서 루프는 무한히 실행되거나 아무 것도 수행하지 않습니다.

또한 입력에서 스캔 한 문자열은 항상 새로 생성 된 문자열이므로 "4"를 입력해도 항상! = "4"가됩니다.

+0

죄송합니다. 루프와 어떻게 관련이 있습니까? –

+0

루프는 조건이 거짓이 될 때까지 매번 조건을 확인하면서 반복해서 본문을 실행합니다. 루프 내부에서 조건을 거짓으로 만들 수없는 경우 "무한 루프"가 발생합니다. –