-1

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"); 
}
}
  • 5
    The problem is `==` doesn't work on strings. You already did it correctly with `name.equals("Dean")`, this is how it should be with the rest as well. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for more info. – cegredev Sep 26 '21 at 18:04

3 Answers3

0

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

el07
  • 65
  • 1
  • 5
0

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.

0

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");
    }

  }
Julian Kreuzer
  • 358
  • 1
  • 9
  • You managed to paste all poor formatting of the initial code snipper. – Nowhere Man Sep 26 '21 at 18:59
  • Also, nested `switch` statements seem to be redundant, they can be replaced with a single formatted print: `System.out.printf("Which player wears number %d on his jersey?%n", scanner.nextInt());` or `System.out.printf("Which number does %s wear?%n", scanner.next());` – Nowhere Man Sep 26 '21 at 19:04