【问题标题】:Split parse text file in java在java中拆分解析文本文件
【发布时间】:2018-12-09 06:24:26
【问题描述】:

所以基本上我可以使用缓冲阅读器阅读文本文件。 之后我应该 SPLIT 和 PARSE 它,在这一点上,我面临一个问题。

输出是: 5 2 9 1 7 3 9 2 10 11 6 3
线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“F:\Gephi\number.txt” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580) 在 java.lang.Integer.parseInt(Integer.java:615) 在 javaapplication17.JavaApplication17.main(JavaApplication17.java:42) C:\Users\Asus\AppData\Local\NetBeans\Cache\8.2\executor-sn-ps\run.xml:53:Java 返回:1 构建失败(总时间:0 秒)

我的密码是

package javaapplication17;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;




public class JavaApplication17 {

    public static void main(String[] args) {

        String count = "F:\\Gephi\\number.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(count))) 
        {
            String line;
            while ((line = br.readLine()) != null)
            {
                System.out.println(line);
            }
        } 

        catch (IOException e) 
        {
            e.printStackTrace();

        }


        // What is the Problem in this part? //

        String[] abc = count.split("/t");
        int[] intArray = new int[abc.length];

        for (int i=0; i<abc.length; i++)

        {

        String numberAsString = abc[i];
        intArray[i] = Integer.parseInt(numberAsString);


        }

        System.out.println("Number of integers: " + intArray.length);

        System.out.println("The integers are:");
        for (int number : intArray)

        {

        System.out.println(number);

        }






    }
}

我的文本文件是这样的

5   6   7
5   2   9
1   7   3
9   2   10
11  6   3

【问题讨论】:

  • 试试Integer.parseInt(numberAsString.trim());
  • 你想怎么拆分count?这个String[] abc = count.split("/t");
  • 应该是这样while ((line = br.readLine()) != null) { line += line; }然后用String[] abc = line.split("/t");
  • @HadiJ 我试过了,先生,但还是卡住了:(
  • String numbers = "",line; try (BufferedReader br = new BufferedReader(new FileReader(count))) { while ((line = br.readLine()) != null) { numbers+=line; } } catch (IOException e) { }

标签: java file parsing split


【解决方案1】:

问题是count 是一个包含值"F:\\Gephi\\number.txt" 的字符串。这不会为您提供文件中的行数/列数。

String[] abc = count.split("/t");  // <------ This won't work

所以当你这样做时

int[] intArray = new int[abc.length];

您正在创建一个大小为 abc 的数组,该数组被 \t 分割,这是不正确的。


要解决您的问题,您可以将 try-catch 块更改为以下内容:

int countOfNum = 0;
try (BufferedReader br = new BufferedReader(new FileReader(count))) {
    String line;
    while ((line = br.readLine()) != null) {
        String[] currLine = line.split("\t");
        System.out.println(Arrays.toString(currLine));
        countOfNum = countOfNum + currLine.length;
    }
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println("Total count of numbers : " +countOfNum);
  • 在上述解决方案中,我们将每一行拆分为变量currLine
  • 接下来使用Arrays.toString() 打印出值
  • 现在使用变量countOfNum我们找到总数 文件中的元素。

【讨论】:

  • 啊,我明白了!那么可能的解决方案是什么?非常感谢您的编辑帮助! :)
  • 没有问题。另外,您在阅读文件后到底想做什么?您只想计算和显示整数吗?为什么这里专门需要一个数组?
  • 先读取,再计数,再拆分解析!基本上4件事
  • 很高兴为您提供帮助。请考虑接受/投票赞成答案。 What should I do when someone answers my question?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
相关资源
最近更新 更多