从文本文件导入Java Array

时间:2018-06-08 04:24:55

标签: java text import inputstream

目前我正在尝试从有关宠物和医生的文本文件中导入数据,并将其发送到我的“petArray”和“doctorArray”。

我对Java非常陌生,因此非常困难。这是我试图做的,但它看起来似乎没有用。

请参阅随附的Java代码和文本文件(链接截图)。

 public void readFile() {
    String fileName = "VetManagement.txt";
    Scanner inputStream = null;
    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening file: " + fileName);
        System.exit(0);
    }
    while (inputStream.hasNextLine()) {
        String line = inputStream.nextLine();
        String initName = "";
        String initSize = "";
        String initType = "";
        String initDoctor = "";
        double initWeight = 0.0;
        int initAge = 0;

        if (line.equals("Pets")) {
            inputStream.nextLine();

            if (line.equals("type cat")) {
                initType = "cat";
                System.out.print(initType);

            } else if (line.equals("type dog")) {
                initType = "dog";
                System.out.print(initType);
            }

            inputStream.nextLine();

            if (line.equals("size small")) {
                initSize = "small";

            } else if (line.equals("size medium")) {
                initSize = "medium";

            } else if (line.equals("size large")) {
                initSize = "large";

            } else System.out.println("error");

            inputStream.nextLine();

            if (line.startsWith("name")) {
                initName = inputStream.next();

            } else {
                System.out.println("error");
            }

            inputStream.nextLine();

            if (line.startsWith("weight")) {
                initWeight = inputStream.nextDouble();
            } else {
                System.out.println("error");
            }
            inputStream.nextLine();

            if (line.startsWith("age")) {
                initAge = inputStream.nextInt();
            } else {
                System.out.println("error");
            }
            inputStream.nextLine();

            if (line.startsWith("doctor")) {
                initDoctor = inputStream.toString();
            } else {
                System.out.println("error");
            }

            petArray[sumPets] = new Pet();
            petArray[sumPets].setType(initType);
            petArray[sumPets].setSize(initSize);
            petArray[sumPets].setName(initName);
            petArray[sumPets].setWeight(initWeight);
            petArray[sumPets].setAge(initAge);
            petArray[sumPets].setDoctorName(initDoctor);

        } else if (line.equals("Doctors")) ;

    }

    inputStream.close();
}

TEXT FILE:

Pets
type cat
size small
name Lara
weight 4
age 5
doctor Joao
type dog
size large
name Biro
weight 15
age 12
doctor Maria
type cat
size large
name Benny
weight 7
age 10
doctor no doctor assigned
Doctors
name Joao
specialisation cat
name Maria
specialisation dog

2 个答案:

答案 0 :(得分:1)

    if (line.equals("Pets")) {
        String nextLine = inputStream.nextLine();
        if (nextLine.equals("type cat")) {
            initType = "cat";
            System.out.print(initType);
        }
        else if (nextLine.equals("type dog")) {
            initType = "dog";
            System.out.print(initType);
        }
        String lineAfterThat=inputStream.nextLine();

您必须先存储每一行​​,然后才能执行任何操作。 您假设inputStream.nextLine()一次又一次地读取同一行,但每次使用inputStream.nextLine()时,它都会读取文件的下一行并最终到达文件末尾。这就是错误。

您不了解Scanner的工作方式 使用此:

if (line.equals("Pets")) {
    String nextLine = inputStream.nextLine();
    if (nextLine.equals("type cat")) {
        initType = "cat";
        System.out.print(initType);
    }
    else if (nextLine.equals("type dog")) {
        initType = "dog";
        System.out.print(initType);
    }
    String lineAfterThat=inputStream.nextLine();
    if (lineAfterThat.equals("size small")) {
            initSize = "small";

        } else if (lineAfterThat.equals("size medium")) {
            initSize = "medium";

        } else if (lineAfterThat.equals("size large")) {
            initSize = "large";

        } else System.out.println("error");
        String nextFirstWord=inputStream.next(); //so that it reads only till the space
        if (nextFirstWord.equals("name")) {
            initName = inputStream.nextLine();

        } else {
            System.out.println("error");
        }
        String ageLineFirstWord = inputStream.next();
        if (ageLineFirstWord .equals("age")) {
            initAge =inputStream.nextInt();
        } else {
            System.out.println("error");
        }
        inputStream.nextLine(); //this is to move the scanner to the nextline
        String doctorLineFirstWord = inputStream.next();
        if (doctorLineFirstWord .equals("doctor")) {
            initDoctor = inputStream.nextLine();
        } else {
            System.out.println("error");
        }

答案 1 :(得分:1)

nextLine可以轻松解决问题,无需过度复杂化工作

由于TEXT FILE包含格式为

的信息
type cat
size small
name Lara
weight 4
age 5
doctor Joao

您可以使用

轻松地将信息存储在所需的变量中
nextLine = inputStream.nextLine().split(" ");

其中nextLine[0]代表第一列,nextLine[1]代表第二列

我希望这有助于让我知道如果你有任何其他问题这里是完全代码(如果你需要)

public static void readFile() {
        String fileName = "F:\\document\\eclipse\\JavaAZ\\src\\VetManagement.txt";
        Scanner inputStream = null;
        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening file: " + fileName);
            System.exit(0);
        }
        String[] name = new String[100];
        String[] size = new String[100];
        String[] type = new String[100];
        String[] doctor = new String[100];
        double[] weight = new double[100];
        int[] age = new int[100];
        if (inputStream.hasNextLine()) {
            String[] nextLine = inputStream.nextLine().split(" ");
            int petCounter = 0;
            int doctorCounter = 0;
            String workingArray = new String(nextLine[0]);

            while(inputStream.hasNextLine()) {
                if(workingArray.equals("Pets")) {
                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("Doctors")) {
                        workingArray = "Doctors";
                        continue;
                    }

                    if (nextLine[0].equals("type")) {
                        type[petCounter] = nextLine[1];
                        //System.out.println(type);
                    }
                    else System.out.println("type error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("size")) {
                            size[petCounter] = nextLine[1];
                            //System.out.println(size);
                    } 
                    else System.out.println("size error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("name")) {
                            name[petCounter] = nextLine[1];
                            //System.out.println(name);
                    } 
                    else System.out.println("name error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("weight")) {
                            weight[petCounter] = Double.parseDouble(nextLine[1]);
                            //System.out.println(weight);
                    } 
                    else System.out.println("weight error"); 

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("age")) {
                            age[petCounter] = Integer.parseInt(nextLine[1]);
                            //System.out.println(age);
                    } 
                    else System.out.println("age error"); 

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("doctor")) {
                            doctor[petCounter] = nextLine[1];
                            //System.out.println(doctor);
                    } 
                    else System.out.println("doctor error"); 
                    petCounter++;
                }
                else if(workingArray.equals("Doctors")) {
                    // CODE HERE
                    doctorCounter++;
                    break;
                }
            }
         }
        System.out.println("PET NAME: "+name[0]+" and its Weight: "+weight[0]);
        inputStream.close();
    }