In: Computer Science
Janet Littleton,6,7,14
Frank Edbrooke,17,9,31
Robert Hovery,25,1,18
Thomas Bingham,21,8,2
Stephen Bruce,7,9,23
For each player it shows:
For example, Janet Littleton hit 6 doubles, 7 triples and 14 home runs. Frank Edbrooke hit 17 doubles, 9 triples and 31 home runs.
Your job is to determine:
Let's first compile and see the output of all the baseball players:
Source Code in Java:
import java.io.*;
import java.util.Scanner;
public class BaseballPlayers
{
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(new File("/home/rohit/Desktop/baseballplayers.csv"));
sc.useDelimiter(",");
while (sc.hasNext())
{
System.out.print(sc.next());
}
sc.close();
}
}
Note: Change the path of the file in your case.
Screenshots for additional help:
Now according to the question, you need to find the number of baseball players which means you can simply return the count of how many players are there in the file or list. Here is the program for that:
import java.io.*;
import java.util.Scanner;
public class CountBaseballPlayers{
public static long countLineBufferedReader(File file) {
long lines = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
while (reader.readLine() != null) lines++;
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public static void main(String[] args){
File file = new File("/home/rohit/Desktop/baseballplayers.csv");
long result = CountBaseballPlayers.countLineBufferedReader(file);
System.out.println(result);
}
}
Note: Change the path of the file in your case.
Screenshots for additional help:
To find who hit more triples than doubles and home runs, go through this program:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CSVReader {
public static void main(String[] args) {
String csvFile = "/home/rohit/Desktop/baseballplayers.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] player = line.split(cvsSplitBy);
String playerName = player[0];
int doubles = Integer.parseInt(player[1]);
int triples = Integer.parseInt(player[2]);
int home_runs = Integer.parseInt(player[3]);
if(triples>doubles && triples>home_runs){
System.out.println(player[0]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Screenshots for additional help:
Note: Above file is updated one
As you can all are having more triples than doubles and home
runs, it displays everyone's name.
ALL THE BEST. THANKS.