0

I am new to Java. I'm trying to make a program where the user inputs a country and it returns the current time in that country. I have this code:

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

    System.out.println("Enter a country: ");
    String userCountryInput = userInput.nextLine();

    if (userInput.equals("Philippines")) {
        Date date1 = new Date();
        System.out.println(date1);
    }

    if (userInput.equals("Russia")) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));

        Date date2 = new Date();
        System.out.println(date2);
    }
}

When I enter "Russia" or "Philippines" it does not output anything. Any idea why?

Edd
  • 3,724
  • 3
  • 26
  • 33
  • asking for books will lead to a discussion, which isn't the aim on this site. In my opinion if Java is the first language, you're learning, you should choose a book in your mothertongue. The most books will cover the basic needs and after you got started, you will learn the most as you need it by reading in forums, apis or solving problems. – Sebastian Walla Jul 08 '15 at 11:57

3 Answers3

3

userCountryInput is the input variable. userInput is the Scanner variable used to get the input

if ("Philippines".equals(userCountryInput)) {
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

You are using equals on userInput wich is the scanner and not the string. It will work on the string userCountryInput :

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

    System.out.println("Enter a country: ");
    String userCountryInput = userInput.nextLine();

    if (userCountryInput .equals("Philippines")){

        Date date1 = new Date();
        System.out.println(date1);
    }

    if (userCountryInput .equals("Russia")){
        TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));

        Date date2 = new Date();
        System.out.println(date2);
    }
}
Pierre-Alexandre Moller
  • 2,354
  • 1
  • 20
  • 29
0

As stated by others, you are mixing up the userInput with userCountryInput.

You should use better names for your variables so you don't mix them up again in the future. For example, it might be worth to rename userCountryInput to userCountry.

Diyarbakir
  • 1,999
  • 18
  • 36