java - 如何根据文本文件中的特定关键字解析文本文件
问题描述
我目前有一个文本文件,我想将其解析为类。但是,我无法弄清楚为什么它没有正确执行。任何建议或帮助将不胜感激。
代码:
`
case "1":
try{
//Create file
File inputFile = new File("C:/Users/Olivia/Desktop/CIS331/products.txt");
Scanner scanner = new Scanner(inputFile);
//read data from file
while (scanner.hasNext()){
String type = scanner.nextLine();
//System.out.println(type);
if (type.equals( "PRODUCT")){
String pName= scanner.nextLine();
String pDescription = scanner.nextLine();
int pQuantity = Integer.parseInt(scanner.nextLine());
double pPrice = Double.parseDouble(scanner.nextLine());
boolean add = Product.addProduct(pName, pDescription, pQuantity, pPrice);
break;
}
else if (type.equals("AUTOMOBILE")){
String pName= scanner.nextLine();
String pDescription = scanner.nextLine();
int pQuantity = Integer.parseInt(scanner.nextLine());
double pPrice = Double.parseDouble(scanner.nextLine());
int y = Integer.parseInt(scanner.nextLine());
String mm = scanner.nextLine();
boolean add = Automobile.addAutomobile(pName, pDescription, pQuantity, pPrice, y, mm);
break;
}
else if (type.equals("VIDEO")){
String pName= scanner.nextLine();
String pDescription = scanner.nextLine();
int pQuantity = Integer.parseInt(scanner.nextLine());
double pPrice = Double.parseDouble(scanner.nextLine());
String genre = scanner.nextLine();
String rate = scanner.nextLine();
int time = Integer.parseInt(scanner.nextLine());
String star = scan.nextLine();
boolean add= Video.addVideo(pName, pDescription, pQuantity, pPrice, type, rate, time, star);
break;
}}
System.out.println("File read successfully");
}
catch (FileNotFoundException e) {
System.out.println("Error reading" + e.toString());
}
break;
`
解决方案
推荐阅读
- kubernetes - kubernetes 客户端中是否有替换“kubectl port-forward”命令的方法
- struct - 自动填充 SystemVerilog 打包结构,以便它们可以放入联合中
- c# - 使用 HttpClient 将 Content-Type 设置为“application/json”并将对象添加到正文
- python - 致命的 Python 错误:initfsencoding:无法加载文件系统编解码器 ModuleNotFoundError:没有名为“encodings”的模块
- angular - 如何解决重复标识符“按”错误?
- validation - Vuelidate 日期输入
- c - 书上说 c 标准提供了六位有效数字的浮点精度,但这不是真的吗?
- google-apps-script - Google Apps 脚本 - 有条件的用户输入 - 数字提示
- python - 来自非负数据集的欢乐图中的负值
- php - PHP - 获取当前 UTC 日期时间的正确方法