Search code examples
javamultidimensional-arraywhile-loopsentinel

Java - Issue with Comparing Strings for Sentinel Values


Having a simple issue with my while loop and sentinel value not registering. I have come to the conclusion that my issue lies with my while loop, or maybe the assignment of user input. The basic idea of my program is that I have a 2D array with String values. I am trying to access those based on a user input. I ask what state the user wants and then my program will return the state, state bird, and state flower. In the main class, I need to ask for user input, use a while loop, and a sentinel value that I chose to be "none". I want the user to be able to continue asking for states until they enter none into the command line. I have included comments in my code to help try and understand what I am trying to do. the questions I have are: Why would my sentinel value not work? Even when I input none, the loop continues forever. Why does my code not return the value of output even when giving the program a legitimate input like "Texas"?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class StateInfo{
  private int counter;
  private int i;
  private int summary[];
  public String output;
  private String userInput;

  // 50 states, each index with 3 entries (state, bird, flower)
  //String[][] stateInfo = new String[50][3];
  String stateInfo[][] = {
                            {"Alabama", "Camellia", "Yellowhammer"},
                            {"Alaska", "Alpine Forget-me-not", "Willow Ptarmigan"},
                            {"Arizona", "Saguaro Cactus Blossom", "Cactus Wren"},
                            {"Arkansas", "Apple Blossom", "Mockingbird"},
                            {"California", "California Poppy", "California Quail"},
                            {"Colorado", "Rocky Mountain Columbine", "Lark Bunting"},
                            {"Connecticut", "Mountain Laurel", "American Robin"},
                            {"Delaware", "Peach Blossom", "Blue Hen Chicken"},
                            {"Florida", "Orange Blossom", "Northern Mockingbird"},
                            {"Georgia", "Cherokee Rose", "Brown Thrasher"},
                            {"Hawaii", "Pua Aloalo", "Hawaiian Goose"},
                            {"Idaho", "Syringa", "  Mountain Bluebird"},
                            {"Illinois", "Violet", "Northern Cardinal"},
                            {"Indiana", "Peony", "Northern Cardinal"},
                            {"Iowa", "Wild Rose", "   Eastern Goldfinch"},
                            {"Kansas", "Wild Native Sunflower", "Western Meadowlark"},
                            {"Kentucky", "Goldenrod", "Northern Cardinal"},
                            {"Louisiana", "Louisiana Iris", "Brown Pelican"},
                            {"Maine", "White Pine Cone and Tassel", "Black-capped Chickadee"},
                            {"Maryland", "Black-Eyed Susan","Baltimore Oriole"},
                            {"Massachussets", "Mayflower", "Black-capped Chickadee"},
                            {"Michigan", "Dwarf Lake Iris", "American Robin"},
                            {"Minnesota", "Pink & White Lady Slipper", "Common Loon"},
                            {"Mississippi", "Coreopsis", "Northern Mockingbird"},
                            {"Missouri", "White Hawthorn Blossom", "Eastern Bluebird"},
                            {"Montana", "Bitterroot", "Western Meadowlark"},
                            {"Nebraska", "Goldenrod", "Western Meadowlark"},
                            {"Nevada", "Sagebrush", "Mountain Bluebird"},
                            {"New Hampshire", "Pink Lady's Slipper", "Purple Finch"},
                            {"New Jersey", "Violet", "Eastern Goldfinch"},
                            {"New Mexico", "Yucca", "Roadrunner"},
                            {"New York", "Rose", "Eastern Bluebird"},
                            {"North Carolina", "Dogwood","Northern Cardinal"},
                            {"North Dakota", "Wild Prairie Rose", "Western Meadowlark"},
                            {"Ohio", "White Trillium", "Northern Cardinal"},
                            {"Oklahoma", "Mistletoe", "Scissor-tailed Flycatcher"},
                            {"Oregon", "Oregon Grape", "Western Meadowlark"},
                            {"Pennsylvania", "Mountain Laurel", "Ruffed Grouse"},
                            {"Rhode Island", "Violet", "Rhode Island Red Chicken"},
                            {"South Carolina", "Yellow Jessamine", "Carolina Wren"},
                            {"South Dakota", "American Pasque", "Ring-necked Pheasant"},
                            {"Tennessee", "Iris", "Northern Mockingbird"},
                            {"Texas", "Bluebonnet", "Northern Mockingbird"},
                            {"Utah", "Sego Lily", "California Gull"},
                            {"Vermont", "Red Clover", "Hermit Thrush"},
                            {"Virginia", "American Dogwood", "Northern Cardinal"},
                            {"Washington", "Coast Rhododendron", "Willow Goldfinch"},
                            {"West Virginia", "Rhododendron", "Northern Cardinal"},
                            {"Wisconsin", "Wood Violet", "American Robin"},
                            {"Wyoming", "Indian Paintbrush", "Western Meadowlark"},
                           };

  public void findState() {
    for (i = 0; i < 50; i++){
        if (userInput == stateInfo[i][0]){
          output = "State: " + stateInfo [i][0] + "/n" + "Flower: " + stateInfo [i][1] + "/n" + "Bird: " + stateInfo [i][2];
        }
  }
}

public void setUserInput(String userInput) {
       this.userInput = userInput;
    }

public String getOutput() {
       return output;
    }
}

class Main extends StateInfo{
    public static void main(String[] args) throws IOException{
      StateInfo newState = new StateInfo();
      BufferedReader myReader = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter a State or None to exit:");
      //assigns user input to a variable for later use
      String input = myReader.readLine(); 

          //while loop to check the value of input and will not be entered if input = "none"
          while(input != "none"){
            //assigns the value of input to userInput from a newState instance
            newState.setUserInput(input);

            //trys to call the findState method. Find state method takes userInput (which should be = to input) and builds a string based on indexes from the 2d array
            newState.findState();

            //getter method for output. I want to get the new string that I built for output
            newState.getOutput();

            //ending first iteration of the while loop, begins another with a new assignment of input. If input = none, while loop exits and prints thank you to the screen
            System.out.println("Enter a State or None to exit:");
            input = myReader.readLine();
          }
          System.out.println("***** Thank you *****");
        }
}

Solution

  • while(input != "none") compares the memory address of input and of the string "none" and will always return false. If you want to check the value of the input variable, try while(!input.equals("none"))