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 7789917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:26:15+00:00 2026-06-01T21:26:15+00:00

I am a beginner level of studying java and revising for my exams through

  • 0

I am a beginner level of studying java and revising for my exams through answering the questions on previous past exam papers and there is one question that I am stuck on.

Consider the code fragment below that reads an input command then processes it.

String cmd = scanner.next();

if (cmd == "forward" )
    robot.forward(1);
else if (cmd == "turn" ) 
    robot.turn();
else
     System.out.println("Unknown command: " + cmd);

When testing the program the scanner reads in the String “forward” into cmd, but the program outputs “Unknown command: forward”.

a) Explain, in detail, why this occurs.

b) What changes should be made to the code to correct this error.

If someone can help me answer question a) and b) I would be grateful.

p.s. I understand that this isn’t a website to just look for answers (#noeasywayout) so I’m going to try my best not to act greedy here. My apologies for any inconvenience.

  • 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-06-01T21:26:16+00:00Added an answer on June 1, 2026 at 9:26 pm

    This old chestnut…

    In java, == tests if the two operands are the exact same object, which obviously they are not (one of the objects is a String constant, the other was read from input).

    Use String.equals() method to compare their value.

    Try this:

    if (cmd.equals("forward"))
        robot.forward(1);
    else if (cmd.equals("turn")) 
        robot.turn();
    else
        System.out.println("Unknown command: " + cmd);
    

    BTW, with this pattern of code, beware calling .equals() on cmd if it is null – you’ll get a NPE. A common way to avoid this without adding any code is to use a “yoda test” (one with a “reversed” logic):

    if ("forward".equals(cmd))
        robot.forward(1);
    else if ("turn".equals(cmd)) 
        robot.turn();
    else
        System.out.println("Unknown command: " + cmd);
    

    This code won’t throw an NPE if cmd is null

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

Sidebar

Related Questions

Is there a beginner-level tutorial on Committing a project created with Anjuta into a
For the record, I'm NOT a Java Beginner, but -- rather - an intermediate-level
I have worked on Mozilla Add-on Development in the past (beginner level). But on
Right now I'm reading a beginner-level book called 'Programming Embedded Systems'. It has one
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
This is a beginner level question for asp.net MVC I have the following code
I'm a beginner level programmer trying to make a game app for the iphone
I'm looking for suggestions for a beginner level ASP/XML test. Essentially I'm trying to
i am writing javascript code on nodepad++,i am at beginner level and write just
level: beginner the following code will print 'False' def function(x): if len(x) == 5:

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.