[英]How do I read from a text file into a 2 dimensional array in Java
I am trying to read a text file filled with numbers into a 2 dimensional integer array as can be seen in the read button in my code. 我正在尝试将填充数字的文本文件读取到二维整数数组中,如在我的代码中的“读取”按钮中所见。 I do not know what the problem is. 我不知道问题是什么。 Is my logic wrong? 我的逻辑错了吗? This is my first time trying to read into an array from a file. 这是我第一次尝试从文件中读取数组。 I need to be able to read 6 lines containing 10 numbers into my array. 我需要能够将包含10个数字的6行读入数组。 The text file contains this; 文本文件包含此内容;
"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"
Code: 码:
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);
}
}
your filepath
is wrong 您的文件filepath
错误
/Downloads/Assignment4/in4.txt
try to use the full path of directory or absolute path. 尝试使用目录的完整路径或绝对路径。
sample: D://folder1//folder2//Downloads//Assignment4//in4.txt
样本: D://folder1//folder2//Downloads//Assignment4//in4.txt
in MAC : add the folder of the root user 在MAC中 :添加root用户的文件夹
File file = new File("//Users//Downloads//Assignment4//in4.txt");
If it didn't solve your problem: 如果它不能解决您的问题:
Run as
debugger
and usebreakpoint
to check where it crashed 作为debugger
运行,并使用breakpoint
检查崩溃的位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.