Reputation: 9
I have the following information I am trying to read from a .txt file
Griffey HHOHOKWOHKSPOOWSH
Piazza OOHHHKPSOHOOHWWHO
Pudge HHHHKOOHHHSWWHHOP
I need to be able to separate the name, then each individual letter (preferably from something like a charAt. Each letter stands for a Hit, Out, Walk, Hit by Pitch, or Strikeout. I have to calculate each player's Batting Average, On Base Percentage, #Hits, #Walks, #Hit by Pitch. Finally I have to find the leaders of the 3 in each category.
So far, I am really stuck on being able to separate the names and plucking out each individual letter.
Here is my code so far:
import java.util.*;
public class readStats
{
public static void main(String[] args) throws Exception
{
//Pull stats.txt File
java.io.File file = new java.io.File("stats.txt");
//Create Scanner
Scanner input = new Scanner(file);
//Read data from file
while (input.hasNextLine())
{
String name1 = input.next();
String griffey1 = input.nextLine();
char batArray = griffey1.charAt(0);
//char griffey2 = input.next().charAt(1);
System.out.print(name1);
//System.out.println(griffey1);
//System.out.println(batArray[1]);
}
}
}
My output is:
GriffeyPiazzaPudge
UPDATE
Okay I have variables for each "item" now with this code:
import java.util.*;
public class readStats
{
public static void main(String[] args) throws Exception
{
//Pull stats.txt File
java.io.File file = new java.io.File("stats.txt");
//Create Scanner
Scanner input = new Scanner(file);
//Read data from file
while (input.hasNextLine())
{
String name1 = input.next();
String stats1 = input.next();
String name2 = input.next();
String stats2 = input.next();
String name3 = input.next();
String stats3 = input.next();
System.out.println(name1);
System.out.println(stats1);
System.out.println(name2);
System.out.println(stats2);
System.out.println(name3);
System.out.println(stats3);
}
}
}
Output:
Griffey
HHOHOKWOHKSPOOWSH
Piazza
OOHHHKPSOHOOHWWHO
Pudge
HHHHKOOHHHSWWHHOP
Upvotes: 1
Views: 166
Reputation: 522762
The following code parses each line of player stats, prints those stats to the console for each player, and then displays the leaders in each category. As a few have mentioned already, a better long term solution might be to encapsulate the stats for each player into a class, and use a formal data structure such as a Map
.
public class readStats {
public static int getStat(String letter, String stats) {
int count = stats.length() - stats.replace(letter, "").length();
return count;
}
public static void main(String[] args) {
String hitsLeader = null;
String outsLeader = null;
String walksLeader = null;
String hitsByPitchLeader = null;
String strikeoutsLeader = null;
int maxHits = 0;
int maxOuts = 0;
int maxWalks = 0;
int maxHitsByPitch = 0;
int maxStrikeouts = 0;
java.io.File file = new java.io.File("stats.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
String[] parts = line.split(" ");
int hits = getStat("H", parts[1]);
int outs = getStat("O", parts[1]);
int walks = getStat("W", parts[1]);
int hitsByPitch = getStat("P", parts[1]);
int strikeouts = getStat("S", parts[1]);
double ba = (double)hits / parts[1].length();
double obp = (double)(hits + walks + hitsByPitch) / parts[1].length();
System.out.println("Player " + parts[0] + " has the following stats:");
System.out.println("hits: " + hits);
System.out.println("outs: " + outs);
System.out.println("walks: " + walks);
System.out.println("hits by pitch: " + hitsByPitch);
System.out.println("strikeouts: " + strikeouts);
System.out.println("batting average: " + ba);
System.out.println("on base percentage: " + obp);
if (hits > maxHits) {
maxHits = hits;
hitsLeader = parts[0];
}
if (outs > maxOuts) {
maxOuts = outs;
outsLeader = parts[0];
}
if (walks > maxWalks) {
maxWalks = walks;
walksLeader = parts[0];
}
if (hitsByPitch > maxHitsByPitch) {
maxHitsByPitch = hitsByPitch;
hitsByPitchLeader = parts[0];
}
if (strikeouts > maxStrikeouts) {
maxHits = hits;
strikeoutsLeader = parts[0];
}
}
// print out leaders from all categories
System.out.println("Leader in hits is: " + hitsLeader);
System.out.println("Leader in outs is: " + outsLeader);
System.out.println("Leader in walks is: " + walksLeader);
System.out.println("Leader in hits by pitch is: " + hitsByPitchLeader);
System.out.println("Leader in strikeouts is: " + strikeoutsLeader);
}
}
Upvotes: 0
Reputation: 112
Here's how you can parse out each player and build a bucket of each stat:
import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class BaseballParser { class Player { String player; Map statCounts = new HashMap(); public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Player: ").append(player).append("\n"); for ( Character c : statCounts.keySet() ) { sb.append(c).append(" : ").append(statCounts.get(c)).append("\n"); } return sb.toString(); } } public Map process(File file) throws Exception { if ( !file.exists() ) { throw new RuntimeException("Cannot locate " + file.getAbsolutePath() + " for processing."); } Scanner scanner = new Scanner(file); String nextLine; Map playerMap = new HashMap(); while ( scanner.hasNext() ){ nextLine = scanner.nextLine(); if ( nextLine.trim().length() 1 ) { System.out.println("Usage: BaseballParser [path to stats file]"); System.exit(1); } BaseballParser parser = new BaseballParser(); Map playerStats = parser.process(new File(s[0])); playerStats.values().stream().forEach(System.out::println); } }
Outputs this:
Player: Piazza P : 1 S : 1 W : 2 H : 6 K : 1 O : 6 Player: Pudge P : 1 S : 1 W : 2 H : 9 K : 1 O : 3 Player: Griffey P : 1 S : 2 W : 2 H : 5 K : 2 O : 5
Hope this helps.
Upvotes: 0
Reputation: 99
You can use the following code
public static void main(String[] args) throws FileNotFoundException {
//Pull stats.txt File
java.io.File file = new java.io.File("c:/stats.txt");
//Create Scanner
Scanner input = new Scanner(file);
//Read data from file
while (input.hasNextLine())
{
String name1 = input.next();
String griffey1 = input.nextLine();
char[] batArray = griffey1.trim().toCharArray();
System.out.println(name1);
System.out.println(griffey1);
for(char c:batArray)
{
System.out.println(c);
}
}
}
Upvotes: 0
Reputation: 9
Okay, so I have this code now. This is helping me get each letter, but I'm wondering if there is any way to make a loop or something that would be faster/take up less lines?
import java.util.*;
public class readStats
{
public static void main(String[] args) throws Exception
{
//Pull stats.txt File
java.io.File file = new java.io.File("stats.txt");
//Create Scanner
Scanner input = new Scanner(file);
//Read data from file
while (input.hasNextLine())
{
String name1 = input.next();
String stats1 = input.next();
String name2 = input.next();
String stats2 = input.next();
String name3 = input.next();
String stats3 = input.next();
char OneABname1 = stats1.charAt(0);
char TwoABname1 = stats1.charAt(1);
System.out.println(name1);
System.out.println(stats1);
System.out.println(name2);
System.out.println(stats2);
System.out.println(name3);
System.out.println(stats3);
System.out.println(OneABname1);
System.out.println(TwoABname1);
}
}
}
Output
Griffey
HHOHOKWOHKSPOOWSH
Piazza
OOHHHKPSOHOOHWWHO
Pudge
HHHHKOOHHHSWWHHOP
H
H
My final output looks something like this written into another .txt file:
Individual stats – each item on a new line
Name
BA:
OB%:
H:
BB:
K:
HBP:
Blank line
League leaders – each item on a new line
League leaders header
BA:
OB%:
H:
BB:
K:
HBP:
Upvotes: 0
Reputation: 731
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
public class readStats
{
public static void main(String[] args) throws Exception
{
java.io.File file = new java.io.File("D:\\user\\Mahfuz\\stats.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
String spliter = ",";
try {
while ((line = br.readLine()) != null) {
String[] eachLise= line.split(spliter);
String griffey1 =eachLise[1];
for(int i=0; i<griffey1.length(); i++){
char batArray = griffey1.charAt(i);
System.out.println(batArray);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
txt file
Griffey,HHOHOKWOHKSPOOWSH
Piazza,OOHHHKPSOHOOHWWHO
Pudge,HHHHKOOHHHSWWHHOP
OutPut
H
H
O
H
O
K
W
O
H
K
S
P
O
O
W
S
H
O
O
H
H
H
K
P
S
O
H
O
O
H
W
W
H
O
H
H
H
H
K
O
O
H
H
H
S
W
W
H
H
O
P
Upvotes: 0
Reputation: 3056
You can use regular expression to get name:
Note: Place stats.txt in same directory of class file
RegEx
^(\\w+)[^\\s]
|-- ^(\\w+) => starts with word one or more
|-- [^\\s] => not contain space character
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class ReadStats {
public static void main(String[] args) {
try {
//Pull stats.txt File
File file = new File("stats.txt");
//Create Scanner
Scanner input = new Scanner(file);
//Read data from file
Pattern pattern = Pattern.compile("^(\\w+)[^\\s]");
while (input.hasNextLine()) {
String line = input.nextLine();
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.print(matcher.group());
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 44854
read each line then use
String words[ ] = String.split (" ");
words[0] = the name
words[1] = the code
then words[1] can then be split into a char array using
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toCharArray()
To read the lines do
Scanner input = new Scanner(file);
while (input.hasNextLine())
{
String line = input.nextLine();
.
Upvotes: 1