gpt4 book ai didi

Java解析多个文件

转载 作者:行者123 更新时间:2023-12-01 13:20:26 24 4
gpt4 key购买 nike

我需要解析多个文件并访问对象的初始化位置之外的方法。
这是我的代码:

public static void main(String[] args) {

try {
File Attrationfile = new File("attractions.txt");
Scanner attractionscanner = null;
attractionscanner = new Scanner(Attrationfile);
while (attractionscanner.hasNext()) {
String nextline = attractionscanner.nextLine();
String[] Attractioncomponents = nextline.split("@");

String ridename =Attractioncomponents[0];
int price = Integer.parseInt(Attractioncomponents[1]);
String type = Attractioncomponents[2];
int unknown = Integer.parseInt(Attractioncomponents[3]) ;
double speed = Attractioncomponents.length <= 4 ? 0 :
Double.parseDouble(Attractioncomponents[4]);

RollerCoaster rollerCoaster = new RollerCoaster(ridename, price , unknown, speed);

}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
File Customerfile = new File("customers.txt");
Scanner Customerscanner = new Scanner(Customerfile);

while (Customerscanner.hasNext()) {
String nextline = Customerscanner.nextLine();
String[] Customercomponents = nextline.split("#");

int accountnumber =Integer.parseInt(Customercomponents[0]);
String name = Customercomponents[1];
int age = Integer.parseInt(Customercomponents[2]) ;
int balance = Integer.parseInt(Customercomponents[3]) ;
String discount = Customercomponents.length <= 4
? String.valueOf(0) : Customercomponents[4];
Customer customer= new Customer(accountnumber,name, age, balance, discount);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

这有效,但我无法访问循环之外的对象。我不确定 Сustomer 类将如何获取有关过山车的信息,例如名称和价格。例如,如果客户对象和过山车对象在同一区域,我就可以通过带走 rollercoaster.getprice 来更新客户余额。来自 customer.getbalance , 和设置 customer.setbalance到计算值。正如您可能已经收集到的那样,我是一个初学者,所以我可能以错误的方式来解决这个问题 - 谢谢。

最佳答案

您可以通过在 main 方法的开头声明它们来更改这些变量的范围。

public static void main(String[] args) {
Customer customer = null;
RollerCoaster rollerCoaster = null;

try {
File Attrationfile = new File("attractions.txt");
Scanner attractionscanner = null;
attractionscanner = new Scanner(Attrationfile);
while (attractionscanner.hasNext()) {
String nextline = attractionscanner.nextLine();
String[] Attractioncomponents = nextline.split("@");

String ridename =Attractioncomponents[0];
int price = Integer.parseInt(Attractioncomponents[1]);
String type = Attractioncomponents[2];
int unknown = Integer.parseInt(Attractioncomponents[3]) ;
double speed = Attractioncomponents.length <= 4 ? 0 :
Double.parseDouble(Attractioncomponents[4]);

rollerCoaster = new RollerCoaster(ridename, price , unknown, speed);

}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
File Customerfile = new File("customers.txt");
Scanner Customerscanner = new Scanner(Customerfile);
while (Customerscanner.hasNext()) {
String nextline = Customerscanner.nextLine();
String[] Customercomponents = nextline.split("#");

int accountnumber =Integer.parseInt(Customercomponents[0]);
String name = Customercomponents[1];
int age = Integer.parseInt(Customercomponents[2]) ;
int balance = Integer.parseInt(Customercomponents[3]) ;
String discount = Customercomponents.length <= 4 ? String.valueOf(0) :
Customercomponents[4];
customer= new Customer(accountnumber,name , age , balance, discount);


}

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

关于Java解析多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707437/

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