haebaragi.m
haebaragi.m

Reputation: 1

Stuck While Loop (Java)

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 + ".");
            }
        }
    }

}

Upvotes: 0

Views: 423

Answers (2)

Tyler
Tyler

Reputation: 1

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.

Upvotes: 0

Venkatesh Nandigama
Venkatesh Nandigama

Reputation: 518

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

Upvotes: 1

Related Questions