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.");
}
}
}
}