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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:13:34+00:00 2026-05-27T22:13:34+00:00

I have this code: import java.util.Scanner; public class Example { public static void main(String[]

  • 0

I have this code:

import java.util.Scanner;

public class Example {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String answer = input.nextLine();

    if(answer == "yes"){
        System.out.println("Yea I programmed this right!");
    }else{
        System.out.println("Awww :(");
    }
  }
}

But when I run it and type yes, it should be saying

“Yea I programmed this right!”

but it says

“Awww 🙁“

  • 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-27T22:13:35+00:00Added an answer on May 27, 2026 at 10:13 pm

    You’re comparing strings incorrectly. You must use the equals() method, like this:

    if (answer.equals("yes"))
    

    When you’re programming in Java, the operator == is generally used for comparing primitive data types (int, double, etc.). If you use == for comparing two object types (like strings), you’re comparing them for identity, that is, checking if they reference the same object in memory. In your case, what you need is to compare if they’re equal: if they have the exact same value (a string of characters in this case) even if they’re two different objects – and for that you must use the equals() method.

    EDIT :

    Even better, for preventing a NullPointerException, it’s considered a good practice flipping the order of the comparison and writing first the string you’re comparing with, like this:

    if ("yes".equals(answer))
    

    The explanation is simple: if for some reason answer is null, the above comparison will evaluate to false (meaning: answer is not "yes"), whereas the first version of the code would cause a NullPointerException when trying to call the equals() method on a null value.

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

Sidebar

Related Questions

Wen I have this code: import java.util.Scanner; import java.util.Arrays; public class Ex02 { /**
This is my Code that I have so far: import java.util.*; public class VoteRecorder
Newbie question, but I have this code: import java.util.*; import java.io.*; public class Welcome1
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
i have this code and i want to change it to BigInteger import java.util.*;
I am writing an updater. I have this code: package main; import java.io.*; import
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
Consider this code: import java.util.Collections; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.userdetails.User; public class SecureStuff { @PreAuthorize(#user.password
Say I have this code: #import <UIKit/UIKit.h> @interface MyView : UIView @end @implementation MyView
I have this code in my forms.py : from django import forms from formfieldset.forms

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.