Java 读取txt文件并将每个类别(品牌、颜色、年份)存储到不同的ArrayList中

Java 读取txt文件并将每个类别(品牌、颜色、年份)存储到不同的ArrayList中,java,loops,arraylist,Java,Loops,Arraylist,尝试读取txt文件并将每个类别(品牌、颜色、年份)存储到不同的ArrayList中,然后显示数组列表中的所有数据 我得到了什么 所以通过使用System.out.println(make.get(i))等,我得到 文本文件如下所示 汽车修理厂的3号显示车库里有多少车 “##”表示车辆详细信息的结尾 代码 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import

尝试读取txt文件并将每个类别(品牌、颜色、年份)存储到不同的ArrayList中,然后显示数组列表中的所有数据

我得到了什么

所以通过使用System.out.println(make.get(i))等,我得到

文本文件如下所示

汽车修理厂的3号显示车库里有多少车 “##”表示车辆详细信息的结尾

代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class testApp {

        static ArrayList<String> year= new ArrayList<>();
        static ArrayList<String> make = new ArrayList<>();
        static ArrayList<String> colors = new ArrayList<>();
    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
       
        
        System.out.println("*****Welcome*****");

        try {
            garage();
           

        } catch (Exception e) {
            //System.out.println(e.getMessage());
        }

    }

    
    public static void garage() throws NumberFormatException, IOException {
      
        System.out.println("setup");
        String filename = "garage.txt";
        String showError = "Error input file " + filename + " is not formmated properly.";
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));

      
        String line = reader.readLine();

        if (line == null) {
            reader.close();
            throw new IOException(showError);
        }

        int numCar= Integer.parseInt(line);

        System.out.println("num ques " + numCar);

        int carCount = 0;

        //loop to look for each car 
        while ((line = reader.readLine()) != null && carCount < numCar) {

          //  System.out.println("reading");

            if (line.equals("#car")) {
               
                // System.out.println("q");
                while ((line = reader.readLine().trim()) != null) {
                   
                    if (!line.equals("##")) {
                       
                        //reads car
                        if (line.equals("#make")) {
                            make.add(reader.readLine());
                         
                            System.out.println("q 1 "+make.get(0));
                        }

                        if (line.equals("#color")) {
                            System.out.println("make orig= " + reader.readLine());
                            colors.add(reader.readLine());
                           
                        }
                        if (line.equals("#year")) {
                            year.add(reader.readLine());
                            System.out.println("ans orig= " + reader.readLine());
                            //color = readAnswer(reader.readLine(),car);
                        }

                    } else {
                        break;
                    }

                }
            
                carCount++;
            }
        }
        
       for (int i = 0; i < 3; i++) {
       System.out.println(make.get(i));
       System.out.println(year.get(i));
       System.out.println(colors.get(i));
       }
         
        

    }

  

}

导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Scanner;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
公共类测试{
静态ArrayList年=新ArrayList();
静态ArrayList make=新建ArrayList();
静态ArrayList颜色=新ArrayList();
专用静态扫描仪扫描=新扫描仪(System.in);
公共静态void main(字符串[]args){
System.out.println(“******欢迎******”);
试一试{
车库();
}捕获(例外e){
//System.out.println(e.getMessage());
}
}
public static void garage()引发NumberFormatException,IOException{
System.out.println(“设置”);
字符串filename=“garage.txt”;
字符串阵雨Ror=“错误输入文件”+文件名+”格式不正确。“;
BufferedReader=new BufferedReader(new FileReader(new File(filename)));
字符串行=reader.readLine();
如果(行==null){
reader.close();
抛出新IOException(showError);
}
int numCar=Integer.parseInt(行);
System.out.println(“num ques”+numCar);
int carCount=0;
//循环查找每辆车
while((line=reader.readLine())!=null&&carCount
试试这个-在第13行(garage.txt)中做一个更改,使其颜色->颜色

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringJoiner;

public class TestApp {

    public static void main(String[] args) throws IOException {
        String filename = "garage.txt";
        File file = new File(filename);
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        String line = null;
        StringJoiner joiner = new StringJoiner("");
        int count = 0;
        while ((line = br.readLine()) != null) {
            if (count == 0) {
                count = Integer.parseInt(line.trim());
            } else {
                joiner.add(line.trim());
            }
        }
        String[] carDetails = joiner.toString().split("###");

        ArrayList<String> cars = new ArrayList<>();
        ArrayList<String> colors = new ArrayList<>();
        ArrayList<String> years = new ArrayList<>();

        for (String details : carDetails) {
            String car = details.split("#make")[1].split("#Color")[0];
            cars.add(car);
            String color = details.split("#make")[1].split("#Color")[1].split("#year")[0];
            colors.add(color);
            String year = details.split("#make")[1].split("#Color")[1].split("#year")[1];
            year = year.replace("#", "");
            years.add(year);
        }
        for (String car : cars) {
            System.out.println(car);
        }
        System.out.println("\n");
        for (String color : colors) {
            System.out.println(color);
        }
        System.out.println("\n");
        for (String year : years) {
            System.out.println(year);
        }
        br.close();
    }
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.StringJoiner;
公共类测试{
公共静态void main(字符串[]args)引发IOException{
字符串filename=“garage.txt”;
文件=新文件(文件名);
FileReader=新的FileReader(文件);
BufferedReader br=新的BufferedReader(读卡器);
字符串行=null;
细木工=新细木工(“”);
整数计数=0;
而((line=br.readLine())!=null){
如果(计数=0){
count=Integer.parseInt(line.trim());
}否则{
joiner.add(line.trim());
}
}
字符串[]carDetails=joiner.toString().split(“####”);
ArrayList cars=新的ArrayList();
ArrayList colors=新的ArrayList();
ArrayList年份=新ArrayList();
用于(字符串详细信息:CardDetails){
字符串car=details.split(“#make”)[1]。split(“#Color”)[0];
cars.add(car);
String color=details.split(#make)[1]。split(#color)[1]。split(#year)[0];
颜色。添加(颜色);
字符串年份=详细信息。拆分(“#make”)[1]。拆分(#Color”)[1]。拆分(#year”)[1];
年份=年份。替换(“#”和“);
年。加上(年);
}
用于(串车:车){
系统输出打印(car);
}
System.out.println(“\n”);
用于(字符串颜色:颜色){
系统输出打印项次(颜色);
}
System.out.println(“\n”);
用于(字符串年份:年){
系统输出打印项次(年);
}
br.close();
}
}

是否可以使用数组列表?我必须使用带有数组列表的数组。
3 
#car
#make
Toyota
#Color 
Blue
#year
2010
##
#car
#make
subaru
#color
black
#year
2003
##
#car
#make
honda
#Color
white
#year
2001
##

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class testApp {

        static ArrayList<String> year= new ArrayList<>();
        static ArrayList<String> make = new ArrayList<>();
        static ArrayList<String> colors = new ArrayList<>();
    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
       
        
        System.out.println("*****Welcome*****");

        try {
            garage();
           

        } catch (Exception e) {
            //System.out.println(e.getMessage());
        }

    }

    
    public static void garage() throws NumberFormatException, IOException {
      
        System.out.println("setup");
        String filename = "garage.txt";
        String showError = "Error input file " + filename + " is not formmated properly.";
        BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));

      
        String line = reader.readLine();

        if (line == null) {
            reader.close();
            throw new IOException(showError);
        }

        int numCar= Integer.parseInt(line);

        System.out.println("num ques " + numCar);

        int carCount = 0;

        //loop to look for each car 
        while ((line = reader.readLine()) != null && carCount < numCar) {

          //  System.out.println("reading");

            if (line.equals("#car")) {
               
                // System.out.println("q");
                while ((line = reader.readLine().trim()) != null) {
                   
                    if (!line.equals("##")) {
                       
                        //reads car
                        if (line.equals("#make")) {
                            make.add(reader.readLine());
                         
                            System.out.println("q 1 "+make.get(0));
                        }

                        if (line.equals("#color")) {
                            System.out.println("make orig= " + reader.readLine());
                            colors.add(reader.readLine());
                           
                        }
                        if (line.equals("#year")) {
                            year.add(reader.readLine());
                            System.out.println("ans orig= " + reader.readLine());
                            //color = readAnswer(reader.readLine(),car);
                        }

                    } else {
                        break;
                    }

                }
            
                carCount++;
            }
        }
        
       for (int i = 0; i < 3; i++) {
       System.out.println(make.get(i));
       System.out.println(year.get(i));
       System.out.println(colors.get(i));
       }
         
        

    }

  

}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringJoiner;

public class TestApp {

    public static void main(String[] args) throws IOException {
        String filename = "garage.txt";
        File file = new File(filename);
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        String line = null;
        StringJoiner joiner = new StringJoiner("");
        int count = 0;
        while ((line = br.readLine()) != null) {
            if (count == 0) {
                count = Integer.parseInt(line.trim());
            } else {
                joiner.add(line.trim());
            }
        }
        String[] carDetails = joiner.toString().split("###");

        ArrayList<String> cars = new ArrayList<>();
        ArrayList<String> colors = new ArrayList<>();
        ArrayList<String> years = new ArrayList<>();

        for (String details : carDetails) {
            String car = details.split("#make")[1].split("#Color")[0];
            cars.add(car);
            String color = details.split("#make")[1].split("#Color")[1].split("#year")[0];
            colors.add(color);
            String year = details.split("#make")[1].split("#Color")[1].split("#year")[1];
            year = year.replace("#", "");
            years.add(year);
        }
        for (String car : cars) {
            System.out.println(car);
        }
        System.out.println("\n");
        for (String color : colors) {
            System.out.println(color);
        }
        System.out.println("\n");
        for (String year : years) {
            System.out.println(year);
        }
        br.close();
    }
}