Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7607273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:37:12+00:00 2026-05-31T00:37:12+00:00

EDIT: Recent happenings of me compiling a program I know could not compile lead

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:37:13+00:00Added an answer on May 31, 2026 at 12:37 am

    First, an error in your code:

    System.out.println ("Loop " + (++i) + " out of 5");
    

    You should not increment i as it is already incremented in your for update statement – you are getting the wrong iteration count as a result.

    Next, your should not use equals to compare char values – use == instead. When you use equals, a lot of unnecessary things happen due to the auto-boxing, that ultimately result in roughly characterObject.equals(anotherCharacterObject), objects being of type java.lang.Character. Just use e.g. ans == 'y' instead.

    Last, as folks have pointed out, you should rewrite your do-while as:

    do {
        ans = input.next().charAt(0);
        if (ans != 'y' && ans != 'n') {
            System.out.println ("Input must be either 'y' or 'n'");
        }
    } while (ans != 'y' && ans != 'n');
    

    or even have a separate method for the condition check (thanks to Chris Browne).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
I've got a macro that updates a copyright header with the most recent edit
I want to find the most recent edit date for tables in my database.
EDIT: This is weird... if I double-click the Recent Projects item for the project
A recent question about string literals in .NET caught my eye. I know that
Edit : Array should be CvMat or IplImage is not an error message specific
Edit 4: I was finally able to solve my own issue. See checkmark answer
The rejection of a recent edit I suggested gave me doubts about something I
EDIT I tried both of the suggestions below, but it's still not working... It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.