EDIT: Recent happenings of me compiling a program I know could not compile lead me to believe I am simultaneously having an issue with my compiler. No doubt due to my running it in WINE on mac as opposed to a native application. Thank you for your responses. I will properly test out all responses and make all changes when I have fixed said error with compiler or I have moved computers to one with a working one.
I am relatively new to programming and also this website so please bear with me. I am getting an error with my two of my If statements and one do/while that I am unable to resolve.
The entire program works as intended but two blocks which are below. The problem is that when I enter the character 'y', everything works as intended and the ("Results =" + Arrays.toString(row)) prints as I expected it to. It also proceeds to continue through the original For loop and start the program again.
However, when I enter any other character, (i.e not 'y' or 'n') the code does not print "Input must be either 'y' or 'n'" and just waits for another input. Even when 'n' is entered, it does not follow out of the the loop as I wanted, it just continues to loop and not proceed to the else if I had thought it would. It does this indefinitely, not accepting any other input than 'y' to continue passed the loop so I can never receive the print "negative".
Does anyone have any thoughts as to why this is happening? While not technically homework, I tagged it as such as I want to know, if possible, what is happening as opposed to just how to fix it.
do {
ans = input.next().charAt(0);
if (!ans.equals('y') || !ans.equals('n')) {
System.out.println ("Input must be either 'y' or 'n'");
}
} while (!ans.equals('y') || !ans.equals('n'));
and
if (ans.equals('y')) {
for (Object[] row : prevResults) {
System.out.println("Results = " + Arrays.toString(row));
}
} //
else if (ans.equals('n')) {
System.out.println("Negative");
//System.exit(0);
}
Full code is as below
import java.util.*;
public class Averages {
public static void main (String [] args) {
//declare variables
int course, exam, average = 0;
char ans;
String pass;
//creating objects
Scanner input = new Scanner(System.in);
List<Object[]> prevResults = new ArrayList<Object[]>();
//full loop
for (int i = 0; i < 5; i++ ) {
System.out.println ("Loop " + (++i) + " out of 5");
//Course loop
do {
System.out.println ("Please enter a course mark out of 100");
course = input.nextInt();
if (course > 100) {
System.out.println ("Number entered is over 100");
}
} while (course > 100);
//Exam loop
do {
System.out.println ("Please enter an exam mark out of 100");
exam = input.nextInt();
if (exam > 100) {
System.out.println ("Number entered is over 100");
}
} while (exam > 100);
average = (course + exam)/2;
// Final Grade
System.out.println ("The average mark is " + average);
if ( average >= 50 && course > 40 && exam > 40) {
System.out.println ("The final grade is pass");
pass = "Pass";
}
else {
System.out.println ("The final grade is fail");
pass = "Fail";
}
//add to array
prevResults.add(new Object[] { "Course mark: " + course, "Exam mark: " + exam,"Average: " + average, "Grade: " + pass});
System.out.println ("Would you like to see previous results? y/n");
//'Previous results' question loop
do {
ans = input.next().charAt(0);
if (!ans.equals('y') || !ans.equals('n')) {
System.out.println ("Input must be either 'y' or 'n'");
}
} while (!ans.equals('y') || !ans.equals('n'));
// Close or Array if statement
if (ans.equals('y')) {
for (Object[] row : prevResults) {
System.out.println("Results = " + Arrays.toString(row));
}
} //
else if (ans.equals('n')) {
System.out.println("Negative");
//System.exit(0);
}
}// end for
}//end main
}//end class
EDIT 2: I have switch computers and all the suggested answers to indeed work. They being
while (ans != 'y' && ans != 'n');
AND
while (!(ans.equals('y') || ans.equals('n')));
AND
Making a separate method as suggested by Chris Browne.
For the benefit of anyone else who reads this, these solutions work wonderfully although I have not had time to look at the BufferedReader suggested by Greg Hewgill I will most likely implement it because it seems like a better option from what he has stated.