gpt4 book ai didi

java - 扫描仪无法正确读取输入

转载 作者:行者123 更新时间:2023-12-02 13:29:18 25 4
gpt4 key购买 nike

 File customer = new File("Cus.txt");
Scanner readCustomer = new Scanner(customer);
while(readCustomer.hasNextLine())
{
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println("Customer arrival: " + newCustomer);
}readCustomer.close();

输出

912, 4, 922, 0, 0
915, 5, -1, 10, 10
918, 0, -1, 5, 5
920, 0, -1, 10, 10
925, 6, 930, 0, 0

CUS.TXT 文件

915, 5, -1, 925, 10,  
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,

我真的很茫然,不知道如何解决这个问题。有谁看到任何错误或者为什么它无法在我的 split[4] 中读取?为什么它要复制 int appt 值中的内容?

最佳答案

@ Jamie 我无法理解您如何获得输出,因为您正在打印对象而不是值。我没有对您的代码进行任何更改,并且它对我来说工作得很好。您可能错过了一些非常小的事情。使用下面的代码,您将能够获得所需的输出。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class ReadingInputIncorrectly {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Customer> customerList=new ArrayList<>();
File customer = new File("path to your Cus.txt file");
Scanner readCustomer = new Scanner(customer);
while (readCustomer.hasNextLine()) {
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println(newCustomer.num1+", "+newCustomer.num2+", "+newCustomer.num3+", "+newCustomer.num4+", "+newCustomer.num5+", ");
}
readCustomer.close();
}
}
class Customer{
int num1,num2,num3,num4,num5;
Customer(int num1,int num2,int num3,int num4,int num5){
this.num1=num1;
this.num2=num2;
this.num3=num3;
this.num4=num4;
this.num5=num5;
}
}

输出

915, 5, -1, 925, 10, 
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,

Cus.txt 文件

915, 5, -1, 925, 10,  
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,

让我知道它是否有效。

关于java - 扫描仪无法正确读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244368/

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