Java 如果我有多个属性,如何读取txt文件?
我试图让一个类通过几行代码来读取我的txt文件,例如: 面霜,1,2,0.1 保湿乳液,2,3,0.2 化妆水,3,4,0.3 芦荟乳液,4,5,0.4 我创建了一个具有属性name(string)、productNo(int)、productRating(int)和productDiscount(double)的类调用Lotion,并创建了另一个类调用listofloon,并添加了一个Lotion数组列表 我的问题是如何让我的ListoFloon类使用txt文件中的值并将其放入arraylist中 我试着在下一次使用indexOf作为名称,但出现了错误, java.lang.StringIndexOutOfBoundsException:开始0,结束-1,长度17 还有,无论如何,我可以分离所有四个值,并确保它们正确存储。例如,面部乳液作为名称存储,1作为prodcuctNo存储Java 如果我有多个属性,如何读取txt文件?,java,Java,我试图让一个类通过几行代码来读取我的txt文件,例如: 面霜,1,2,0.1 保湿乳液,2,3,0.2 化妆水,3,4,0.3 芦荟乳液,4,5,0.4 我创建了一个具有属性name(string)、productNo(int)、productRating(int)和productDiscount(double)的类调用Lotion,并创建了另一个类调用listofloon,并添加了一个Lotion数组列表 我的问题是如何让我的ListoFloon类使用txt文件中的值并将其放入arraylis
public void addListOfLotion(){
ArrayList<Lotion> lotion = new ArrayList<Lotion>();
Scanner scanner = new Scanner("Desktop/Lotion.txt");
while(scanner.hasNext()){
String readLine = scanner.nextLine();
int indexProductNo = readLine.indexOf(',');
int indexOfProductRating = readLine.indexOf(',');
double indexOfProductDiscount = readLine.indexOf(',');
lotion.add(new Lotion(readLine.substring(0, indexOfProductNo),0,0,0));
}scanner.close();
}
Got this error as result:
java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 17
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)
at ListOfVenues.addListOfLotion(ListOfLotion.java:42)
public void addlistofloon(){
ArrayList乳液=新ArrayList();
扫描仪=新扫描仪(“Desktop/Lotion.txt”);
while(scanner.hasNext()){
String readLine=scanner.nextLine();
int indexProductNo=readLine.indexOf(',');
int indexOfProductRating=readLine.indexOf(',');
double indexOfProductDiscount=readLine.indexOf(',');
乳液。添加(新乳液(readLine.substring(0,indexOfProductNo),0,0,0));
}scanner.close();
}
由于以下原因导致此错误:
java.lang.StringIndexOutOfBoundsException:开始0,结束-1,长度17
位于java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
位于java.base/java.lang.String.substring(String.java:1874)
addlistofloon(listofloon.java:42)
是不是因为我把readLine,indexOf(“,”)作为每个readLine,它只是停在第一个“,”?无论如何,我可以有效地让java知道,这个索引和这个索引之间代表name,这个索引和这个索引之间代表productNo
谢谢各位,非常感谢。您可以使用正则表达式():
您可以在类中将其定义为常量:
private static final Pattern LOTION_ENTRY =
Pattern.compile("([\\w\\s]+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+(?:\\.\\d+))");
然后,您可以为每个条目创建一个匹配器,并提取组:
Matcher matcher = LOTION_ENTRY.matcher(readLine);
if(matcher.matches()) {
String name = matcher.group(1);
int no = Integer.parseInt(matcher.group(2));
int rating = Integer.parseInt(matcher.group(3));
double discount = Double.parseDouble(matcher.group(4));
// do something
} else {
// line doesn't match pattern, throw error or log
}
不过需要注意的是:如果输入无效,parseInt()
和parseDouble
可以抛出一个NumberFormatException
,因此您必须捕获这些异常并相应地采取行动。您可以使用正则表达式():
您可以在类中将其定义为常量:
private static final Pattern LOTION_ENTRY =
Pattern.compile("([\\w\\s]+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+(?:\\.\\d+))");
然后,您可以为每个条目创建一个匹配器,并提取组:
Matcher matcher = LOTION_ENTRY.matcher(readLine);
if(matcher.matches()) {
String name = matcher.group(1);
int no = Integer.parseInt(matcher.group(2));
int rating = Integer.parseInt(matcher.group(3));
double discount = Double.parseDouble(matcher.group(4));
// do something
} else {
// line doesn't match pattern, throw error or log
}
不过需要注意的是:如果输入无效,parseInt()
和parseDouble
可以抛出一个NumberFormatException
。因此您必须捕获这些异常并相应地采取行动。由于行是逗号分隔的列表,因此可以使用split()
将行拆分为单个变量。
另一个需要考虑的是,<代码> Scanner(“file .txt”)不读取指定的文本文件,但只读取给定的<代码>字符串< /代码>。您必须首先创建<代码>文件>代码>对象。< /P>
File input = new File("Desktop/Lotion.txt");
Scanner scanner;
scanner = new Scanner(input);
while(scanner.hasNext()){
String readLine = scanner.nextLine();
String[] strArray = readLine.split(",");
int indexOfProductNo = Integer.parseInt(strArray[1].trim());
int indexOfProductRating = Integer.parseInt(strArray[2].trim());
double indexOfProductDiscount = Double.parseDouble(strArray[3].trim());
lotion.add(new Lotion(strArray[0],indexOfProductNo,indexOfProductRating,indexOfProductDiscount));
}
由于行是逗号分隔的列表,因此可以使用split()
将行拆分为单个变量。
另一个需要考虑的是,<代码> Scanner(“file .txt”)不读取指定的文本文件,但只读取给定的<代码>字符串< /代码>。您必须首先创建<代码>文件>代码>对象。< /P>
File input = new File("Desktop/Lotion.txt");
Scanner scanner;
scanner = new Scanner(input);
while(scanner.hasNext()){
String readLine = scanner.nextLine();
String[] strArray = readLine.split(",");
int indexOfProductNo = Integer.parseInt(strArray[1].trim());
int indexOfProductRating = Integer.parseInt(strArray[2].trim());
double indexOfProductDiscount = Double.parseDouble(strArray[3].trim());
lotion.add(new Lotion(strArray[0],indexOfProductNo,indexOfProductRating,indexOfProductDiscount));
}
如果这是复制粘贴,则问题在于“indexOfProductNo”未在此处声明,而是从其他地方使用。否则,您能否确保复制粘贴您正在使用的确切代码?如果这是复制粘贴,则问题在于“indexOfProductNo”不是在这里声明的,而是从其他地方使用的。否则,您可以确保复制粘贴您正在使用的确切代码吗?扫描仪的捕捉效果很好,我甚至没有注意到扫描仪的捕捉效果很好,我甚至没有注意到这一点