0

all! I'm a university freshman computer science major taking a programming course. While doing a homework question, I got stuck on a certain part of my code. Please be kind, as this is my first semester and we've only been doing Java for 3 weeks.

For context, my assignment is: "Create a program that will ask the user to enter their name and to enter the number of steps they walked in a day. Then ask them if they want to continue. If the answer is "yes" ask them to enter another number of steps walked. Ask them again if they want to continue. If they type anything besides "yes" you should end the program by telling them "goodbye, [NAME]" and the sum of the number of steps that they have entered."

For the life of me, I can not get the while loop to end. It's ignoring the condition that I (probably in an incorrect way) set.

Can you please help me and tell me what I'm doing wrong?

import java.util.Scanner;

public class StepCounter 
{

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) 
    {
        final String SENTINEL = "No";

        String userName = "";
        String moreNum = "";
        int numStep = 0;
        int totalStep = 0;
        boolean done = false;
        Scanner in = new Scanner(System.in);
        Scanner in2 = new Scanner(System.in);

        // Prompt for the user's name
        System.out.print("Please enter your name: ");
        userName = in.nextLine();

        while(!done)
        {
            // Prompt for the number of steps taken
            System.out.print("Please enter the number of steps you have taken: ");
            // Read the value for the number of steps
            numStep = in.nextInt();
            // Prompt the user if they want to continue
            System.out.print("Would you like to continue? Type Yes/No: ");
            // Read if they want to continue
            moreNum = in2.nextLine();
            // Check for the Sentinel
            if(moreNum != SENTINEL)
            {
                // add the running total of steps to the new value of steps
                totalStep += numStep;
            }
            else
            {
                done = true;
                // display results
                System.out.println("Goodbye, " + userName + ". The total number of steps you entered is + " + totalStep + ".");
            }
        }
    }

}
9
  • try to use debugger and see what is in your loop Commented Nov 5, 2019 at 5:58
  • I don't know how to use the debugger yet. ): Commented Nov 5, 2019 at 5:59
  • And the assignment says that you should check 'yes' to continue not 'no' to stop Commented Nov 5, 2019 at 5:59
  • What type of idi or editor you use to write code? Commented Nov 5, 2019 at 6:00
  • Netbeans because I have to. Commented Nov 5, 2019 at 6:01

2 Answers 2

1

To compare the contents of String objects you should use compareTo function.

moreNum.compareTo(SENTINEL) return 0 if they are equal.

== operator is used to check whether they are referring to same object or not.

one more issue with addition of steps, addition should be done in case of "No" entered also

Sign up to request clarification or add additional context in comments.

Comments

0

Use

if(!moreNum.equals(SENTINEL))

Instead of

if(moreNum != SENTINEL)

Also, make sure to add: totalStep += numStep; into your else statement so your program will actually add the steps together.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.