2017-12-04 6 views
0

の配列を検索し、私は、ユーザーが学生データベースから(彼らは両方とも一意であると仮定)IDまたは 名のいずれかを使用して、特定の学生を検索して情報を表示することができますGUIプログラムを作成しようとしていますこんにちは名前、ID、GPAなど。オブジェクト

私は名前またはIDを使用して検索した場合と同様に、学生情報学生のデータベースファイルのサンプルのJLabel(学生情報)

GUIプログラムの下部に表示する必要があります

12345 Mark 3.6 
72305 Sam 2.8 
12945 Chris 3.1 
18325 John 2.5 
52341 Drake 2.7 
98475 Sara 3.8 
24536 Robin 3.1 
34765 Ted 3.6 
23845 Kelly 3.1 

名前またはIDで検索するときに、私はエラーを取得していますなぜ私は得ることができませんでしたか? また、検索後に生徒情報を表示するにはどうすればよいですか?

とはい私は、異なる検索方法にID

の名前と他のための1つを使用していますここに私のコードは、プログラムがこの

Sample of the GUI Program interface

のように見えます

import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.util.*; 
import javax.swing.*; 


public class GUIProgram extends JFrame implements ActionListener { 

JLabel name, id, stInfo; 
JButton clearBtn, searchName, searchID; 
JTextField nameTxt, idTxt, nameInfo, idInfo, gpaInfo; 
int i ; 
String n; 
public static void main(String[] args) { 
    new GUIProgram(); 
} 


public GUIProgram(){ 
    super("GUIProgram"); 
    setTitle("Student Database"); 
    setLayout(new FlowLayout()); 
    setSize(480, 200); 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

    JPanel searchPanel = new JPanel(new FlowLayout()); 
    name = new JLabel("Name: "); 
    nameTxt = new JTextField(10); 
    id = new JLabel("ID: "); 
    idTxt = new JTextField(10); 
    clearBtn = new JButton("Clear"); 
    clearBtn.addActionListener(this); 

    searchPanel.add(name); 
    searchPanel.add(nameTxt); 
    searchPanel.add(id); 
    searchPanel.add(idTxt); 
    searchPanel.add(clearBtn); 
    add(searchPanel,BorderLayout.NORTH); 

    JPanel centerPanel = new JPanel(new FlowLayout()); 
    searchName = new JButton("Search By Name"); 
    searchID = new JButton("Search By ID"); 

    centerPanel.add(searchName); 
    centerPanel.add(searchID); 
    add(centerPanel,BorderLayout.CENTER); 

    JPanel infoPanel = new JPanel(new FlowLayout()); 

    stInfo = new JLabel("Student Info: "); 
    nameInfo = new JTextField(10); 
    nameInfo.setEditable(false); 
    idInfo = new JTextField(10); 
    idInfo.setEditable(false); 
    gpaInfo = new JTextField(10); 
    gpaInfo.setEditable(false); 

    infoPanel.add(stInfo); 
    infoPanel.add(nameInfo); 
    infoPanel.add(idInfo); 
    infoPanel.add(gpaInfo); 

    add(infoPanel,BorderLayout.SOUTH); 

    searchID.addActionListener(this); 
    searchName.addActionListener(this); 

    setVisible(true); 

} 

public void actionPerformed(ActionEvent ae) { 
    Object source = ae.getSource(); 
    if (source == clearBtn); 
    nameTxt.setText(""); 
    idTxt.setText(""); 

    try{ 
     Scanner file = new Scanner(new File("StudentDB.txt")); 

     ArrayList<Integer> id = new ArrayList<Integer>(); 
     ArrayList<String> name = new ArrayList<String>(); 
     ArrayList<Double> gpa = new ArrayList<Double>(); 

     while(file.hasNext()){ 
      id.add(file.nextInt()); 
      name.add(file.next()); 
      gpa.add(file.nextDouble()); 
     } 

     Student [] stList = new Student[9]; 
     for(int i = 0; i < name.size(); i++){ 
      stList[i] = new Student(id.get(i), name.get(i), gpa.get(i)); 
     } 

     n = nameTxt.getText(); 
     i = Integer.parseInt(idTxt.getText()); 
     try { 

      if(source == searchName){ 
       linearSearch(stList, n); 

      } 
      else if(source == searchID){ 
       binarySearch(stList, i); 
      } 
     }catch(InputMismatchException e){ 
      JOptionPane.showMessageDialog(null, "Input Mismatch Exception"); 
     } 

    }catch(InputMismatchException | FileNotFoundException e){ 
     JOptionPane.showMessageDialog(null, "Input Mismatch Exception"); 
    } 


} 

/*static final int NONE = -1; // not a legal index 
static int linearSearch(int id, Student[] a) { 
for (int i = 0; i < a.length; i++) { 
    if (id == a[i]) 
return i; 
} 
return NONE; 
}*/ 

static final int NONE = -1; // not a legal index 
static int linearSearch(Student[] a, String name) { 
    for (int p = 0; p < a.length; p++) {  
     if (name.equals(a[p])) 

      return p; 
     } 
    return NONE; 
    } 


public static int binarySearch(Student[] a, int x) { 
    int low = 0; 
    int high = a.length - 1; 
    int mid; 

    while (low <= high) { 
     mid = (low + high)/2; 

     if (a[mid].compareTo(x) < 0) { 
      low = mid + 1; 
     } else if (a[mid].compareTo(x) > 0) { 
      high = mid - 1; 
     } else { 
      return mid; 
     } 
    } 

    return -1; 
} 


class Student implements Comparable{ 
    int iDNumber; 
    String name; 
    double gpa; 

    public Student(int iDNumber, String name, double gpa){ 
     this.iDNumber = iDNumber; 
     this.name = name; 
     this.gpa = gpa; 
    } 
    public int getiDNumber() { 
     return iDNumber; 
    } 
    public void setiDNumber(int newId) { 
     this.iDNumber = newId; 
    } 
    public void setGpa(double newGpa) { 
     this.gpa = newGpa; 
    } 
    public void setName(String newName) { 
     this.name = newName; 
    } 
    public String getName() { 
     return name; 
    } 
    public double getGPA() { 
     return gpa; 
    } 

    public String toString(){ 
     return iDNumber+"\t"+name+"\t"+gpa; 
    } 


    public int compareTo(Object o) { 
     if(o == null) { 
      throw new NullPointerException(); 
      } 
     else if(o.getClass()!= getClass()) { 
       throw new ClassCastException(); 
      } 
     else if(o.getClass()== getClass()) { 
       Student st = (Student) o; 
       return iDNumber - st.getiDNumber(); 
      } 
      else { 

       Student st = (Student) o; 
       return (int) (gpa - st.getGPA()); 
     } 
    } 



} // end of st 



class ComparatorId implements Comparator { 
    public int compare(Object o1, Object o2) { 
     if(o1 == null | o2 == null) 
      throw new NullPointerException(); 
     else { 
      if(!(o1 instanceof Student) | !(o2 instanceof Student)) 
       throw new ClassCastException(); 
      else { 
       Student st1 = (Student) o1; 
       Student st2 = (Student) o2; 
       return st1.getiDNumber() - st2.getiDNumber(); 
      } 
     } 
    } 
} // End of ComparatorId class 


class ComparatorGPA implements Comparator { 
    public int compare(Object o1, Object o2) { 

     if(o1 == null | o2 == null) 
      throw new NullPointerException(); 
     else { 
      if(!(o1 instanceof Student) | !(o2 instanceof Student)) 
       throw new ClassCastException(); 
      else { 
       Student st1 = (Student) o1; 
       Student st2 = (Student) o2; 
       return Double.compare(st1.getGPA(), st2.getGPA()); 

     // return ((Student) o1).getGPA().compareTo2((Student) o2).getGPA())); 
     // return (int)(((Student) o1).getGPA()).compareTo2((Double)o1.getGPA()); 
    } 
     } 
    } 
} // End of ComparatorGPA class 

class ComparatorName implements Comparator { 
    public int compare(Object o1, Object o2) { 
     return ((Student) o1).getName().compareTo(((Student) o2).getName()); 

    } 
}// End of ComparatorName class 
} 

です

+0

あなたはどのようなエラーを受けていますか? –

+0

IDで検索しようとするとエラーが発生します。 スレッド "AWT-EventQueue-0"の例外java.lang.NumberFormatException:入力文字列の場合: " \t at java.lang.NumberFormatException。 forInputString(不明なソース)java.lang.Integer.parseInt(不明なソース)java.lang.Integer.parseIntで \t(不明なソース)で \tはExercise2.actionPerformed(Exercise2.java:116)で \t – Ammar

+0

私はドン」私の検索方法が正しく機能しているかどうかを実際に知る – Ammar

答えて

2

あなたが書いたコードにいくつかの問題、主にちょうど小さなバグがあります。

この行では、idTxtボックスにあるものを取得してintに変換しようとしているため、エラーを表示する項目はi=Integer.parseInt(idTxt.getText());です。あなたはそこに数に入れていますがないと名前だけで検索するとき、それは「」空白または離れた場合これが正常に動作します。アクションソースがsearchIDと等しいかどうかをチェックする前に解析するので、空の文字列を解析しようとします。テキストフィールドに数値があることがわかったら解析を行うと、エラーは発生しません。 EG:

if(source == searchID){ 
    i=Integer.parseInt(idTxt.getText()); 
} 

また、あなたのLinearSearch方法であなたは、Stringオブジェクトは、学生のオブジェクトと等しい場合、あなたが見ているname.equals(a[p])行っています。代わりに、直接それらが等しいかどうかを確認するために2つの文字列を比較しますname.compareTo(a[p].getName())ような何かを行うことができます。

最終的に、バイナリ検索はソートされた配列に対してのみ機能するため、生徒用配列を最初にソートする場合に使用できるStudent IDのバイナリ検索を使用しています。 LinearSearchメソッドのオーバーロードされたバージョンを使用して、intを受け取り、Studentオブジェクトのidと比較します。

関連する問題