Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

java - How to add comma separated text file data into different fields?

I am currently modelling a booking system for a cinema. Have been tasked with loading data from a text file and displaying it in certain text fields and combo boxes. I can load the WHOLE file into A text field but don't know how to split it up and add to others.

TEXT FILE INPUT - (type of booking - show / title / start time (hour)/(minute) / duration) (type of booking - booking / name / title / (hour) / (minute) / seats booked)

S, Tenet, 19, 30, 150
S, No Time to Die, 18, 00, 163
S, No Time to Die, 21, 00, 163
S, Skyfall, 20, 00, 162
B, Adam, Skyfall, 20, 00, G4, G5, G8, G3, G7, G6, G1, G2
B, Chris, A Christmas Carol, 17, 15, H7, H8
B, David, No Time to Die, 21, 00, F4, F3, F5, F6

Currently my seats are buttons labelled in the GUI accordingly. They are required to appear 'clicked' if they are booked.

Code for my 'open' file button.

private void OpenMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
     int returnVal = FileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = FileChooser.getSelectedFile();
    try {
      // What to do with the file,
       
      //Somewhere to go -> .read(new FileReader( file.getAbsolutePath() ), null );
    } catch (IOException ex) {
      System.out.println("problem accessing file"+file.getAbsolutePath());
    }
} else {
    System.out.println("File access cancelled by user.");
}
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Whoever generated that CSV should be slapped around...

Once you do a split and have an array of values from each line, you then need to analyze each array entry.

  S, Tenet, 19, 30, 150
becomes
  S
  Tenet
  19
  30
  150
And
  B, Chris, A Christmas Carol, 17, 15, H7, H8
becomes
  B
  Chris
  A Christmas Carol
  17
  15
  H7
  H8

So the first index [0] is the type of booking. The next one has some sort of meaning, either the name of the movie, or another name and then the name of the movie. Followed by the start hour and minutes and possibly the duration. Lastly you have a series of already booked seats.

The somewhat saving grace is that the movie name(s) is a string, the start time/duration are numbers, and the seats all start with a character. So you can analyse the array value if it is a number or not, then set flags to determine where in the array you are: inMovieName, inTimeDuration, inSeats. Then place the values in the array into a data object dependent on the flags.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...