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

  • Home
  • SEARCH
  • 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 7525439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:32:46+00:00 2026-05-30T03:32:46+00:00

I think I’m having trouble with strings and while loops. When I run this

  • 0

I think I’m having trouble with strings and while loops. When I run this program and I type in an action the program does nothing. It doesn’t exit, it just sits there. That’s why I think it’s a problem with my while loop. But I think it could also be with my Strings right before the while loop. Am I declaring those Strings correctly? Or am I comparing them wrong in the while loop or something? Thanks for the help.

import java.util.Scanner;

public class HW2tester3 {

    public static void main(String args[]) {

        MotorBoat ex3 = new MotorBoat();
        Scanner keyboard = new Scanner(System.in);
        System.out.printf("Input how fast the motorboat was going: ");
        int s = keyboard.nextInt();
        ex3.ChangeSpeed(s);
        System.out.printf("Input the time the motorboat ran: ");
        int t = keyboard.nextInt();
        ex3.OperatingTime(t);

        // Ask the user what action he or she wants to take
        System.out.printf("If you want your distance travelled type:" + 
                          " distance\n");
        System.out.printf("If you want how much fuel you used type: fuel\n");
        System.out.printf("If you want to refuel type: refuel\n");
        System.out.printf("If you are finished type: done\n");
        System.out.printf("What would you like to do? ");

        // Compares the input with the defined strings and preforms the
        //   the action requested
        String a = keyboard.nextLine();
        String b = "done";
        String c = "distance";
        String d = "fuel";
        String e = "refuel";
        if (a != b) {
            while (a != b) {
                a = keyboard.nextLine();
                if (a == c) {
                    ex3.getDistance();
                }
                else if (a == d) {
                    ex3.getFuelUsed();
                }
                else if (a == e) {
                    ex3.Refuel();
                }
            }
        }
        if (a == b) {
            System.exit(0);
        }
    }
}
  • 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-30T03:32:48+00:00Added an answer on May 30, 2026 at 3:32 am

    a == b checks whether a and b are the same object, which isn’t always the case for strings. Use string.equals() instead.

    Also, use variable names you can differentiate between. a, b, c, d, etc. are not good variable names and will frequently confuse you.

    That being said, try this:

    String input = "";
    
    do {
      input = keyboard.nextLine();
    
      if (input.equals("distance")) {
        ex3.getDistance();
      } else if (input.equals("fuel")) {
          ex3.getFuelUsed();
      } else if (input.equals("refuel")) {
          ex3.Refuel();
      }
    } while (!input.equals("done"));
    
    System.exit(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think its a 5am brain drain, but I'm having trouble with understanding this.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I
I think most people know how to do this via the GUI (right click
I think this is specific to IE 6.0 but... In JavaScript I add a
I think I know how to handle this case, but I just want to
Think of this: A view controller's view is added as subview to another view.
Think Windows Explorer's 'Details' view. I need to output my database to this view.
I think that asking this might be kind of silly but I'm still wondering
Think that I have many activities,and all I want is this: I have a

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.