gpt4 book ai didi

java - 从文本文件中读取和提取不同的数据并创建不同的对象

转载 作者:行者123 更新时间:2023-11-30 06:22:31 25 4
gpt4 key购买 nike

我有三个文本文件。第一个看起来像这样:

1574683 Abyssinia 105 60 25 21 11

第二个看起来像这样:

2454438 Oceanway 112 52 20 5 8

但是第三个的一小部分看起来像这样(其余部分就这样,没有什么不同):

9146578 Atilgan 98 59 18 15 20

1000085320 N N
1000255961 N N
1000511923 N N
1000853206 N N
1001279810 N N
1001791735 N N
...

我试图从这些文件创建对象并将它们存储在数组列表中,前两个文件成功。但最后一个有点不同,因为每一行都是一个对象,但第一行是另一个对象,它必须包含其余对象,还有一个数组列表。
这是我的代码:

String[] name_and_path_of_files = {
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\1574683.txt",
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\2454438.txt",
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\9146578.txt"
};
Scanner inputFileReader = null;
Liman liman = new Liman();
try {
for (String fileInfo : name_and_path_of_files) {

inputFileReader = new Scanner(new File(fileInfo));

int line_index = 0;

while (inputFileReader.hasNext()) {
//String separated_numbers[] = inputFileReader.nextLine().split(" ");
//inputFileReader.useDelimiter("\\\\d+");
int IMO = inputFileReader.nextInt();
String isim = inputFileReader.next();
int Max_Konteyner_sayisi = inputFileReader.nextInt();
int Buyuk_Konteyner_sayisi = inputFileReader.nextInt();
int Sogutuculu_Konteyner_sayisi = inputFileReader.nextInt();
int Toksik_Taşıma_siniri = inputFileReader.nextInt();
int Patlayıcı_Taşıma_siniri = inputFileReader.nextInt();

Gemi yeniGemi = new Gemi(IMO, isim, Max_Konteyner_sayisi, Buyuk_Konteyner_sayisi, Sogutuculu_Konteyner_sayisi,Toksik_Taşıma_siniri, Patlayıcı_Taşıma_siniri);
liman.gemiEkle(yeniGemi); //gemiEkle adds ships into limans arraylist 'Gemiler'.
System.out.println(liman.Gemiler);
if(line_index > 0){
int Seri_Numarasi = inputFileReader.nextInt();
String Konteyner_Ozelligi = inputFileReader.next();
String Icerik_Ozelligi = inputFileReader.next();
Konteyner yeniKonteyner = new Konteyner(Seri_Numarasi,Konteyner_Ozelligi,Icerik_Ozelligi);
yeniGemi.konteynerEkle(yeniKonteyner);
System.out.println(yeniGemi.Konteynerler.toString());
}
line_index++;
}
}
//System.out.println(liman.Gemiler);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputFileReader.close();
}

几乎所有内容都有效,但第三个文件成功获取了第一行,但是当它获取其他对象时,它在这一行给出了一个InputMismatchException:

int Max_Konteyner_sayisi = inputFileReader.nextInt();

这是输出:

[IMO: 1574683,Isim: 阿比西尼亚, 105, 60, 25, 11]]
[IMO: 1574683,Isim:阿比西尼亚,105,60,25,11],IMO:2454438,Isim:Oceanway,112,52,20,8]]
[IMO: 1574683,Isim:阿比西尼亚,105,60,25,11],IMO:2454438,Isim:Oceanway,112,52,20,8],IMO:9146578,Isim:阿蒂尔甘,98,59,18,20 ]]

最佳答案

您只需要使用 hasNextInt():根据到目前为止我对您的设计的了解,您需要进行以下更改:

    String[] name_and_path_of_files = {
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\1574683.txt",
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\2454438.txt",
"C:\\Users\\Asus\\Documents\\NetBeansProjects\\NesneProjesi\\src\\\\9146578.txt"
};
Scanner inputFileReader = null;
Liman liman = new Liman();
try {
for (String fileInfo : name_and_path_of_files) {

inputFileReader = new Scanner(new File(fileInfo));

int line_index = 0;
Gemi yeniGemi = null; //---declare your arraylist-containing object here.
while (inputFileReader.hasNext()) {
//String separated_numbers[] = inputFileReader.nextLine().split(" ");
//inputFileReader.useDelimiter("\\\\d+");
int IMO = inputFileReader.nextInt();
String isim = inputFileReader.next();
if(inputFileReader.hasNextInt()){
int Max_Konteyner_sayisi = inputFileReader.nextInt();
int Buyuk_Konteyner_sayisi = inputFileReader.nextInt();
int Sogutuculu_Konteyner_sayisi = inputFileReader.nextInt();
int Toksik_Taşıma_siniri = inputFileReader.nextInt();
int Patlayıcı_Taşıma_siniri = inputFileReader.nextInt();
yeniGemi = new Gemi(IMO, isim, Max_Konteyner_sayisi, Buyuk_Konteyner_sayisi,
Sogutuculu_Konteyner_sayisi,Toksik_Taşıma_siniri, Patlayıcı_Taşıma_siniri)
liman.gemiEkle(yeniGemi); //gemiEkle adds ships into limans arraylist 'Gemiler'.
System.out.println(liman.Gemiler);
}
else{
//int Seri_Numarasi = inputFileReader.nextInt();//---why repeat when you have retrived this line's leading values?
//String Konteyner_Ozelligi = inputFileReader.next();
String Icerik_Ozelligi = inputFileReader.next();
Konteyner yeniKonteyner = new Konteyner(IMO,isim,Icerik_Ozelligi);
yeniGemi.konteynerEkle(yeniKonteyner);
System.out.println(yeniGemi.Konteynerler.toString());
}
}
}
//System.out.println(liman.Gemiler);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputFileReader.close();
}

关于java - 从文本文件中读取和提取不同的数据并创建不同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47838374/

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