Home > database >  How to fix if statement? [duplicate]
How to fix if statement? [duplicate]

Time:09-27

I am new to coding and am struggling to understand why this code is not working. I am trying to make a simple if " name = String" statement but have been having many problems. Please take a look and help me understand what is wrong. The problems are the if name statements at the bottom. Thanks

import java.util.Scanner;

public class MarylandBaseball {

public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    System.out.print( "Type 1 to enter a number or 2 to enter a name: " ) ;
    int number = scanner.nextInt() ;

    if (number == 1)
    System.out.print("Enter player number: ");
    if (number == 2)
    System.out.print("Enter player name: ");
    int jersey = scanner.nextInt() ;
    String name = scanner.nextLine();
    
    if (jersey == 42) System.out.print
    ("Which player wears number 42 on his jersey?"); 
    if (jersey == 11) System.out.print
    ("Which player wears number 11 on his jersey?"); 
    if (jersey == 6) System.out.print
    ("Which player wears number 6 on his jersey?"); 
    if (jersey == 4) System.out.print
    ("Which player wears number 4 on his jersey?"); 
    
    
    
    if (name.equals ("Dean")) { System.out.print
    ("What number does Dean wear?");
    }; 
    if (name == "Alleyne") System.out.print
    ("What number does Alleyne wear"); 
    if (name == "Shaw") System.out.print
    ("What number does Shaw wear"); 
    if (name == "Costes") System.out.print
    ("What number does Costes wear"); 
}
}

CodePudding user response:

Java does not work for string to string comparision with == symbol. You should use "equals" to compare like you did in the first if.

CodePudding user response:

You see, in java strings are immutable objects, the JVM can optimize the amount of memory allocated for them by storing only one copy of each literal String in the pool called string pool. This process is called interning.

When we create a String variable and assign a value to it, the JVM searches the pool for a String of equal value.

If found, the Java compiler will simply return a reference to its memory address, without allocating additional memory.

Hense whenever you are using '==' to check the equality, it will check for string reference in the string pool. So if you want to check their values, you need to use equals() method.

CodePudding user response:

in your example we use the switch instead of thousands of if statements, in java you should always use .equals method for objects. Only primitives like int double char etc. are "==" safe.

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.print( "Type 1 to enter a number or 2 to enter a name: " ) ;
    int number = scanner.nextInt() ;
    System.out.print("Enter player ");
    switch (number){
      case 1:
        System.out.println("number: ");
        switch (scanner.nextInt()){
          case 42: System.out.print
              ("Which player wears number 42 on his jersey?");
          break;
          case 11: System.out.print
              ("Which player wears number 11 on his jersey?");
          break;
          case 6: System.out.print
              ("Which player wears number 6 on his jersey?");
          break;
          case 4: System.out.print("Which player wears number 4 on his jersey?");
          break;
        }
        break;
      case 2:
        System.out.println("name: ");
        switch (scanner.nextLine()){
          case "Dean": System.out.print
              ("What number does Dean wear?");
          break;
          case "Alleyne":
            System.out.print
                ("What number does Alleyne wear");
            break;
          case "Shaw":
            System.out.print
                ("What number does Shaw wear");
            break;
          case "Costes":
            System.out.print
                ("What number does Costes wear");
            break;
        }
        break;
      default: throw new IllegalArgumentException("invalid input");
    }

  }
  •  Tags:  
  • java
  • Related