Can someone help me with my computer science homework. It is in java and i am supposed to find the batting average of a baseball player
Here is the assignment:
A batter in baseball makes one or more plate appearances. In this problem, there are only six different outcomes of a plate appearance: a single(S), Double(D), triple(T), home run(H),out(O),or a walk(W). All of these plate appearances, except a walk or an out, are considered hit. A player's batting average is the number of hits divided by the number of at-bats, rounded to three decimal places, You are to write a program that computes a player's batting average.
Input Format: the first line in the data file indicates the number of data sets. The data set will be on one line and contain no less than one and no more than ten plate appearances.
Here is how the Data file I have to read it from looks
5
HOOWTDSOHW
WWW
WWOOWWWOO
OOSWHHDO
OO
Nevermind I figured it out, my hits and out were ints instead of doubles so it wouldn't average out right. Here is my new code:PS. It doesnt let me post the for loops right import java.io.*;
import java.util.Scanner;
import java.io.File;
public class BattingAverage {
public BattingAverage() {
}
public static void main(String[] args) throws IOException{
Scanner infile = new Scanner (new File("C:\\CSA\\BattingAverage.txt"));
String at_bat="";
double avg=0.0;
String possibilities="SDTHOW";
char S=possibilities.charAt(0);
char D=possibilities.charAt(1);
char T=possibilities.charAt(2);
char H=possibilities.charAt(3);
char O=possibilities.charAt(4);
char W=possibilities.charAt(5);
double hits=0; //S D T H
double out=0;
BattingAverage ex= new BattingAverage();
int num_batters=infile.nextInt();
for(int batter=0;batter
at_bat=infile.next();
hits=0;
out=0;
for(int plate_app =0; plate_app
char character=at_bat.charAt(plate_app);
if(character==S||character==D||character==T||character==H)
hits++;
else if(character==O)
out++;
}
int batternum=batter+1;
if(hits==0&&out==0)
System.out.printf("Batter %d did not get an at bat\n", batternum);
else{
avg=hits/(hits+out);
System.out.printf("Batter %d had a %.3f batting average\n", batternum,avg);
}
}
}
}