Parsing a text file

I need to write a program that will analyze baseball player information and hits, exit, walk, ect from a txt file. For example, a txt file might look something like this: Sam Slugger, h, h, o, s, w, w, h, w, o, o, o, h, s Jill Jenks, o, o, s, h, h, o, o Will Jones, o, o, w, h, o, o, o, o, w, o, o

I know how to parse a file and can make this code work fine. The only problem I encountered is that we should print only the name for each player and 3 or their plays. For example: Sam Slagger hit, hit, Jill Jenks came out, out, sacrifice to fly Will Jones come out, go out, walk

I am not sure how to limit this, and every time I try to disable it, I always get the first person who works fine, but he breaks the loop and does nothing for all the other players.

This is what I have so far:

import java.util.Scanner; import java.io.*; public class ReadBaseBall{ public static void main(String args[]) throws IOException{ int count=0; String playerData; Scanner fileScan, urlScan; String fileName = "C:\\Users\\Crust\\Documents\\java\\TeamStats.txt"; fileScan = new Scanner(new File(fileName)); while(fileScan.hasNext()){ playerData = fileScan.nextLine(); fileScan.useDelimiter(","); //System.out.println("Name: " + playerData); urlScan = new Scanner(playerData); urlScan.useDelimiter(","); for(urlScan.hasNext(); count<4; count++) System.out.print(" " + urlScan.next() + ","); System.out.println(); } } } 

This prints: Sam Slugger, h, h, o, but then the other players are canceled. I need help printing others as well.

+5
source share
4 answers

Try using this file with FileReader here Assuming your file format matches this

  Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Johns,h,h,o,s,w,w,h,w,o,o,o,h,s 

with each player in his own line, then this may work for you

  BufferedReader reader; try { reader = new BufferedReader(new FileReader(new File("file.txt"))); String line = ""; while ((line = reader.readLine()) != null) { String[] values_per_line = line.split(","); System.out.println("Name:" + values_per_line[0] + " " + values_per_line[1] + " " + values_per_line[2] + " " + values_per_line[3]); line = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } 

otherwise, if they are aligned all as one line, which does not make sense, then change this pattern.

  Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s| John Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s BufferedReader reader; try { reader = new BufferedReader(new FileReader(new File("file.txt"))); String line = ""; while ((line = reader.readLine()) != null) { // token identifier is a space String[] data = line.trim().split("|"); for (int i = 0; i < data.length; i++) System.out.println("Name:" + data[0].split(",")[0] + " " + data[1].split(",")[1] + " " + data[2].split(",")[2] + " " + data[3].split(",")[3]); line = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } 
+1
source

First problem

Change while(fileScan.hasNext())) to while(fileScan.hasNextLine()) . Not a problem, but when using a scanner, you usually put sc.* Immediately after sc.has* .

Second problem

Delete the line fileScan.useDelimiter(",") . This line does nothing in this case, but replaces the default delimiter, so the scanner no longer breaks into spaces. Which does not matter when using Scanner.nextLine , but may later have some unpleasant side effects.

Third problem

Change this line for(urlScan.hasNext(); count<4; count++) to while(urlScan.hasNext()) . Honestly, I am surprised that the line was even compiled, and if she did, then only the first 4 of the scanner.

If you want to limit the amount processed for each row, you can replace it with

for( int count = 0; count < limit && urlScan.hasNext( ); count++ )

This will limit the amount read by limit , while processing rows that have less data than the limit.

Make sure that each of your datasets is separated by a line, otherwise the output may not make much sense.

0
source

You need to reset your car count in a while loop:

  while(fileScan.hasNext()){ count = 0; ... } 
0
source

You should not have multiple scanners if you want the format published in your question, you can use regular expressions for this.

This demonstrates a regular expression to match the player and use as a separator for the scanner. I gave the scanner a line in my example, but the method is the same, regardless of the source.

 int count = 0; Pattern playerPattern = Pattern.compile("\\w+\\s\\w+(?:,\\w){1,3}"); Scanner fileScan = new Scanner("Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s,h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o"); fileScan.useDelimiter("(?<=,\\w)\\s"); while (fileScan.hasNext()){ String player = fileScan.next(); Matcher m = playerPattern.matcher(player); if (m.find()) { player = m.group(0); } else { throw new InputMismatchException("Players data not in expected format on string: " + player); } System.out.println(player); count++; } System.out.printf("%d players found.", count); 

Exit:

 Sam Slugger,h,h,o Jill Jenks,o,o,s Will Jones,o,o,w 

The call to Scanner.delimiter() sets the delimiter for retrieving tokens. Regular expression (?<=,\\w)\\s :

 (?< // positive lookbehind ,\w // literal comma, word character ) \s // whitespace character 

Which limits the players to a space between their records, not matching anything but this space, and does not correspond to a space between names.

The regular expression used to extract up to 3 pieces per player is \\w+\\s\\w+(?:,\\w){1,3} :

 \w+ // matches one to unlimited word characters (?: // begin non-capturing group ,\w // literal comma, word character ){1,3} // match non-capturing group 1 - 3 times 
0
source

All Articles