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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:27:05+00:00 2026-05-20T09:27:05+00:00

I am learning JAVA and trying to write my first loop. The loop should

  • 0

I am learning JAVA and trying to write my first loop. The loop should prompt the user to guess a defined name.

The code is not performing right. I have tried to search for help on different JAVA tuturials both not found any examples where you guess a name/string but a lot where you should guess a number.

This is my code:

 /**
     *
     * @author mso_
     */
    import java.util.Scanner;

    public class GuessName {

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

        public static final int C_Max_Trials = 10;

        public static void main(String[] args) {
            //Define correct name
            String name = "Morten";
            String guessName;

            //Create a scanner
            Scanner guess = new Scanner(System.in);

            //Recieve a guess
            do {
            System.out.println("Please guess my name. Enter your guess here: ");
            String guessName = guess.next(); <-- ERROR

            //Create loop
            } while (guessName != name); <-- ERROR
                System.out.println("Sorry, wrong guess, please enter another guess: ");

              if (guessName = name); <-- ERROR
                System.out.println("Right on! ");


        }

    }

What have I done wrong?

  • 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-20T09:27:05+00:00Added an answer on May 20, 2026 at 9:27 am

    String comparison

    You can’t compare Strings like this. This will only compare the references. You must use the equals() method :

    while (! guessName.equals(name));
    

    A little explanation : http://www.zparacha.com/java-string-comparison/

    Variable redeclaration

    There is another error in your code, you try to redeclare guessName inside the loop. You must declare guessName only once outside of the loop (ie before the do {).

    General mistakes

    Thirdly, there’s a some other errors in your code. I think all of them were pointed out in the others answer, but I’ll do a quick list :

    1. if (guessName = name); This is a useless statement as is, you must open a block : if(condition) { statement; }
    2. Same line, your doing an assignment, not a comparison, and like said, with String you must use .equals()
    3. The System.out.println() won’t be executed when you think. Re-read the doc about do { } while() loop until you really understand them.

    My advise : read carefully the error message of your compiler and read some doc before writing code.

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

Sidebar

Related Questions

I'm trying to learn how to write tests. I'm also learning Java, I was
I'm learning Objective-C and I'm trying to write a Vector3D class. In C++ or
I'm currently learning the BSON java library for mongodb and I'm trying to transform
this is my first post.. so I'm learning Android & Java (coming from Actionscript),
I'm learning Java by reading Head First Java and by doing all the puzzles
When I was learning Java coming from a background of some 20 years of
I am learning Java for a test (tomorrow) and am wondering about a question
So I'm learning java, and I have a question. It seems that the types
I'm learning Java (and OOP) and although it might irrelevant for where I'm at
I am a .NET dev learning Java. As a result, I understand all the

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.