簡體   English   中英

從包含使用java的數據打印混合的文本文件中讀取(;)分隔值

[英]Read ( ; ) separated value from text file that contains mixture of data print using java

我正在學習大學生,所以我需要你的幫助來解決這個問題。 我在UG_Student和GradStudent的student.txt文件混合中有以下數據。

Jane Jones;111999;11 First St, Unitown,2999;0401 444 444;4483;4;6;7
Peter Smith;111998;22 Second St, West,5999;999 777;B Sc;2000;4672;5;5;5
Jim Smith;111988;25 Third St, South,6001;0412 222 333;BESE;1998;4672;4;5;5
John William;334344;123 Fig St, Townville,5655;0404 333 333;4333;5;6;5

First和Last行按此順序有一個數據,表示UG_Student:

Name;StudentID;Address; ContactNo;SubjectNo;Test1;Test2;Test3

第二行和第三行按此順序有數據,表示GradStudent:

Name;StudentID;Address; ContactNo;PreviousDegree;GradYr;SubjectNo;Test1;Test2;Test3

我有以下代碼,如果txt文件包含UG_Student或GradStudent,則只能在JTextarea上讀取和打印。

public void openFile()
    {
        try
        {
            input = new Scanner( new File("student.txt"));
            input.useDelimiter(";");
        }
        catch ( FileNotFoundException fileNotFoundException)
        {
            System.err.println("Error opening file.");
            System.exit(1);
        }
    }

    public String readFile()
    {
        Student ugStudent = new UGstudents();
        Student gradStudent = new Gradstudents();

        //int lineNumber = 1;

        //System.out.printf("running...");

        try
        {
            while ( input.hasNext() )
            {       
                String name = ugStudent.name = input.next();
                int studentID = ugStudent.studentID = input.nextInt();
                String address = ugStudent.address = input.next();
                int contactNo = ugStudent.contactNo = input.nextInt();
                int subjectNo = ugStudent.subjectNo = input.nextInt() ;
                int test1 = ugStudent.test1 = input.nextInt();
                int test2= ugStudent.test2 = input.nextInt();
                int test3= ugStudent.test3 = input.nextInt();
                String checkGrade= ugStudent.checkGrade();

                //System.out.println(name + studentID + address + contactNo + subjectNo +  test1 + test2 + test3 + checkGrade);
                resultColumn = ("Name\t Student Id\t Address\t\t Contact No\t Subject No\t Test 1\t Test 2\t Test 3\t Grade\n--------------------------------------------------------------------------------------------------------------------\n");
                result+=(name+ "\t" + studentID + "\t " + address + "\t " + contactNo + "\t " + subjectNo + " \t" +  test1 + "\t " + test2 + "\t " + test3 + "\t " + checkGrade);

            }               
        }
        catch ( NoSuchElementException elementException)
        {
            System.err.println( "File improperly formed." );
            input.close();
            System.exit( 1 );
        }
        catch ( IllegalStateException stateException )
        {
            System.err.println( "Error reading from file." );
            System.exit( 1 );
        }
        return resultColumn +result;
    }

    public void closeFile()

   {

      if ( input != null )

         input.close();

   } 
}

但是,給定的文本文件應包含如上所示的數據混合。 我的問題是,如何從該文本文件中只讀取和打印UG_Student或GradStudent數據? 抱歉很長的問題,但這是我能做的最好的。

在我看來,你最好的選擇是使用正則表達式。 您可以為每種類型的行創建表達式,並嘗試匹配每一行,跳過那些不適用的行。 例如:

Pattern undergrad = Pattern.compile("(\\w*) (\\w*);(\\d*); ...");
Matcher matcher = undergrad.match(line);
if (matcher.matches()) {
    String firstName = matcher.group(1);
    String secondName = Matcher.group(2);
    String studentNumber = Matcher.group(3);
    ...
}

使用正則表達式的優點是您匹配行並標識要在單個指令中提取的值。 有關其使用的更多信息,請參見Pattern

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM