-2

I have a problem with my java programs when I use if statements. In fact the programs don't run well and completely as I write. To tell you the truth I'm really confused about it because I don't know where is the problem from...

For example I've written these codes but when I run them, I can't get the city codes:

import java.util.Scanner;
public class Code {

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

        System.out.println("Give the name, Get the code!\n* * * * * * * * * * * * * * *\n");
        System.out.print("Please enter the name of the country : ");
        String countryInput = sc.next().toLowerCase();
        System.out.print("Please enter the name of the city : ");
        String cityInput = sc.next().toLowerCase(); 
        System.out.println("\n-----------------------------");

        switch(countryInput) {
            case "iran" :
                System.out.print("+98 ");
                break;
            case "us" :
                System.out.print("+1 ");
                break; 
                default: System.out.print("ERROR: the entered country doesn't exist in program database!"); 
        }

        if(countryInput=="iran") {
            switch(cityInput) {
                case "tabriz" :
                    System.out.print("411");
                    break;
                case "tehran" :
                    System.out.print("21");
                    break;
                case "mashhad" :
                    System.out.print("511");
                    break;
                    default : System.out.print("\nERROR: the entered city doesn't exist in program database!"); 
            }
        }

        if(countryInput=="us") {
            System.out.print("(Sorry, the US cities aren't avalable!)");
        }
        System.out.print("\n-----------------------------\nProgrammer: user3891619");

    }
}

And also this one. The program never says "Password is wrong" even when is necessary:

  import java.util.Scanner;
public class Pass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int pass = 1234 ;
        System.out.print("Please enter your password: ");
        if(sc.hasNextInt() && sc.nextInt() == pass) {
            System.out.println("The password was successfully entered."); 
        }
        else { 
            if(sc.hasNextInt()) {
                System.out.println("The password is wrong.");
            }
            else {
                System.out.println("Password format is incorrect.");
            }
        }
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 2
    String is an object, and you should use the `.equals()` method to compare two strings, not `==` – Ben Green Jul 31 '14 at 10:10
  • `==` also works fine in Java – prince Jul 31 '14 at 10:12
  • Debug your code and try to see what is read by your scanner. – Jean Logeart Jul 31 '14 at 10:12
  • 3
    @prince sure it does but NOT FOR STRINGS and objects if you actually want to compare them by **content** and not by **reference**. – EpicPandaForce Jul 31 '14 at 10:15
  • @prince `==` does not works `fine` when you need to compare content. – Sanjeev Jul 31 '14 at 10:18
  • See this question - [how-do-i-compare-strings-in-java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Chris Nevill Jul 31 '14 at 10:19
  • You might be interested in this [java debugging tutorial](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/) I wrote. It's lightweight and short. – keyser Jul 31 '14 at 10:21
  • @Zhuinden I do agree with you, but I've tested a sample application with strings and it worked for me. – prince Jul 31 '14 at 10:57
  • @prince of course it did, because Java caches string literals, but not dynamic data. So when you write `public static void main(String[] args) { String hello = "Hello!"; if(hello == "Hello!") { System.out.println("Such cache, much wow"); } }` of course it will work in this specific case, but it will NOT work in any more complex setup for example reading the line from user input. – EpicPandaForce Jul 31 '14 at 11:03
  • @Zhuinden ok, I understood. I tried the same example which you showed just now. Thank you!! – prince Jul 31 '14 at 11:09

3 Answers3

1

Your post has 2 questions.

For the second one it's easy. When you call sc.hasNextInt() it verifies if there is an integer in the next section. Calling sc.nextInt() will read this integer and put the read cursor behind it. If you again call hasNextInt() it will verify if there is a second integer value which is not the case in your situation.

Your if structure should be:

if (sc.hasNextInt())
{
    if (sc.nextInt()==pass)
        System.out.println("The password was successfully entered."); 
    else
        System.out.println("The password is wrong.");
} else {
    System.out.println("Password format is incorrect.");
}
Conffusion
  • 4,335
  • 2
  • 16
  • 28
0

Try with:

while (sc.hasNextInt(){

 if(sc.nextInt().equals(pass)) {
        System.out.println("The password was successfully entered."); }
    else{ 
        if(sc.hasNextInt()){System.out.println("The password is wrong.");}
        else{System.out.println("Password format is incorrect.");}
    }}

You have to use .equals for Strings instead of ==

sharkbait
  • 2,980
  • 16
  • 51
  • 89
0

Yes Ben Green is right. Please use .equals when comparing strings.

countryInput.equals("something")

Please read this to find your answer.

What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
John Hogan
  • 996
  • 1
  • 13
  • 26