クラブ、部員数が書いてあるtxtファイル、部員名のtxtファイルを読み込むプログラムです
ファイルを読み込み、表示ができるのですが、ファイル通りに人数が読み込まれずおかしくなってしまうので教えてください
###該当のソースコード
package reidai;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.GridLayout;
import java.awt.Component;
import javax.swing.SwingConstants;
import javax.swing.ScrollPaneConstants;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
public class Rei4_09 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JPanel panelCenter;
private JTextField textField01;
private JTextField textField02;
private JTextField textField03;
private JLabel label01;
private JLabel label02;
private JScrollPane scrollPane;
private JList list;
private JList list1;
private JLabel label03;
private JButton button;
private JPanel panel4;
private JPanel panel3;
private JPanel panel2;
private JPanel panel1;
private JRadioButton radioButton01;
private JRadioButton radioButton02;
private JComboBox comboBox;
private JRadioButton aBunkabu;
private JRadioButton aUndoubu;
private final static int MAX=60;
private int buinsu[];
private int buinsu1[];
private String[] aseito=new String[MAX];
private String[] bseito=new String[MAX];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rei4_09 frame = new Rei4_09();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Rei4_09() {
setTitle("リストとファイル");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700,250);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
panelCenter = new JPanel();
contentPane.add(panelCenter, BorderLayout.CENTER);
panelCenter.setLayout(new GridLayout(2, 1, 0, 0));
panel1 = new JPanel();
panel1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
panelCenter.add(panel1);
panel1.setLayout(new GridLayout(6, 1, 0, 0));
label01 = new JLabel("クラブ名");
panel1.add(label01);
label01.setHorizontalAlignment(SwingConstants.LEFT);
textField01 = new JTextField();
panel1.add(textField01);
textField01.setColumns(10);
label02 = new JLabel("部員数");
label02.setHorizontalAlignment(SwingConstants.LEFT);
panel1.add(label02);
textField02 = new JTextField();
panel1.add(textField02);
textField02.setColumns(10);
label03 = new JLabel("クラブ名2");
panel1.add(label03);
label03.setHorizontalAlignment(SwingConstants.LEFT);
textField03 = new JTextField();
panel1.add(textField03);
textField03.setColumns(10);
panel2 = new JPanel();
FlowLayout fl_panel2 = (FlowLayout) panel2.getLayout();
fl_panel2.setAlignment(FlowLayout.RIGHT);
panelCenter.add(panel2);
radioButton01 = new JRadioButton("運動部");
radioButton01.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<String> tmpAL=new ArrayList<String>(MAX);
buinsu=new int[MAX];
int i=0;
File fl = new File("./undoubu.txt");
try{
BufferedReader br = new BufferedReader(new FileReader(fl));
String cItem;
while((cItem=br.readLine())!=null && i<MAX){
tmpAL.add(cItem);
if((cItem=br.readLine())==null)
break;
buinsu[i]=Integer.parseInt(cItem);
i++;
}
br.close();
}catch(IOException fe){
textField01.setText(fe.toString());
}
tmpAL.trimToSize();
list.setListData(tmpAL.toArray(new String[tmpAL.size()]));
}
});
panelCenter.add(radioButton01);
radioButton02 = new JRadioButton("文化部");
radioButton02.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<String> tmpAL=new ArrayList<String>(MAX);
buinsu1=new int[MAX];
int i=0;
File fl = new File("./bunkabu.txt");
try{
BufferedReader br = new BufferedReader(new FileReader(fl));
String aItem;
while((aItem=br.readLine())!=null && i<MAX){
tmpAL.add(aItem);
if((aItem=br.readLine())==null)
break;
buinsu1[i]=Integer.parseInt(aItem);
i++;
}
br.close();
}catch(IOException fe){
textField03.setText(fe.toString());
}
tmpAL.trimToSize();
list.setListData(tmpAL.toArray(new String[tmpAL.size()]));
String[] tmpS1=tmpAL.toArray(new String[tmpAL.size()]);
String suu1;
int suu2;
for(int j=0;j<i-1;j++){
for(int k=j+1;k<i;k++){
if(buinsu1[j]<buinsu1[k]){
suu2=buinsu1[j];
buinsu1[j]=buinsu1[k];
buinsu1[k]=suu2;
suu1=tmpS1[j];
tmpS1[j]=tmpS1[k];
tmpS1[k]=suu1;
}
}
}
}
});
panelCenter.add(radioButton02);
comboBox = new JComboBox();
panelCenter.add(comboBox, BorderLayout.NORTH);
aUndoubu = new JRadioButton("運動部の人");
aUndoubu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.removeAllItems();
int i=1;
while(aseito[i]!=null){
comboBox.addItem(aseito[i]);
i++;
}
}
});
aBunkabu = new JRadioButton("文化部の人");
aBunkabu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.removeAllItems();
int i=1;
while(bseito[i]!=null){
comboBox.addItem(bseito[i]);
i++;
}
}
});
File fl = new File("./aaa.txt");
try{
BufferedReader br = new BufferedReader(new FileReader(fl));
int i=1;
while((aseito[i]=br.readLine())!=null && i<MAX){
i++;
}
br.close();
}catch(IOException fe){
System.out.println(fe.getLocalizedMessage());
}
fl = new File("./bbb.txt");
try{
BufferedReader br = new BufferedReader(new FileReader(fl));
int i=1;
while((bseito[i]=br.readLine())!=null && i<MAX){
i++;
}
br.close();
}catch(IOException fe){
System.out.println(fe.getLocalizedMessage());
}
panelCenter.add(aBunkabu);
panelCenter.add(aUndoubu);
ButtonGroup bGroup=new ButtonGroup();
bGroup.add(radioButton01);
bGroup.add(radioButton02);
bGroup.add(aUndoubu);
bGroup.add(aBunkabu);
scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(120, 2));
contentPane.add(scrollPane, BorderLayout. WEST);
list = new JList();
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
textField01.setText((String)list.getSelectedValue());
textField02.setText(String.valueOf(buinsu[list.getSelectedIndex()]));
}
});
scrollPane.setViewportView(list);
}
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。