4 Replies - 755 Views - Last Post: 20 December 2013 - 10:20 PM Rate Topic: -----

#1 A Riddler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-November 13

Java program with if else and scanner

Posted 20 December 2013 - 04:15 PM

I'm making a program that takes user input for location and returns a recommended site to go see. I am using if else statements and returning recommended sites based on correct user input. I want to know if I have to write and else if statement for every single location and return a site or if there is a more simple method.

Here is a sample of my code

public static void main(String [] args){
	
	Scanner location = new Scanner(System.in);
	System.out.println("Where will you be traveling to?");
	String Country = location.nextLine();
	
	if (Country.equals("New York"))
		System.out.println("You should see the statue of Liberty");
	else if (Country.equals("Virginia"))
		System.out.println("You should visit the Washington Monument");
	else 
		System.out.println("Area Unknown");
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java program with if else and scanner

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,135
  • Joined: 20-September 08

Re: Java program with if else and scanner

Posted 20 December 2013 - 04:21 PM

Well you could simply have a Map<String, String> and look up the site based on location
Was This Post Helpful? 1
  • +
  • -

#3 A Riddler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-November 13

Re: Java program with if else and scanner

Posted 20 December 2013 - 04:51 PM

View Postg00se, on 20 December 2013 - 04:21 PM, said:

Map<String, String>



Is that suppose to be an array? Thanks for your response
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Java program with if else and scanner

Posted 20 December 2013 - 05:19 PM

A Map is essentially a dictionary of key,value pairs.
Was This Post Helpful? 1
  • +
  • -

#5 A Riddler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-November 13

Re: Java program with if else and scanner

Posted 20 December 2013 - 10:20 PM

View Postandrewsw, on 20 December 2013 - 05:19 PM, said:

A Map is essentially a dictionary of key,value pairs.



Thanks Guys I got it to work after learning how to use Java HashMap its slightly quicker than my previous method and much cleaner
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1