In: Computer Science
Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound).
Make this method call FileSum.read and make your method catch all the errors.
FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound.
I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know what the whole "lower bound" means or is suppose to do. Please attach comments/notes as to why something is there/ how it works. I'm also not very great on how to import files. Thank you if you can help even just a bit.
Given:
FileSum.java
import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int read(String filename, int lowerBound) throws Exception { Scanner inputFile = new Scanner(new File(filename)); int acc = 0; boolean atLeastOneFound = false; while (inputFile.hasNext()) { int data = inputFile.nextInt(); if (data >= lowerBound) { acc += data; atLeastOneFound = true; } } if (!atLeastOneFound) { throw new UnexpectedException(""); } return acc; } }
---------------------------------------------------------------------------------------------------------
Question1.java
import java.util.Scanner; public class Question1 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a filename"); String filename = keyboard.nextLine(); System.out.println("Enter a lower bound"); int lowerBound = keyboard.nextInt(); FileSumWrapper.handle(filename, lowerBound); } }
---------------------------------------------------------------------------------------------------------
err.txt
50 40 30 90 85 23 06 30x 54 675 875 34 2323 423 423 5 5 79 97 90y 7986 5 46 64656 66 6 333 93 9 300 20 2 9 209 290 39 48 85 7847 578048
---------------------------------------------------------------------------------------------------------
t1.txt
50 40 30 90 85 23 06 30 54 675 875 34 2323 423 423 5 5 79 97 90 7986 5 46 64656 66 6 333 93 9 300 20 2 9 209 290 39 48 85 7847 578048
---------------------------------------------------------------------------------------------------------
Input | Output |
---|---|
t1.txtENTER 50 |
Enter a filename\n Enter a lower bound\n Sum of all numbers in t1.txt is 665177\n |
Input | Output |
---|---|
t1.txtENTER 500 |
Enter a filename\n Enter a lower bound\n Sum of all numbers in t1.txt is 662410\n |
Input | Output |
---|---|
t1.txtENTER 5000000 |
Enter a filename\n Enter a lower bound\n CS112 error: Found no numbers above 5000000\n |
Input | Output |
---|---|
t1.txtENTER 6000000 |
Enter a filename\n Enter a lower bound\n CS112 error: Found no numbers above 6000000\n |
Input | Output |
---|---|
t1e.txtENTER 50 |
Enter a filename\n Enter a lower bound\n CS112 error: Couldn't open t1e.txt\n |
Input | Output |
---|---|
t11e.txtENTER 50 |
Enter a filename\n Enter a lower bound\n CS112 error: Couldn't open t11e.txt\n |
Input | Output |
---|---|
err.txtENTER 50 |
Enter a filename\n Enter a lower bound\n CS112 error: Was reading integers but found something else\n |
FileSum.java
import java.io.File;
import java.rmi.UnexpectedException;
import java.util.Scanner;
public class FileSum {
public static int read(String filename, int lowerBound) throws
Exception
{
Scanner inputFile = new Scanner(new File(filename));
int acc = 0;
boolean atLeastOneFound = false;
while (inputFile.hasNext()) {
int data = inputFile.nextInt();
if (data >= lowerBound) {
acc += data;
atLeastOneFound = true;
}
}
if (!atLeastOneFound) {
throw new UnexpectedException("");
}
return acc;
}
}
FileSumWrapper.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileSumWrapper {
public static void handle(String fileName, int lowerBound)
{
// section to check:
// 1. If the file exists or not, and
// 2. If only integers are being read from the file or not
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNext())
{
if(!isDigit(fileReader.next()))
System.exit(0);
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Error: Couldn't open " + fileName);
System.exit(0);
}
// section for calling FileSum.read() method
try
{
int sum = FileSum.read(fileName, lowerBound);
System.out.println("Sum of all numbers in " + fileName + " is " +
sum);
}catch(Exception ex){
System.out.println("Error: Found no numbers above " +
lowerBound);
System.exit(0);
}
}
// this method returns true if the String s in an integer, else
returns false
private static boolean isDigit(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException nfe){
System.out.println("Error: Was reading integers but found something
else");
return false;
}
}
}
Question1.java (Main class)
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a filename");
String filename = keyboard.nextLine();
System.out.println("Enter a lower bound");
int lowerBound = keyboard.nextInt();
FileSumWrapper.handle(filename, lowerBound);
}
}
**************************************************************** SCREENSHOT *******************************************************