当前位置: 首页 > 知识库问答 >
问题:

从文本文件导入Java数组

栾和风
2023-03-14

我对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();
}

文本文件

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

共有1个答案

戚默
2023-03-14

nextline可以轻松解决问题,不需要使工作过于复杂

因为文本文件包含的信息格式如下

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

您可以使用以下方法轻松地将信息存储在所需的变量中

nextLine = inputStream.nextLine().split(" ");
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();
    }
 类似资料:
  • 想要知道更多东西吗?当你需要从表中查找某些值时,可以使用冗长的 case 语句或 selectors 实现,但更整洁的方式是使用 extlookup 函数实现。 在 puppetmaster 上可以使用 extlookup 函数查询外部的 CSV 文件,并返回匹配的数据片段。 将所有数据组织到一个单一的文件并将它从 Puppet 配置清单中分离出来, 可以使维护工作变得更简单,也便于与其他人分享:

  • 我正在制作一个应用程序,它处理存储在文本文件中的大量数据。本质上,应用程序浏览一个. txt文件,一旦找到,应用程序需要把文件中的所有数据放入JTable,然后我需要对数据执行一些过滤操作,然后将其导出。. txt文件中的数据格式如下: 有数千行。每行由双类型数字组成(A、B……均为1.3、2.0等) 我通过手动添加数组中的所有列名,然后将表的模型设置为 我已经把行作为'空'在这里,因为我不知道我

  • 您可以将由其他应用程序创建的文件文本导入到图稿中。 Illustrator 支持用于导入文本的以下格式: 用于 Windows 97、 98、 2000、 2002、 2003 和 2007 的 Microsoft® Word 用于 Mac OS X 2004 和 2008 的 Microsoft Word • RTF(富文本格式) • 使用 ANSI、Unicode、Shift JIS、GB23

  • 问题内容: 有没有办法将数据从JSON文件导入R?更具体地说,该文件是带有字符串字段,对象和数组的JSON对象的数组。关于如何处理此http://cran.r-project.org/web/packages/rjson/rjson.pdf,RJSON软件包尚不清楚。 问题答案: 首先安装软件包: 然后: 更新: 从0.2.1版本开始

  • 问题内容: 我有以下文件夹结构。 我想从位于另一个Python文件中的导入一些功能 我试过了 和其他一些尝试,但到目前为止,我无法正确导入。我怎样才能做到这一点? 问题答案: 默认情况下,你不能这样做。导入文件时,Python仅搜索当前目录,入口点脚本运行所在的目录,并且包括诸如软件包安装目录之类的位置(实际上比这稍微复杂一点,但这涵盖了大多数情况) 。 但是,你可以在运行时添加到Python路径

  • 所以我看了几个解决方案,但没有一个对我有效。感谢一些人,我最终得到了无限循环,但它仍然对我不起作用。我想将文件“output.txt”读入对象列表。所以我提供了剩下的代码,因为我认为它们不会有任何帮助... 错误消息如下: java.io.StreamCreptedException:无效的流头:3134313b,位于java.io.objectinPutStream.ReadStreamHead