簡體   English   中英

從Java中的文本文件讀取Race結果

[英]Reading Race results from a text file in Java

對於我的第一階段,我試圖從文本文件中讀取比賽結果。 如果每一行都具有完全相同的格式,就不會有問題。 造成這一困難的部分是學校名稱。 這是文本文件中一行的示例。

1  262  Jackson Bertoli (12) Terre Haute South  15:29.1 4:59

每所學校的名稱可能有1,2或3個字。 因此,我嘗試在學校名稱后尋找一個整數(時間),然后停止在該學校名稱中搜索單詞。 但是我在從reader.next();中獲取價值時遇到了問題。 並將其插入從整數中求值字符串的代碼部分,因為一個使用void,而另一個使用static。 另外,在整個代碼中都無法識別我的變量schoolnext和schoolnext2。

這很漫長,但這是我的第一個大男孩程序,對於我所擁有的東西或應該嘗試的其他方法,我將不勝感激。 預先感謝您花費所有的時間瀏覽它!

    /* Original program done by Donald Bough started 11/01/13
    * Cross Country results experimenter!
    * Put your results from indianarunner.com in a .txt file, plug them into the program,       and 
 * you should "eventually be able to" modify your own time or others in the text file
 * to see how it would've affected team places and points.
 * 
 * As of 11/04/13 I'm stuck figuring out how to tell, when scanning the text file, how many words make
 * up the school name, then printing off the rest of the results according to that number. This is the
 * beginning phase where I'm just trying to re-print off what the results already have basically.
 * Once I figure this out, I plan on taking those variables and actually doing the math with it.
 */

import java.lang.*;
import java.io.*;
import java.util.*;

public class myreadfile {
    //Setting up my variables
    int setschool = 0;
    int setschool2 = 0;
    String schoolnext;
    String schoolnext2;
    private Scanner reader;


    //Opening my text file with the race results. This part works 100%
    public void openfile(){
        try{
            reader = new Scanner(new File("C:\\Donalds\\Java\\Partsemistateresults.txt"));
        }
        catch (Exception e){
            System.out.println("This file does not exist");
        }
    }



    // Taking care of the first line of words we don't want to deal with. This also works 100%
        public void firstline(){

                String a = reader.next();
                String b = reader.next();
                String c = reader.next();
                String d = reader.next();
                String e = reader.next();
                String f = reader.next();
                String g = reader.next();


            System.out.printf("%s %s %s %s %s %s %s" , a , b, c , d ,e , f, g);
        }


    //tells if a string is an integer. Everything from now on gets uncertain and sticky.

        public static boolean isInteger(String schoolnext) {
        try { 
            Integer.parseInt(schoolnext); 
        } catch(NumberFormatException e) { 
            // This isn't an integer, so the number 2 is telling us we have a string
            //I want to move onto the next word since it's another name in the 
            //school. Now go back down to the readfile method.
            int setschool=+2;
            return false; 
        }
        //Is an integer. We now know schoolnext took the value of an integer. This 
        //integer would be the time part of the race results. Signal to move past 
        //the attempt in finding school names. Now go back down to the readfile method.
        int setschool =+1;
        return true;
    }



    //This is where the program goes after deciding we had another word in the school na,e/
    public static boolean isInteger2(String schoolnext2) {
        try { 
            Integer.parseInt(schoolnext2); 
        } catch(NumberFormatException e) { 
            //schoolnext2 turns out to be a string, that's three school word names. DONE
            int setschool2=+2;
            return false; 
        }
        //schoolnext2 turns out to be an integer, we're done. schoolnext2 value is the time value
        int setschool2 =+1;
        return true;
    }



    // Meat of the code, goes through all the lines
     public void readfile(){
        while(reader.hasNext()){
            //These are going to always be in this order, makes it do-able and simple.
            String place = reader.next();
            String bib = reader.next();
            String firstname = reader.next();
            String lastname = reader.next();
            String grade = reader.next();
            String school = reader.next() ;
            String schoolnext = reader.next();


        /*  *** Here is where I have trouble. All the schools have a different amount of words
             in their school name. You will now see my attempt at telling when the next part of the 
             race results line is part of the school name or the time ***              */


            // If setschool== 1, it is an integer. Making schoolnext in this case our time string
            //from the race results. Then we print the rest off.
                        if (setschool == 1) {
                            String pace = reader.next();
                            System.out.printf("%s %s %s %s %s %s %s %s\n" , place, bib, firstname, 
                            lastname, grade, school, schoolnext, pace);
                        }

            // If setschool was == 2, the next word isn't an integer, its another school name.
            // So we continue on with trying to find if the next string is the race results time
            //or another school name
            if (setschool == 2){
                String schoolnext2 = reader.next();





                //The most words a school has is 3, so once we know the third string is also a word,
                //we can can print off the 3 school strings which are representing each word of the 
                //school.
                if(setschool2 == 2){
                    String time = reader.next();
                    String pace = reader.next();
                    System.out.printf("%s %s %s %s %s %s %s %s %s %s\n" , place, bib, firstname, 
                    lastname, grade, school, schoolnext, schoolnext2, time, pace);
                }

                //The third word was an integer, our time from the race results. So now we print off
                //the two other Strings and realize schoolnext2 took on the schoolnext2 value
                if(setschool2 == 1){
                    String pace = reader.next();
                    System.out.printf("%s %s %s %s %s %s %s %s %s\n" , place, bib, firstname, lastname,
                    grade, school, schoolnext, schoolnext2, pace);
                }


            }







        }
    } 


    //Closing the file for good programming
    public void closefile(){
        reader.close();
    }
}

關於reader.nextInt()方法reader.nextInt() 掃描時查找下一個整數。

暫無
暫無

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

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