英文:
Java parsing multiple files
问题
我需要解析多个文件并在它们被初始化的位置之外访问对象的方法。
这是我的代码:
public static void main(String[] args) {
try {
File attractionFile = new File("attractions.txt");
Scanner attractionScanner = null;
attractionScanner = new Scanner(attractionFile);
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();
}
}
这个代码能够工作,但是我不能在它们的循环之外访问对象。我不确定 Customer 类如何获取有关过山车的信息,比如名称和价格。例如,如果顾客和过山车对象在同一区域,我就可以通过从 rollercoaster.getprice
减去 customer.getbalance
来更新顾客余额,并将 customer.setbalance
设置为计算值。正如你可能已经了解的,我是一个初学者,所以我可能走错了方法 - 谢谢。
英文:
I need to parse multiple files and get access to the object's methods outside of where they were initialized.
This is my code:
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();
}
}
This works but I can't get access to the objects outside of their loops. I am not sure how the Сustomer class would get information about the roller coaster, such as the name and price. For example, if the customer and rollercoaster objects were in the same area, I would be able to update the customer balance by taking away rollercoaster.getprice
from the customer.getbalance
, and setting customer.setbalance
to the value of the calculation. As you have probably already gathered, I am a beginner, so I am probably going about this in the wrong way - thanks.
答案1
得分: 1
以下是翻译后的内容:
您可以通过在主方法开始时声明这些变量来更改变量的作用范围。
public static void main(String[] args) {
Customer customer = null;
RollerCoaster rollerCoaster = null;
try {
File attractionFile = new File("attractions.txt");
Scanner attractionScanner = null;
attractionScanner = new Scanner(attractionFile);
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();
}
}
英文:
You can change the scope for those variables by declaring them at the start of the main method.
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();
}
}
答案2
得分: 1
欢迎来到SO!正如Hovercraft所指出的,这些对象是在循环的范围内声明的,这意味着你不能在循环外访问它们,正如你注意到的那样。另外,它们在每次迭代时都会被覆盖,因为你在每次循环中都会声明和初始化对象。考虑使用ArrayList
,如下所示(这里只针对顾客):
ArrayList<Customer> customerList = new ArrayList<>();
try {
while (customerScanner.hasNext()) {
// ...
customerList.add(new Customer(accountnumber, name, age, balance, discount));
}
} catch (...) {
// ...
}
这里是ArrayList
的文档。<T>
是一个泛型类型,对你来说意味着你可以有一个ArrayList<Customer>
,ArrayList<RollerCoaster>
,ArrayList<String>
...。
附注:按照惯例,变量名以小写字母开头,比如Scanner customerScanner
,而不是Scanner Customerscanner
。
英文:
Welcome to SO! As Hovercraft pointed out, the objects are declared within the scope of the loop, meaning you can't access them outside of it as you noticed. Also, they are overwritten on every iteration, since you declare and initialize the object on every pass. Consider using an ArrayList
like so (here just for the customers):<br>
ArrayList<Customer> customerList = new ArrayList<>();
try {
while (customerScanner.hasNext()) {
// ...
customerList.add(new Customer(accountnumber,name, age, balance, discount));
}
} catch (...) {
// ...
}
Here's the doc for the ArrayList
.<br><br> <T>
is a generic type, which for you means that you can have an ArrayList<Customer>
, ArrayList<RollerCoaster>
, ArrayList<String>
...<br><br>
Sidenote: By convention, variable names start with a lowercase letter, like Scanner customerScanner
instead of Scanner Customerscanner
.
答案3
得分: 0
这是否是一个作用域问题?尝试在循环体外部声明对象。
因为在Java中,大括号表示作用域。嵌套的大括号越多,作用域就越小。您可以尝试在外部作用域中声明需要调用的对象,作用域可以是相同的或者更大的。
String type = null;
RollerCoaster rollerCoaster = null;
while (attractionscanner.hasNext()) {
String nextline = attractionscanner.nextLine();
String[] Attractioncomponents = nextline.split("@");
String ridename = Attractioncomponents[0];
int price = Integer.parseInt(Attractioncomponents[1]);
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);
}
英文:
Is it a question of scope? Try to declare an object outside the body of the loop.
Because in Java, the brace is a scope. The more nested braces, the smaller the scope. You can try to declare the objects you need to call in the external scope in the same or larger scope
String type = null;
RollerCoaster rollerCoaster = null;
while (attractionscanner.hasNext()) {
String nextline = attractionscanner.nextLine();
String[] Attractioncomponents = nextline.split("@");
String ridename =Attractioncomponents[0];
int price = Integer.parseInt(Attractioncomponents[1]);
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论