Java,如何标记数组? (从文本文件读取/继承) [英] Java, how to tokenize an Array? (Reading from text file/ Inheritance)

查看:99
本文介绍了Java,如何标记数组? (从文本文件读取/继承)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我要分配一个作业,我必须从一个名为"Information.txt"的文本文件中读取内容,并将每行分成4个标记.姓名,员工编号,日期和时间.

"Information.txt"

Ok, so I have an assignment where I have to read from a text file called "Information.txt," and separate each line into 4 tokens. NAME, EMPLOYEE NUMBER, DATE, HOURS.

"Information.txt"

Jane Rivers 902-A 05/16/2001 1 16.25
Bob Cox 823-S 06/21/1990 2 17.50
Ann Ramsey 715-A 02/12/1998 1 16.25
Joseph Chandler 723-P 12/22/2000 3 14.35
Arnold Kennedy 133-S 08/10/1999 2 18.20
Larry Huber 198-P 02/12/2000 3 17.25
Annette Wilson 501-H 04/04/1995 1 20.25
Robert Ferguson 674-H 04/10/2002 2 16.50
Ava Gaines 434-P 01/05/2000 3 15.65


这是我现在的主班


This is my main class right now

import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;

public class Employees {
     public static void main (String[] args)throws IOException
     {
            String line, name, file = "Information.txt";

            StringTokenizer tokens = new StringTokenizer(line);

            String eName;
            String eNumber;
            String hireDate;
            String hours;

            //Array of worker information
            String[] workerInfo = new String[9];

            File openFile = new File (file);
            Scanner inFile = new Scanner (openFile);

            //Stores file information in an array
            while (inFile.hasNext())
            {
            for(int i = 0; i < workerInfo.length; i++)
                {
                workerInfo[i] = inFile.nextLine();
                }
            }
            
            ???????? How do I tokenize this?

            for(int i = 0; i < workerInfo.length; i++)
            {
                eName = tokens.nextToken();
                eNumber = tokens.nextToken();
                hireDate = tokens.nextToken();
                hours = tokens.nextToken();
                workerInfo[i] = new Employee(eName, eNumber, hireDate, hours);
            }
        }
}


这只是主要课程,我已经写好员工课程,我遇到的问题是我对如何标记信息不太熟悉.如您在Information.txt中看到的,其中有一个工作人员的姓名,电话号码,日期和小时数.我阅读了文本文件,并将其存储在数组中.如何将该数组中的信息标记为这4个标记,以便将其成功传递给Employee类?

***我知道可能有一种更简单的方法来做到这一点,但这是我选择的路线,并且真的很想使用数组来做到这一点.感谢您的帮助!


This is just the main class, I have the employee class already written out, the problem I am having is that I am not very familiar with how to tokenize information. As you can see in Information.txt, there is a worker name, number, date, and hours. I read the text file and stored it in an array. How do I tokenize the information in that array into those 4 tokens so I can pass it to my Employee class successfully?

***I am aware that there may be an easier way to do this but this is the route I chose and would really like to do it this way using an array. Thanks for any help!

推荐答案

0.类StringTokenizer需要将字符串作为参数进行标记化,因此只有在循环内部时才对其进行初始化.

1.文件格式是否固定?如果您有分隔符,则任务将变得更加容易.

2.以下示例 仅当at的格式为First SecondName EmployeeId date hours时有效:

0. The class StringTokenizer needs the string to be tokenized as a parameter, so don''t initialise it until you are inside the loop.

1. Is the file format fixed? If you had a delimiter character then the task becomes much easier.

2. The following example ONLY works if the form at is First SecondName EmployeeId date hours:

//Array of worker information
Employee[] workerInfo = new Employee[9];
File openFile = new File(file);
Scanner inFile = new Scanner(openFile);
//Stores file information in an array
while (inFile.hasNext()) {
    for (int i = 0;
            i < workerInfo.length;
            i++) {
        StringTokenizer tokens = new StringTokenizer(
                inFile.nextLine(), " ");
        eName = tokens.nextToken() + " " + tokens.nextToken();
        eNumber = tokens.nextToken();
        hireDate = tokens.nextToken();
        hours = tokens.nextToken();
        workerInfo[i] = new Employee(eName, eNumber, hireDate, hours);
    }
}


这篇关于Java,如何标记数组? (从文本文件读取/继承)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆