如何从Java文本文件读取到二维数组

时间:2015-03-28 03:30:50

标签: java arrays file

我正在尝试将填充数字的文本文件读入二维整数数组,如我的代码中的读取按钮所示。我不知道问题是什么。我的逻辑错了吗?这是我第一次尝试从文件中读取数组。我需要能够在我的数组中读取包含10个数字的6行。文本文件包含此内容;

"1 1 3 3 0 0 1 1 0 3

1 1 1 0 0 3 3 3 3 0

1 0 0 0 1 1 3 0 0 3

1 3 1 3 3 3 0 0 1 1

3 0 1 3 1 1 1 1 1 1

0 3 1 0 3 0 0 3 3 0"

代码:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class GUI extends JFrame{

    private JPanel mainPanel,titlePanel, fieldPanel, buttonPanel;

    private JLabel title, teams, totalP, wlt;

    private JTextField team1, team2, team3, team4, team5, team6, total1, total2, total3, total4, total5, total6, wlt1, wlt2, wlt3, wlt4, wlt5, wlt6;

    private JButton read, calc, champWin, earthCW, exit;

    final private int WINDOW_HEIGHT = 400;
    final private int WINDOW_WIDTH = 900;

    public GUI(){

        buildtitlePanel();
        buildfieldPanel();
        buildbuttonPanel();
        buildmainPanel();

        setTitle("Desert Soccer League");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private void buildmainPanel() {

        mainPanel = new JPanel();

        mainPanel.setLayout(new BorderLayout());

        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(fieldPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

    }

    private void buildtitlePanel() {

        titlePanel = new JPanel();

        title = new JLabel();

        title.setText("2014 Desert Soccer League Totals");

        titlePanel.add(title);



    }

    private void buildfieldPanel() {

        fieldPanel = new JPanel();

        fieldPanel.setLayout(new GridLayout(7, 3));

        teams = new JLabel();
        teams.setText("Teams");

        totalP = new JLabel();
        totalP.setText("Total Points");

        wlt = new JLabel();
        wlt.setText("Win-Loss-Tie");

        team1 = new JTextField(10);
        team2 = new JTextField(10);
        team3 = new JTextField(10);
        team4 = new JTextField(10);
        team5 = new JTextField(10);
        team6 = new JTextField(10);

        total1 = new JTextField(10);
        total2 = new JTextField(10);
        total3 = new JTextField(10);
        total4 = new JTextField(10);
        total5 = new JTextField(10);
        total6 = new JTextField(10);

        wlt1 = new JTextField(10);
        wlt2 = new JTextField(10);
        wlt3 = new JTextField(10);
        wlt4 = new JTextField(10);
        wlt5 = new JTextField(10);
        wlt6 = new JTextField(10);

        team1.setEditable(false);
        team2.setEditable(false);
        team3.setEditable(false);
        team4.setEditable(false);
        team5.setEditable(false);
        team6.setEditable(false);

        total1.setEditable(false);
        total2.setEditable(false);
        total3.setEditable(false);
        total4.setEditable(false);
        total5.setEditable(false);
        total6.setEditable(false);

        wlt1.setEditable(false);
        wlt2.setEditable(false);
        wlt3.setEditable(false);
        wlt4.setEditable(false);
        wlt5.setEditable(false);
        wlt6.setEditable(false);

        fieldPanel.add(teams);
        fieldPanel.add(totalP);
        fieldPanel.add(wlt);

        fieldPanel.add(team1);
        fieldPanel.add(total1);
        fieldPanel.add(wlt1);

        fieldPanel.add(team2);
        fieldPanel.add(total2);
        fieldPanel.add(wlt2);

        fieldPanel.add(team3);
        fieldPanel.add(total3);
        fieldPanel.add(wlt3);

        fieldPanel.add(team4);
        fieldPanel.add(total4);
        fieldPanel.add(wlt4);

        fieldPanel.add(team5);
        fieldPanel.add(total5);
        fieldPanel.add(wlt5);

        fieldPanel.add(team6);
        fieldPanel.add(total6);
        fieldPanel.add(wlt6);

    }

    private void buildbuttonPanel() {

        buttonPanel = new JPanel();

        buttonPanel.setLayout(new GridLayout(1, 5));

        read = new JButton();
        calc = new JButton();
        champWin = new JButton();
        earthCW = new JButton();
        exit = new JButton();

        read.setText("Read Input File");
        read.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int[][] points = null;

                File file = new File("/Downloads/Assignment4/in4.txt");

                try {
                    Scanner sc = new Scanner(file);

                    while(sc.hasNextLine()){
                        for(int i = 0; i<6; i++){
                            for(int j = 0; j<10;j++){
                                points[i][j] = sc.nextInt();
                                System.out.print(points[i][j]);
                            }
                        }
                    }
                    sc.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }








            }
        });

        calc.setText("Calculate Points");
        calc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        champWin.setText("Championship Winner");
        champWin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        earthCW.setText("Earth Cup Winner");
        earthCW.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        exit.setText("Exit");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        buttonPanel.add(read);
        buttonPanel.add(calc);
        buttonPanel.add(champWin);
        buttonPanel.add(earthCW);
        buttonPanel.add(exit);



    }


}

1 个答案:

答案 0 :(得分:0)

您的filepath错误

/Downloads/Assignment4/in4.txt

尝试使用目录的完整路径或absolute path.

示例:D://folder1//folder2//Downloads//Assignment4//in4.txt

MAC 中的

:添加root用户的文件夹

File file = new File("//Users//Downloads//Assignment4//in4.txt");

如果它没有解决您的问题:

  

debugger运行并使用breakpoint检查崩溃的位置