3

Newbie question, but I have this code:

import java.util.*;
import java.io.*;

public class Welcome1  
{
   // main method begins execution of Java application
   public static void main( String[] args )
   {
      String confirm = "y";
      while(confirm=="y")
      {
         System.out.println( "Welcome to Java Programming!" );
         System.out.println( "Print Again? (y/n)" );
         Scanner input = new Scanner(System.in);
         confirm = input.nextLine();
      }
   }
}

I just need to simply print the welcome message again when user input "y" when asked. But it's not working. Any ideas?

Benjamin
  • 2,960
  • 2
  • 26
  • 39
Rendy
  • 103
  • 1
  • 7
  • How is it not working? Does it hang? Does the program terminate abnormally? Or something else? – Ingo Jan 02 '12 at 16:19
  • Use any IDE, doesn't matter so far whether Netbeans or Eclipse, because at compile-time it shows you Warnings and Errors that points what might go wrong. This time it showed Warning: "Comparing strings using == or !=" – Benjamin Jan 02 '12 at 16:22
  • 3
    Better would be to use a `do-while` loop here, since the loop has to be run at least once. Also note that the `Scanner` can be created outside of the loop, now we're using a new instance for every line, which is not necessary. – Joost Jan 02 '12 at 16:36

4 Answers4

11

In Java, the primitive types (int, long, boolean, etc.) are compared for equality using ==, whereas the Object types (String, etc.) are compared for equality using the equals() method. If you compare two Objects types using ==, you're checking for identity, not equality - that is, you'd be verifying if the two objects share exactly the same reference in memory (and hence are the same object); and in general, what you need is just verifying if their values are the same, and for that you use equals().

As a good programming practice, it's better to compare Strings like this, flipping the order of the strings:

while ("y".equals(confirm)) {

In this way, you can be sure that the comparison will work, even if confirm was null, avoiding a potential NullPointerException.

Óscar López
  • 225,348
  • 35
  • 301
  • 374
  • The reason that it works once is that the two `"y"` string objects are actually the same in memory, since they refer to the same string in the binary. The compiler stores equal strings only once and thus the first time the comparison is made, the references match and the while-body is executed. – Joost Jan 02 '12 at 16:41
6

For string comparison, use .equals().

while(confirm.equals("y")){
Christian Neverdal
  • 5,205
  • 6
  • 34
  • 88
5

you should use equals() instead of operator==.

the operator== checks if the two object are actually the same objects, while you want to check if they are equal.

code snap:

while(confirm.equals("y")) {
amit
  • 172,148
  • 26
  • 225
  • 324
1

Rewrite your code as follows (just an example):

import java.util.*;
import java.io.*;

public class Welcome1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String confirm = "y";

        do {
            System.out.println("Welcome to Java Programming!");
            System.out.println("Print Again? (y/n)");
            confirm = input.nextLine();
        }
        while (confirm.equalsIgnoreCase("y"));
    }
}
Nico
  • 3,431
  • 2
  • 28
  • 40