1

My problem:

I have txt file with that structure:

20:00   Norwich Res-Milton K.   
2.45
3.30
2.45 
20:30   Everton Res-Blackpool   
2.24
3.25
2.73

What i want is to read text file, and create objects from data inside. One object that i need is ie.(fields of one object ) :

    20:00   Norwich Res-Milton K. (String)
    2.45 (double)
    3.30 (double)
    2.45 (double)
...

My method to read data from txt:

public ArrayList<Match> getMatches(){
    try{
        File file = new File("matches.txt");
        FileReader readerF = new FileReader(file);
        BufferedReader reader = new BufferedReader(readerF);

        String line = null;

        while((line = reader.readLine()) !=null){
               //here i dont know what to do 
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, "");
    }
    return matches;
}

Do You have any tips/tricks how to do that? Thanks a lot for some answer

EDIT:

My Match class:

    public class Match {

        private String matchName;
        private double course1;
        private double courseX;
        private double courseY;

        public Match(String matchName, double course1, double courseX, double courseY){
            this.matchName=matchName;
            this.course1=course1;
            this.courseX=courseX;
            this.courseY=courseY;

    }

}
2
  • 2
    grab each line, if the line contains ":" you know you are at the beginning of a new object. Take that line and use it to contruct a new object along with the next 3 lines for it's other variables. Commented Nov 7, 2016 at 22:46
  • one possibility is: if (line.contains(":")) String tempStr = line; else double tempDbl = Double.parseDouble(line.trim()); Notes: this depends on if file structure, ensure all cases are handled. Also the temp variables would need to be printed/stored because they'll loose scope. Commented Nov 7, 2016 at 22:47

2 Answers 2

2

Hint: The logic for "//here i dont know what to do" needs to be something like this:

  1. Is this a line that starts a new match?
  2. If yes:
    1. parse the line to extract components
    2. create new match record
    3. make it the current match record
  3. If no:
    1. Is there a current match record? If no, then ERROR.
    2. parse the line as a number
    3. add the new number (whaterever it means) to the current match record.
Sign up to request clarification or add additional context in comments.

Comments

0

Try this (I'm assuming the input file is valid so you might need to handle exceptions otherwise):

     public ArrayList<Match> getMatches(){
        try{
            File file = new File("matches.txt");
            FileReader readerF = new FileReader(file);
            BufferedReader reader = new BufferedReader(readerF);

            String line = null;
            String matchName = null;
            double course1;
            double courseX;
            double courseY;
            ArrayList<Match> matches = new ArrayList<>();
            int count = 0;

            while((line = reader.readLine()) !=null){
                   if (count%4 == 3) {
                       Match match = new Match(line, course1, courseX, courseY);
                       matches.add(match);
                   } else if (count%4 == 2) {
                       courseY = Double.parseDouble(line);
                   } else if (count%4 == 1) {
                       courseX = Double.parseDouble(line);
                   } else {
                       course1 = Double.parseDouble(line);
                   }
                   count++;
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "");
        }
        return matches;
    }

1 Comment

i needed to replace the first if instructions with the else instructions, but anyway: It works. thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.