通过使用空格作为分隔符的文件进行解析-java。(我不了解它)
有人能帮我理解我做错了什么吗? 我必须通读一个文件,每行有11个整数和双精度,每行需要成为它自己的对象并存储在arrayList中。但是,分隔符是单个空格。我使用过这个代码,但它似乎不起作用,我也不确定我做错了什么通过使用空格作为分隔符的文件进行解析-java。(我不了解它),java,oop,parsing,arraylist,delimiter,Java,Oop,Parsing,Arraylist,Delimiter,有人能帮我理解我做错了什么吗? 我必须通读一个文件,每行有11个整数和双精度,每行需要成为它自己的对象并存储在arrayList中。但是,分隔符是单个空格。我使用过这个代码,但它似乎不起作用,我也不确定我做错了什么 package p2_0000000; import java.util.Scanner; import java.io.*; import java.util.ArrayList; import java.util.Arrays; public class P2_000000
package p2_0000000;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class P2_000000 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Which file year would you like to analyze:\n"
+ "1) 2007\n"
+ "2) 2011\n"
+ "3) 2013\n"
+ "(Enter number for choice)");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
ArrayList<dwellingClass> alist = new ArrayList<dwellingClass>();
if (choice == 1) {
try {
File file = new File("2007.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] info = line.split(" ");
int age = Integer.parseInt(info[0]);
int region = Integer.parseInt(info[1]);
double lmed = Double.parseDouble(info[2]);
double fmr = Double.parseDouble(info[3]);
double extremelyLowIncome = Double.parseDouble(info[4]);
double veryLowIncome = Double.parseDouble(info[5]);
double lowIncome = Double.parseDouble(info[6]);
int bedrooms = Integer.parseInt(info[7]);
double value = Double.parseDouble(info[8]);
int rooms = Integer.parseInt(info[9]);
double utility = Double.parseDouble(info[10]);
dwellingClass dwelling = new dwellingClass(age, region, lmed, fmr, extremelyLowIncome, veryLowIncome, lowIncome, bedrooms, value, rooms, utility);
alist.add(dwelling);
}
scanner.close();
} catch (Exception a) {
a.printStackTrace();
System.exit(0);
};
for (dwellingClass each : alist) {
System.out.println(each);
}
}
System.out.println(alist.get(0).getAge());
}
}
谢谢大家的帮助!
我还发现,这也适用于以后阅读本文的任何人:
public class P2_0000000 {
public static void main(String[] args) {
// TODO code application logic here
ArrayList<dwellingClass> alist = new ArrayList<dwellingClass>();
try {
File file = new File("2007.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
int age = input.nextInt();
int region = input.nextInt();
double lmed = input.nextDouble();
double fmr = input.nextDouble();
double extremelyLowIncome = input.nextDouble();
double veryLowIncome = input.nextDouble();
double lowIncome = input.nextDouble();
int bedrooms = input.nextInt();
double value = input.nextDouble();
int rooms = input.nextInt();
double utility = input.nextDouble();
dwellingClass dwelling = new dwellingClass(age, region, lmed, fmr, extremelyLowIncome, veryLowIncome, lowIncome, bedrooms, value, rooms, utility);
alist.add(dwelling);
}
input.close();
} catch (Exception a) {
a.printStackTrace();
System.exit(0);
};
for (dwellingClass each : alist) {
System.out.println(each.getAge() + each.getRegion());
}
System.out.println(alist.get(0).getAge());
}
}
公共类P2\u0000000{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
ArrayList alist=新的ArrayList();
试一试{
File File=新文件(“2007.txt”);
扫描仪输入=新扫描仪(文件);
while(input.hasNext()){
int age=input.nextInt();
int region=input.nextInt();
double lmed=input.nextDouble();
double fmr=input.nextDouble();
double extremelyLowIncome=input.nextDouble();
double veryLowIncome=input.nextDouble();
double Low Income=input.nextDouble();
int beddrooms=input.nextInt();
double value=input.nextDouble();
int rooms=input.nextInt();
double utility=input.nextDouble();
住宅等级住宅=新的住宅等级(年龄、地区、lmed、fmr、极低收入家庭、极低收入家庭、卧室、价值、房间、公用设施);
(a)增加(居住);
}
input.close();
}捕获(例外a){
a、 printStackTrace();
系统出口(0);
};
适用于(每个居住等级:alist){
System.out.println(each.getAge()+each.getRegion());
}
System.out.println(alist.get(0.getAge());
}
}
您正在创建一个新文件,而不是加载您的文件。要加载文件,需要使用文件输入流。您的扫描仪在新文件中找不到任何内容,因此其全部为空
我站在更正,语法是正确的,这将加载一个现有的文件。正如上面其他人提到的,这是一个数据问题首先,您可以通过以下方式读取文件:
Scanner in = new Scanner(new FileReader("2007.txt"));
其次,要解析空白,您需要使用如下内容:
yourString.split("\\s+");
因此,您的这一行应该是:
String[] info = line.split("\\s+");
然后,您可以按您的方式访问您的字符串
但是,请确保您向每个方法传递了正确的值,即正确的类型,否则您将得到您得到的错误 您应该对从文件中读取的字符串进行数字验证,并确保它符合构建住宅类对象的要求
String line = scanner.nextLine();
String[] info = line.split("\\s+");
boolean validInput = true;
//loop through your info array and check each number is valid beforehand
for(int i = 0; i < info.length; i++)
{
if(!info[i].matches("\\d"))
{
validInput = false;
break;
}
}
//now we want to make sure our input was valid or else we don't create the object
if(info.length == 11 && validInput == true)
{
dwellingClass dwelling = new dwellingClass(
Integer.parseInt(info[0]),
Integer.parseInt(info[1]),
Double.parseDouble(info[2]),
Double.parseDouble(info[3]),
Double.parseDouble(info[4]),
Double.parseDouble(info[5]),
Double.parseDouble(info[6]),
Integer.parseInt(info[7]),
Double.parseDouble(info[8]),
Integer.parseInt(info[9]),
Double.parseDouble(info[4]));
alist.add(dwelling);
}
String line=scanner.nextLine();
字符串[]信息=line.split(\\s+);
布尔值validInput=true;
//循环查看您的信息数组,并事先检查每个数字是否有效
对于(int i=0;i
如果将其放入while循环中,它将只创建从文件中读取的仅包含数字和11位数字的行的对象,其他行将被忽略。这将允许执行文件,即使行的格式不正确。此外,我无法打印最后一行(getAge)。NumberFormatException
无法将字符串值解析为整数时返回parseInt
。因此,您需要查看数据,以确保以正确的顺序读取整数和double。要调试问题,您可以打印出字符串数组信息中的每个值,以确保它是您所期望的值。您可以在输入文件中包含导致错误的文件吗?我在这个输入文件上运行了您的代码,它没有得到任何异常:输入文件的第一行:1.1 2.2 3.3 4.4 5.5.1 7.8 9
输入文件的第二行:11 12.1 12.2 13.3 14.4 15.5 11 17.7 18 19.9
谢谢大家的帮助!如果有人支持,我会发布我提出的解决方案,但我仍然不明白为什么我的原始代码会返回这些错误。根据,这是scanner的有效用法:scanner sc=new scanner(新文件(“MyNumber”)
非常违反直觉,但好吧,我猜就像Pritam说的,这可能是因为空格失败了,可能这就是原因,在常规编辑器中,通过搜索一个选项卡,然后是空格,或者一个空格,然后是选项卡来检查您的文件。为什么您认为创建了一个新文件?如果文件已经存在,构造器会抛出FileNotFoundException
,因此它永远不会创建新文件。是的,我纠正了错误,检查了文档,发现语法非常违反直觉,我想这是因为我在web应用程序中不经常这样做。从不需要从本地加载,大部分时间甚至不允许。但通常在用new初始化一个对象时,它是一个新对象,JVM外部的任何东西都从JPA或某种形式的InputStream加载,因此新文件(“myfile.txt”)似乎不存在,问题是分隔符是一个空格,而您的模式将匹配多个空格
String line = scanner.nextLine();
String[] info = line.split("\\s+");
boolean validInput = true;
//loop through your info array and check each number is valid beforehand
for(int i = 0; i < info.length; i++)
{
if(!info[i].matches("\\d"))
{
validInput = false;
break;
}
}
//now we want to make sure our input was valid or else we don't create the object
if(info.length == 11 && validInput == true)
{
dwellingClass dwelling = new dwellingClass(
Integer.parseInt(info[0]),
Integer.parseInt(info[1]),
Double.parseDouble(info[2]),
Double.parseDouble(info[3]),
Double.parseDouble(info[4]),
Double.parseDouble(info[5]),
Double.parseDouble(info[6]),
Integer.parseInt(info[7]),
Double.parseDouble(info[8]),
Integer.parseInt(info[9]),
Double.parseDouble(info[4]));
alist.add(dwelling);
}