我想将特定值从文本文件复制到Java应用程序中的Arraylist。这是我的文本文件(以test.text的形式存储在我的桌面中)
String name = carrot;
double unit_price = 200;
int unit = 10;
我想将这个值存储在Arraylist中,该值在我的主应用程序中显示如下:
package com.main;
import com.collection.Ingridient;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileReaderApp {
public static void main(String[] args) throws FileNotFoundException, IOException {
Ingridient i_one = new Ingridient();
ArrayList<Ingridient> db = new ArrayList<Ingridient>();
FileReader fin = new FileReader("/home/yati/Desktop/test");
Scanner src = new Scanner(fin);
// Read the ingridient from text file.
while (src.hasNext()) {
if (src.hasNext()) {
i_one.setName(src.next());
System.out.println("Name: " +src.next());
} else
if(src.hasNextDouble()) {
i_one.setUnit_price(src.nextDouble());
System.out.println("Unit Price: " +src.nextDouble());
}
else if (src.hasNextInt()) {
i_one.setUnit(src.nextInt());
System.out.println("Unit: " +src.nextInt());
} else {
System.out.println("File format error.");
return;
}
db.add(i_one);
}
fin.close();
}
}
她的Ingridient课有以下代码:
package com.collection;
public class Ingridient {
String name;
Double unit_price;
int unit;
public Ingridient() {
name = null;
unit_price = null;
unit = 0;
}
public void setName(String name) {
this.name = name;
}
public void setUnit_price(Double unit_price) {
this.unit_price = unit_price;
}
public void setUnit(int unit) {
this.unit = unit;
}
}
我的问题是我的应用程序只能在Ingridient对象中存储名称,而不能在unit和unit_price中存储任何值。获得的输出是:我知道我在某个地方犯错,但是我无法解决此问题。有什么建议吗?
这应该做到这一点:
public static void main(String[] args) throws IOException {
String content = "String name = carrot;\ndouble unit_price = 200;\nint unit = 10;";
try (Scanner sc = new Scanner(content)) {
sc.useDelimiter("(;*\n*.+ .+ = )|;");
List<Incredient> incredients = new ArrayList<>();
while (true) {
Incrediend incredient = new Incredient();
if (sc.hasNext()) {
String name = sc.next();
incredient.setName(name);
System.out.println("Name: " + name);
} else {
break;
}
if (sc.hasNextDouble()) {
double unitPrice = sc.nextDouble();
incredient.setUnit_price(unitPrice);
System.out.println("Unit Price: " + unitPrice);
} else {
break;
}
if (sc.hasNextInt()) {
int unit = sc.nextInt();
incredient.setUnit(unit);
System.out.println("Unit: " + unit);
} else {
break;
}
incredients.add(incredient);
}
} catch (Exception e) {
e.printStackTrace();
}
}
This works because of the delimiter I've used (;*\n*.+ .+ = )|;
. This pice of regex just removes all the parts of the text file that you're not interested in saving.
There are a couple of problems with your approach, for example this:
i_one.setName(src.next());
System.out.println("Name: " +src.next());
Here, you're reading 2 tokens from the scanner because there are 2 calls to next()
, if you want to use the same token for multiple things you should create a new variable to store it in (ex: String name = sc.next()
).
The default delimiter that the Scanner
is using is a single space and because of that, in your code, hasNextDouble()
and hasNextInt()
will never be true, all the numbers in the text file end with ;
.
我不确定您要在这里做什么,从文本文件中解析Java代码有点不寻常。如果可以更改文本文件的格式,则应选择一种更易于解析的格式(例如CSV)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句