Jump to content

I don't understand why this doesn't work

Oryzaki
Go to solution Solved by zwirek2201,

First of all, for pasting code please use code tags. You can find them in the edit post bar marked by <> icon. It will make it easier for people to read the code and edit it if neede.

 

Now to your problem:

Instead of using s1 == "boy" use s1.equals("boy")

 

"==" operator checks whether references to s1 and "boy" are equal. ".equals" method checks actual contents. 

 

You can read about it more here:

https://stackoverflow.com/questions/767372/java-string-equals-versus

package apples;

import java.util.Scanner;

public class apple {
     public static void main (String args[]){
         Scanner input = new Scanner(System.in);
         String girl = "girl";
         
        
         System.out.println("What is your gender?");
         String s1 = input.next();
         if (s1 == "boy") {
              System.out.println("How old are you?");
                  int age1 = input.nextInt();
                      if (age1 > 10) {
                          System.out.println("you can enter");
                              }else {
             System.out.println("you are too young!");
                              }
         
         
         }else {
             System.out.println("How old are you");
            }
         
         
         //if (boy > 10) {
            // System.out.println("you can enter");
         //}else{
             //System.out.println("you are too young!");
         }
     }

 

this code always gives the else (the one without the ?) even when boy is entered

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Oryzaki said:

-snip-

Here's a good debugging tip. If you're debugging using an IDE, it should list all your variables and their values. Or, you can have a bunch of temporary print statements to see what your:

input.next()

is really generating. Also remember to use the CODE tabs when posting code snippets. This is under the <> icon in the post editor.

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

First of all, for pasting code please use code tags. You can find them in the edit post bar marked by <> icon. It will make it easier for people to read the code and edit it if neede.

 

Now to your problem:

Instead of using s1 == "boy" use s1.equals("boy")

 

"==" operator checks whether references to s1 and "boy" are equal. ".equals" method checks actual contents. 

 

You can read about it more here:

https://stackoverflow.com/questions/767372/java-string-equals-versus

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

One of my friends figured it out for me the problem is I used == rather than  .equals 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, zwirek2201 said:

First of all, for pasting code please use code tags. You can find them in the edit post bar marked by <> icon.

 

Now to your problem:

Instead of using s1 == "boy" use s1.equals("boy")

 

"==" operator checks whether references to s1 and "boy" are equal. ".equals" method checks actual contents. 

 

You can read about it more here:

https://stackoverflow.com/questions/767372/java-string-equals-versus

thank you

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×