Reputation: 111
I am creating a reverse polish notation calculator in java that accepts file input. One of the requirements is that certain statistics are generated from the calculations too. below are two snippets of my code.
public static int invalidlines = 0;
public static int validlines = 0;
public static int stats1 = 0;
private static Scanner input;
public static ArrayList<String> validList = new ArrayList<String>();
public static ArrayList<Integer> stats = new ArrayList<Integer>(); {
if (stats.isEmpty());
display("n/a");
}
static int sum(ArrayList<Integer> stats)
{
int value = 0;
for(int i : stats)
{
value += i;
}
return value;
}
Thats the code for my array lists.
static void fileInput() throws IOException
{
input = new Scanner(System.in);
try
{
String currentLine = new String();
int answer = 0;
//Open the file
display("Enter File Name: ");
FileInputStream fstream = new FileInputStream(input.nextLine()); // make a input stream
BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); // pass input stream to a buffered reader for manipulation
String strLine; // create string vars
//loop to read the file line by line
while ((strLine = br.readLine()) != null) { // Whilst the buffered readers read line method is not null, read and validate it.
currentLine = strLine;
if(strLine.trim().isEmpty())
{
display("You have entered a blank line, the program will now exit");
System.exit(0);
}
if(isValidLine(currentLine))
{
validList.add(currentLine);
validlines++;
String[] filearray = new String[3];
filearray = currentLine.split(" ");
int val1 = Integer.parseInt(filearray[0]);
int val2 = Integer.parseInt(filearray[1]);
display("Your expression is: " + filearray[0] + " " + filearray[1] + " " + filearray[2]);
switch(filearray[2]) {
case("+"):
answer = val1 + val2;
stats.add(answer);
break;
case("-"):
answer = val1 - val2;
stats.add(answer);
break;
case("/"):
answer = val1 / val2;
stats.add(answer);
break;
case("*"):
answer = val1 * val2;
stats.add(answer);
break;
}
display("Your calculation is " + filearray[0] + " " + filearray[2] + " " + filearray[1] + " = " + answer);
}
}
}
catch (FileNotFoundException e)
{
display("Please Enter a valid file name");
}
display("Evaluations Complete");
display("=====================");
display("Highest Result: " + Collections.max(stats));
display("Lowest Result: " + Collections.min(stats));
display("Aggregate Result: " + sum(stats));
display("Average Result: " + sum(stats) / validlines);
display("Total Valid Lines: " + validlines);
display("Total Invalid Lines: " + invalidlines);
}
I require it so that if there are no valid calculations from the text file that the highest, lowest and average result displays show as 'n/a' instead they bring the java error in my console shown below.
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(Unknown Source)
at java.util.Collections.max(Unknown Source)
at Assignment.fileInput(Assignment.java:156)
at Assignment.main(Assignment.java:177)
Upvotes: 0
Views: 308
Reputation: 140318
Replace
Collections.max(stats)
with
(!stats.isEmpty() ? Collections.max(stats) : "n/a")
(similarly for Collections.min
)
You are also going to want to handle the case of validlines == 0
to avoid the division by zero error in / validlines
.
Upvotes: 4