0

I have a project that involves me to create a program that reads the user input and the program then tells them what zone they are in, but I cant seem to how to add multiple strings.

import java.util.*;

public class hello {

  public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();

    // I would like more stations to be added but I don't no how
    if ("Mile End".equals(answer)) { 
      System.out.println( input +" is in Zone 2");
    } else {
      System.out.println("That is not a Station, please try again");
    }
  }

}
1
  • 1
    This is not related to JavaScript. Commented Oct 22, 2015 at 12:16

4 Answers 4

1

It seems like you want a loop. One such option, would be to stop when the user enters a special "zone" (like quit below).

String answer = input.nextLine();
while (!answer.equalsIgnoreCase("quit")) {
    // I would like more stations to be added but I don't no how
    if ("Mile End".equals(answer)) { 
        System.out.println( input +" is in Zone 2");
    } else {
        System.out.println("That is not a Station, please try again. "
                + "Quit to stop.");
    }
    answer = input.nextLine();
}
Sign up to request clarification or add additional context in comments.

Comments

0

am not entirely sure what you mean by "but I cant seem to how to add multiple strings." but you seem to print the Scanner object " System.out.println( input +" is in Zone 2");" instead of the answer System.out.println( answer +" is in Zone 2"); Could it be because of this you are not seeing the expected result ?

public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();
    if (answer.equals("Mile End")) { // i would like more stations to be added but i dont no how
      System.out.println( answer +" is in Zone 2");
    } else {
      System.out.println("That is not a Station, please try again");
    }
  }

Comments

0

You may need an else if statement

import java.util.*;

public class hello {

  public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();

    if ("Mile End".equals(answer)) { 
      System.out.println( answer+" is in Zone 2");
    } else if("Hobbitland".equals(answer) {
      System.out.println( answer +" is in Zone 42");
    } else
      System.out.println("That is not a Station, please try again");
    }
  }
}

Alternatively you could use a switch like:

import java.util.*;

public class hello {

  public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();

    switch(answer){
      case "Mile End":
        System.out.println( answer +" is in Zone 2");
        break;
      case "Hobbitland":
        System.out.println( answer +" is in Zone 42");
        break;
      default:
        System.out.println("That is not a Station, please try again");
        break;
    }
  }
}

There are still other means to solve this problem without the need of such a complex control structure. Just create a Map that holds your station names as key and their zone as value. When you get an input you just look it up in your map and retrieve its zone. If it's not in your map you print your error message.

Comments

0

Why not create a map where the zone is the key and the value is a list of stations that come under that zone?

You can then have a method that handles the population of the map...

private static Map<String, List<String>> createZoneMap() {

    Map<String, List<String>> zoneMap = new HashMap<String, List<String>>();

    // Probably want to populate this map from a file

    return zoneMap;
}

Then your main can look something like...

public static void main(String args[]) {

    Map<String, List<String>> zoneMap = createZoneMap();

    Scanner scan = new Scanner(System.in);

    String input;

    while (true) {

        input = scan.nextLine();

        // Some code to exit the application...
        if (input.equalsIgnoreCase("quit")) {            
            System.out.println("Exiting...");
            System.exit(1);
        }

        String zone = findZone(zoneMap, input);

        if (zone != null) {
            System.out.println(input + " is in " + zone);
        } else {
            System.out.println("That is not a Station, please try again");
        }
    }
}

Then when you type in the station name you look through the map to find the zone which the station comes under, if its not present then return null or something

private static String findZone(Map<String, List<String>> zoneMap, String station) {

    // Maybe make this more versatile so that it does not care about case...

    for (Map.Entry<String, List<String>> entry : zoneMap.entrySet()) {
        if (entry.getValue().contains(station)) {
            return entry.getKey();
        }
    }
    return null;
}

Hope that's a good starting point for you. You could also consider moving away from performing all of your logic in the main method and instead create an instance of your class in the main.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.