0

Here is my code.

import java.util.Scanner;

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

    String input;
    char medium1;
    double distance;

    Scanner medium = new Scanner(System.in);//Scanner for user input of the type of medium
    Scanner traveled = new Scanner(System.in);//Scanner for the distance traveled

    System.out.print("Please enter the type of medium (Air, Water, Steel):");
    input = medium.next().toLowerCase();

    //this was to make sure it was changing it to lower case
    System.out.println(input);

    if (input == "air"||input == "water"||input == "steel"){

        //used to get the first character of the input they had to match the switch case
    medium1 = input.charAt(0);

    switch (medium1)
    {
    case 'a':
        System.out.println("Please enter the distance here:");
        break;
    case 'w':
        System.out.println("Please enter the distance here:");
        break;
    case 's':
        System.out.println("Please enter the distance here:");
        break;


    }

    }
    else{
        System.out.println("Incorrect type of medium entered.");
    }
    }
}

What I want to do is make it so that when a user inputs something; if it doesn't match the if statement, then it just prints the else statement. The only issue is that it always prints out the else statement no matter what.

As long as the input equals air, water, or steel I want it to run the switch statements in between. That is why the charAt is there. Any ideas on how to accomplish this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Zoe Feb 10 '19 at 13:56

2 Answers2

2
   if ("air".equals(input)||"water".equals(input)|| "steel".equals(input)){ 

String comparison in Java should be equals, not ==

kosa
  • 65,990
  • 13
  • 130
  • 167
0

Don't use ==.

if (input.equals("air") || ...) {

or, with a static java.util.Set<String>:

if (valid.contains(input)) {
Community
  • 1
  • 1
Julian Fondren
  • 5,459
  • 17
  • 29