简体   繁体   English

从文本文件将输入存储到数组中

[英]Storing inputs into an array from text file

forgive me for my ignorance as I'm still learning. 原谅我仍在学习中的无知。 This is my first assignment with try blocks and catch statements, and I'm confused on how to store the values into an array and display them correctly. 这是我第一次使用try块和catch语句进行分配,并且我对如何将值存储到数组中并正确显示它们感到困惑。 According to my instructor's notes, my code looks correct but there's obviously something I'm missing since the console is only printing null. 根据我的讲师的说明,我的代码看起来正确,但由于控制台仅输出null,因此显然缺少了某些内容。 I'm assuming that somewhere the month files just aren't being stored in the array, but I'm not sure where they are supposed to be in my code. 我假设月份文件没有存储在数组中的某个地方,但是我不确定它们应该在我的代码中的什么位置。

String months;

String[][]Array1 = new String [4][3];

//fileReader object

try
{

    FileReader fr = new FileReader ("months.txt");
    BufferedReader inFile = new BufferedReader (fr);

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 3; j++) 
        {
            months = inFile.readLine();
            Array1[i][j] = months;
        }
        } 

        inFile.close();

    } 
    catch (FileNotFoundException exception) {

    }
    catch (IOException exception) {

    }


    for (int i = 0; i < 4; i++)
        {
        for (int j = 0; j < 3; j++) 
        {
            System.out.println(Array1[i][j]+ " ");
        }
        System.out.println();

The text file I'm trying to use is months and just has the months each separated by a new line. 我要使用的文本文件是几个月,每个月都用新行分隔。

EDIT: My text file and console output: here 编辑:我的文本文件和控制台输出: 在这里

I put your code in my IDE, and I ran it, and i got 我将您的代码放在IDE中,然后运行它,我得到了

JAN 
FEB 
MAR 

APR 
MAY 
JUN 

JUL 
AUG 
SEP 

OCT 
NOV 
DEC 

Process finished with exit code 0

can you tell us, what it's the result that you are getting? 你能告诉我们,你得到的结果是什么? this is the content of mi file months.txt 这是mi文件months.txt的内容

这是mi文件months.txt的内容

Your project structure must be: 您的项目结构必须为:

结构体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM