gpt4 book ai didi

java - 读取制表符分隔的文件并忽略空格

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:09 27 4
gpt4 key购买 nike

我正在开发一个简单的项目,其中将制表符分隔的文本文件读入程序中。

我的问题:读取文本文件时,通常会出现空数据空间。数据的缺乏导致了意外的输出。对于 token[4] 位置没有数据的行,所有读取的数据都将被忽略,并且当我运行 System.out.println 时显示“4”(只是测试数据是否正确读取)。当我在 token[4] 位置合并一个值时,数据读取良好。我在 token[4] 位置输入一个值是 Not Acceptable 。请参阅下面的文件和代码。

2014    Employee    Edward Rodrigo  6500
2014 Salesman Patricia Capola 5600 5000000
2014 Executive Suzy Allen 10000 55
2015 Executive James McHale 12500 49
2015 Employee Bernie Johnson 5500
2014 Salesman David Branch 6700 2000000
2015 Salesman Jonathan Stein 4600 300000
2014 Executive Michael Largo 17000 50
2015 Employee Kevin Bolden 9200
2015 Employee Thomas Sullivan 6250

我的代码是:

// Imports are here
import java.io.*;
import java.util.*;

public class EmployeeData {

public static void main(String[] args) throws IOException {
// Initialize variables
String FILE = "employees.txt"; // Constant for file name to be read
ArrayList<Employee> emp2014; // Array list for 2014 employees
ArrayList<Employee> emp2015; // Array list for 2015 employees
Scanner scan;

// Try statement for error handling
try {
scan = new Scanner(new BufferedReader(new FileReader(FILE)));
emp2014 = new ArrayList();
emp2015 = new ArrayList();

// While loop to read FILE
while (scan.hasNextLine()) {
String l = scan.nextLine();
String[] token = l.split("\t");
try {
String year = token[0];
String type = token[1];
String name = token[2];
String monthly = token[3];
String bonus = token[4];
System.out.println(year + " " + type + " " + name + " " + monthly + " " + bonus);
} catch (Exception a) {
System.out.println(a.getMessage());
}
}
} catch(Exception b) {
System.out.println(b.getMessage());
}
}

}

我收到的包含“Employee”的行的输出以意外的方式返回。

输出:

run:
4
2014 Salesman Patricia Capola 5600 5000000
2014 Executive Suzy Allen 10000 55
2015 Executive James McHale 12500 49
4
2014 Salesman David Branch 6700 2000000
2015 Salesman Jonathan Stein 4600 300000
2014 Executive Michael Largo 17000 50
4
4
BUILD SUCCESSFUL (total time: 0 seconds)

我尝试使用 if-then 来测试 token[4] 位置中的空值,但这并没有真正帮助我。我做了很多搜索但没有成功。

我对编程世界还很陌生,所以请原谅我编码效率低下。非常感谢任何有助于提高我的技能的支持和一般反馈!

谢谢你,布莱恩

最佳答案

Java Devil 认为根本问题是由于 ArrayOutOfBoundsException 造成的,这是正确的。但也值得探索为什么你没有看到这一点。正如我们在评论中讨论的,您的“尝试错误处理语句”实际上根本不处理您的错误,而是 suppressing them ,这通常是一个糟糕的计划,因为它允许您的程序继续运行,即使您的假设(它正常工作)被违反。

这是代码的稍微清理过的版本。导致 ArrayOutOfBoundsException 的根本问题仍然存在,但如果您以这种方式构建代码,问题就会立即显现出来。有一些评论指出了内嵌问题。

public class EmployeeData {
// constants should be declared static and final, and not inside main
private static final String FILE = "employees.txt";

// If you have an exception and you don't know how to handle it the best thing
// to do is throw it higher and let the caller of your method decide what to do.
// If there's *nothing* you want to do with an exception allow main() to throw
// it as you do here; your program will crash, but that's a good thing!
public static void main(String[] args) throws IOException {
// Notice the <> after ArrayList - without it you're defining a "raw type"
// which is bad - https://stackoverflow.com/q/2770321/113632
ArrayList<Employee> emp2014 = new ArrayList<>();
ArrayList<Employee> emp2015 = new ArrayList<>();

// A try-with-resources block automatically closes the file once you exit the block
// https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
try (Scanner scan = new Scanner(new BufferedReader(new FileReader(FILE)))) {
while (scan.hasNextLine()) {
String l = scan.nextLine();
String[] token = l.split("\t");
// The code below this line assumes that token has at least five indicies;
// since that isn't always true you need to handle that edge case before
// accessing the array indicies directly.
String year = token[0];
String type = token[1];
String name = token[2];
String monthly = token[3];
String bonus = token[4];
System.out.println(year + " " + type + " " + name + " " + monthly + " " + bonus);
}
}
}
}

关于java - 读取制表符分隔的文件并忽略空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com