In: Computer Science
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; } fileReader.close(); } }
(How can you edit the following source code, I dont wanna use filenotfound but throws ioexception. also i cant use split. please modify these two details for me. Can't use a method or advanced knowledge.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class BowlerReader {
private static final String FILE_NAME = "bowler.txt";
public static void main(String[] args) throws IOException {
System.out.println("Reading Data from file");
Scanner fileReader = new Scanner(new File(FILE_NAME));
System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data",
"Game 1", "Game 2", "Game 3", "Average");
int bowler = 1;
while (fileReader.hasNext())
{
//reading scores
for a bowler
int
s1=fileReader.nextInt();
int
s2=fileReader.nextInt();
int
s3=fileReader.nextInt();
double
average = s1+s2+s3;
average
=average/ 3.0;
System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " +
bowler, s1,s2,s3, average);
bowler +=
1;
}
fileReader.close();
}
}