Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Homework Help - If/Else Statement and != Operator

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Homework Help - If/Else Statement and != Operator

    Hello, and thanks for providing this forum to people like me for help with programming.

    I have searched the forums and actually found two or three threads that involve the same assignment that I am currently trying to complete, however my code is not very similar and I could not find the answer to my specific question.

    The assignment is for beginners (I am definitely a beginner) and requires modifying provided code so that it functions properly/differently.
    The code is for an application that prompts and accepts input from the user, the values of which are test scores, calculates the average of the scores, and then prints the results to the user. Values up to 100 are valid, and anything over results in an error message stating the input as invalid. Entering 999 is supposed to end the input and print the results, and not give the invalid error message.
    I have modified the code so that it accepts input values up to 100 and displays the error message for values over 100, as well as completing input and calculating when 999 is entered, however my problem is that I can't seem to get it to not display the error message for 999 as well.
    As I said everything works, but it also prints the error message for 999 before correctly calculating and finishing.

    Here is my code:

    import java.util.Scanner;
     
    public class ModifiedTestScoreApp
    {
        public static void main(String[] args)
        {
            // display operational messages
            System.out.println("Please enter test scores that range from 0 to 100.");
            System.out.println("To end the program enter 999.");
            System.out.println();  // print a blank line
     
            // initialize variables and create a Scanner object
            double scoreTotal = 0;
            int scoreCount = 0;
            int testScore = 0;
            Scanner sc = new Scanner(System.in);
     
            // get a series of test scores from the user
            while (testScore != 999)
            {
                // get the input from the user
                System.out.print("Enter score: ");
                testScore = sc.nextInt();
     
                // accumulate score count and score total
                if (testScore <= 100)
                {
                    scoreCount = scoreCount + 1;
                    scoreTotal = scoreTotal + testScore;
                }
                if (testScore > 100 && testScore != 999)
                    System.out.println("Invalid entry, not counted");
            }
     
            // display the score count, score total, and average score
            double averageScore = scoreTotal / scoreCount;
            String message = "\n" +
                             "Score count:   " + scoreCount + "\n"
                           + "Score total:   " + scoreTotal + "\n"
                           + "Average score: " + averageScore + "\n";
            System.out.println(message);
        }
    }


    As you can see, I tried to use the != operator to exclude 999 from displaying the error message, but it is not working.
    I also tried something like testScore > 100 && testScore < 999, but it didn't seem to work either.

    I suspect the problem may be that those statements are within the while (testScore != 999) loop, but I'm not sure.
    Also, as this is a beginner's assignment, I believe I need to keep the code simple and use only statements similar to those that are currently being used.

    I hope this post is clear, and thank you for any help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    Can you post the console for when you execute the program that shows what you type in and what the program prints out?

    What is the value of testScore when the error message is printed? You should include the invalid value in the message so you know what the code is seeing.

    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Kakihara (September 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    Hi Norm and thanks for the quick reply.

    Here is what is shown in the command prompt:


    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\java\netbeans\ex_starts\ch02_ex2_TestScore\buil d\classes\dist>java -jar ch02_
    ex2_TestScore.jar

    Please enter test scores that range from 0 to 100.
    To end the program enter 999.

    Enter score: 80
    Enter score: 90
    Enter score: 125
    Invalid entry, not counted
    Enter score: 999
    Invalid entry, not counted

    Score count: 2
    Score total: 170.0
    Average score: 85.0


    C:\java\netbeans\ex_starts\ch02_ex2_TestScore\buil d\classes\dist



    As you can see, I entered 80 and 90 and they were correctly taken as valid values. Then I tested 125 as an invalid value and it successfully printed my error message. The problem is that the "Invalid entry, not counted" error message is displayed after entering 999.
    As 999 is the value to quit input and finish the program, it should not have this message.
    I used the != operator in my code to try to prevent it, but it is obviously not working.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    Did you try this:
    What is the value of testScore when the error message is printed? You should include the invalid value in the message so you know what the code is seeing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    I tried changing the error message to

    System.out.println("Invalid entry, not counted: " + testScore);"

    And I'm not seeing it print any value for testScore.
    Would that be the correct way to do it?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    If the value of testScore is not being printed at the end of the error message, that means that there is an old version of the program you are executing. If your new code was being executed, it would print testScore's value.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Kakihara (September 24th, 2012)

  9. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    Thank you Norm, that seems to have been the problem.
    I had assumed it must have been my code because I'm such a beginner.
    This was my first time really using Netbeans, so I wasn't aware that it could keep old versions of code executing, despite my re-building and saving each time. I'll just have to be more careful in the future to keep things clean and fresh.
    Sorry for such a novice problem, and thanks again.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Homework Help - If/Else Statement and != Operator

    A quick way to see if that is happening is to make a change to some print out and see it that change is printed. If not, there is an old version somewhere.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Kakihara (September 24th, 2012)

Similar Threads

  1. How Compare Integers using the if statement and && operator? Can it be done?
    By sethxhero7 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2012, 03:48 AM
  2. How to use final operator.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2011, 07:57 PM
  3. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 03:49 PM
  4. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 02:57 AM
  5. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM