2017-05-01 13 views
1

EnterGroupメソッドを使用してすべての情報を入力するときに問題がありますが、groupSearchを使用して検索すると正しく名前が表示され、値。私はJOPtionPaneと何か関係があると考えていますが、私が入力したものを返さないのですが、これを修正する方法はわかりません。ここJOptionPaneが文字列を格納していません

package assignment7; 

import java.awt.FlowLayout; 
import java.util.ArrayList; 
import java.util.StringTokenizer; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


import javax.swing.*; 

public class homescreen extends JFrame { 
    private JLabel windowTitle; 
    private JTextField textBox; 
    private JButton button; 
    private JComboBox<String> comboBox; 
    private JRadioButton rbutton; 
    private JRadioButton rbutton2; 
    private ButtonGroup radioGroup; 
    private JPanel window; 


private ArrayList<person> people = new ArrayList<person>(); 
    private ArrayList<group> groups = new ArrayList<group>(); 
    private ArrayList<String> peopleNames = new ArrayList<String>(); 
    private ArrayList<String> groupNames = new ArrayList<String>(); 

public homescreen() { 
    super("Database"); 
    setLayout(new FlowLayout()); 

    windowTitle = new JLabel("Please enter a group or person"); 
    textBox = new JTextField("Enter name here"); 
    button = new JButton("enter"); 
    rbutton = new JRadioButton("Add", true); 
    rbutton2 = new JRadioButton("Search", false); 
    String[] choices = {"group", "person"}; 
    comboBox = new JComboBox<String>(choices); 
    window = new JPanel(); 

    add(windowTitle); add(textBox); 
    window.add(comboBox); 
    add(rbutton); 
    add(rbutton2); 
    radioGroup = new ButtonGroup(); 
    radioGroup.add(rbutton); 
    radioGroup.add(rbutton2); 
    add(button); 
    add(window); 

    //Handler h = new Handler(); 
    //ActionListener l = new ActionEvent(); 
    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if(rbutton.isSelected() && comboBox.getSelectedItem().equals(choices[0])) 
       enterGroup(textBox.getText()); 
      if(rbutton.isSelected() && comboBox.getSelectedItem().equals(choices[1])) 
       enterPerson(); 
      if(rbutton2.isSelected() && comboBox.getSelectedItem().equals(choices[0])) 
       groupSearch(textBox.getText()); 
      if(rbutton2.isSelected() && comboBox.getSelectedItem().equals(choices[1])) 
       personSearch(); 
     } 
    }); 
    //button.addActionListener(h); 
} 

public void groupSearch(String name) { 
    boolean found = false; 
    for(int i = 0; i < groupNames.size(); i++) { 
     if(name.equals(groupNames.get(i))) { 
      JOptionPane.showMessageDialog(null, "Name: " + groupNames.get(i) + "\nYears Established: " + groups.get(i).yearsEstablished + "\nState: " + groups.get(i).state + "\nCity: " + groups.get(i).city + "\nMembers: " + groups.get(i).members, "Group Found",JOptionPane.INFORMATION_MESSAGE); 
      i = groupNames.size(); 
     } 
    } 
    if(found == false) 
     JOptionPane.showMessageDialog(null, "Group Not Found", "Not In Database",JOptionPane.INFORMATION_MESSAGE); 
} 

public void personSearch() { 

} 

public void enterGroup(String name) { 
    //String name = JOptionPane.showInputDialog("Enter name of group"); 
    String yearsEstablished = null; 
    String state = null; 
    String city = null; 
    String members = null; 
    yearsEstablished = JOptionPane.showInputDialog("Enter number of years group has been established"); 

    if(yearsEstablished != null) { 
     state = JOptionPane.showInputDialog("Enter state group is located in"); 
      if(state != null) { 
       city = JOptionPane.showInputDialog("Enter city group is located in"); 
        if(city != null) { 
         members = JOptionPane.showInputDialog("Enter number of members in group"); 
      } 
     } 
    } 
    int years = 0; 
    int num = 0; 

    if(yearsEstablished != null) { 
     StringTokenizer st = new StringTokenizer(yearsEstablished); 
     years = Integer.parseInt(st.nextToken()); 
    } 
    if(members != null) { 
     StringTokenizer st2 = new StringTokenizer(members); 
     num = Integer.parseInt(st2.nextToken()); 
    } 
    if(name != null && yearsEstablished != null && state != null && city != null && members != null) { 
     groups.add(new group(name, years, state, city, num)); 
     groupNames.add(name); 
     JOptionPane.showMessageDialog(null, "group has been added to the database", "Complete", JOptionPane.INFORMATION_MESSAGE); 
    } 

} 

public void enterPerson() { 

} 
} 

は、グループクラスであるあなたがそれを必要とする場合

package assignment7; 

public class group { 
    public String name; 
    public int yearsEstablished; 
    public String state; 
    public String city; 
    public int members; 

public group(String name, int yearsEstablished, String state, String city, int members) { 
    name = this.name; 
    yearsEstablished = this.yearsEstablished; 
    state = this.state; 
    city = this.city; 
    members = this.members; 
} 

public String getName() { 
    return name; 
} 

public int getYears() { 
    return yearsEstablished; 
} 

public String getState() { 
    return state; 
} 

public String getCity() { 
    return city; 
} 

public int getMembers() { 
    return members; 
} 
} 

、ここでは、ランナー/ドライバのクラスであるあなたがそれを必要とする場合

package assignment7; 

import javax.swing.JFrame; 

public class runner { 
    public static void main(String[] args) { 
     homescreen screen = new homescreen(); 

    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    screen.setSize(500,500); 
    screen.setVisible(true); 
} 
} 
+0

GUIを削除していくつかのデータを作り、そこにエラーがあるかどうかを確認するためにロジックをテストしてください。 – MadProgrammer

+0

ようこそ!ツアーを行い、見て回り、[ヘルプセンター](https://stackoverflow.com/help)、特に[質問方法](https://stackoverflow.com/help)を読んでください。/how-to-ask)と[このサイトでは、最小、完全、および検証可能な例を作成する方法](https://stackoverflow.com/help/mcve)を参照してください。問題を解決するために努力してください。そうした特定の問題にぶつかった場合は、あなたの試み(関連するすべてのコードを含む)に質問を掲示し、何がうまくいかないと言って、これまでのところあなたの研究を説明してください。 – Teocci

答えて

3

この...

public group(String name, int yearsEstablished, String state, String city, int members) { 
    name = this.name; 
    yearsEstablished = this.yearsEstablished; 
    state = this.state; 
    city = this.city; 
    members = this.members; 
} 

が間違っています。あなたはパラメータにgroupクラスのインスタンスフィールドを割り当てている、割り当ては

public group(String name, int yearsEstablished, String state, String city, int members) { 
    this.name = name; 
    this.yearsEstablished = yearsEstablished; 
    this.state = state; 
    this.city = city; 
    this.members = members; 
} 

他の方法でラウンドする必要があります。また、Coding Conventions for the Java Languageを見て取りたいことがあり、それは、それが簡単にあなたが他の人々のコードを読むことになります他の人があなたのことを読むために

+0

ああ、私は愚か者だと感じて、あなたに感謝 – McLovinIt

+0

@McLovinItうん、コンピュータはあなたにそれを行います – MadProgrammer

関連する問題