爪哇 - 从一个txt文件中读取号码,并将其写入到一个二维数组 [英] Java - Read numbers from a txt file and write them into a 2d array

查看:308
本文介绍了爪哇 - 从一个txt文件中读取号码,并将其写入到一个二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从一个txt文件中读取数据,并将它们放入一个二维数组。我的code这就是给我的问题是:

 扫描仪输入=新的扫描仪(新的FileReader(文件));    //保存顶点的数量
    vCount = input.nextInt();    //创建一个二维数组
    整数[] [] = adjList新的整数[vCount] [vCount];    串线;
    的String [] arrLn;    的for(int i = 0; I< vCount;我++){        行= input.nextLine(); //'线'的值永远不会改变
        arrLn = line.split();        对于(INT J = 0; J< vCount; J ++){
            adjList [I] [J] =的Integer.parseInt(arrLn [J]); //错误列表问题是在这里
        }
    }

样品txt文件是这样的:

  5
1 2 3 4
2 3
3 1 2
4 1 4
5 2 4

其中第一行是verteces的数量和行的其余都将被插入到该阵列中的数据。他们需要插入,这样的txt文件中的每一行停留在自己的行数组(即:数组元素排着1等于'1,2,3,4,2,3',而是'1, 2,3,4。

我不能为我的生活理解,为什么行变量不实际读取行。并即时得到了code没有错误,只是当我运行它。

收到错误:

 运行:
输入文件名
C:\\用户\\ YAZAN \\桌面\\ test.txt的
异常线程mainjava.lang.NumberFormatException:对于输入字符串:
在java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    在java.lang.Integer.parseInt(Integer.java:504)
在java.lang.Integer.parseInt(Integer.java:527)
在dbsearchs.Graph.runBFS(Graph.java:38)
在dbsearchs.Driver.main(Driver.java:24)
Java结果:1
BUILD SUCCESSFUL(总时间:26秒为单位)


解决方案

首先,你真的确定行的值永远不会改变?

其次,有可能因为在调用第一input.nextInt(),然后input.nextLine()是一个问题。试着用而不是执行input.nextLine()而不是nextInt(),并从该行得到你的电话号码。目前,您对input.nextLine(第一次调用)是最有可能给你​​什么留下的第一行 - 没有什么

第三,我相信当你运行程序,你会得到一个异常的ArrayIndexOutOfBounds一旦NumberFormatException的是固定的。在你的第二个循环,不循环,直到vCount但循环,直到arrLn.length来代替。

My goal is to read data from a txt file, and place them into a 2d array. my code thats giving me issues is:

    Scanner input = new Scanner(new FileReader(file));

    //save the number of vertex's
    vCount = input.nextInt();

    //create a 2d array
    Integer[][] adjList = new Integer[vCount][vCount];

    String line;
    String [] arrLn;

    for (int i = 0; i < vCount; i++) {

        line = input.nextLine(); //the value of 'line' never changes
        arrLn = line.split(" ");

        for (int j = 0; j < vCount; j++) {
            adjList[i][j] = Integer.parseInt(arrLn[j]); //errors list problem is here
        }
    }

sample txt file is this:

5
1 2 3 4
2 3
3 1 2
4 1 4
5 2 4

Where the first row is the number of verteces and the rest of the rows are the data to be inserted into the array. They need to be inserted so that each row in the txt file stays in its own row in the array (ie: elements in array row 1 cant equal '1,2,3,4,2,3', but rather '1,2,3,4'.

I cannot for the life of me understand why the line variable does not actually read the line. And Im getting no errors in the code, just when I run it.

Error recieved:

run:
Enter the File Name
C:\Users\YAZAN\Desktop\test.txt
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at dbsearchs.Graph.runBFS(Graph.java:38)
at dbsearchs.Driver.main(Driver.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 26 seconds)

解决方案

First of all, are you really sure that the value of line never changes?

Secondly, there might be a problem because you call first input.nextInt() and then input.nextLine(). Try instead with performing input.nextLine() instead of nextInt() and get your number from that line. Currently, your first call to input.nextLine() is most likely giving you what's left of the first line -- nothing.

Thirdly, I believe you will get an ArrayIndexOutOfBounds exception once the NumberFormatException is fixed when you run the program. In your second loop, don't loop until vCount but loop until arrLn.length instead.

这篇关于爪哇 - 从一个txt文件中读取号码,并将其写入到一个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆