-1

I am trying to code a program that allows to enter various words(step by step) until one types in "quit" . I am having trouble stopping the loop (even with the word quit typed, it doesn't stop) Using System.out.println(sum); I can check that the words are adding up, but it never stops.. ((Summary : if(string == "quit") does not work and for (String end = "quit"; string!=end;) does not work )) Sorry if its hard to read. its my second day coding :(

public static void main(String[] args) {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    String sum = "";
    System.out.println("Type in a word");
    String string = scanner.nextLine();
    if (string == "quit")
    {System.out.println("Ending system");

    }
    else{
    for(String end = "quit"; string!=end;  )
    {
        sum = sum + " " + string;
        System.out.println(sum);
        System.out.println("Type in another word");
        String stringextra = scanner.nextLine();
        if(stringextra == "quit"){break;}
        string = stringextra;

    }

    scanner.close();
    System.out.println("Stopping... due to the word quit");
    System.out.println("all the words typed are " + sum);

    }

}}
elmigue017
  • 343
  • 2
  • 12

5 Answers5

0

Strings string and "quit" are not the same objects(as stored in the jvm) yet equal so checking with == will not work.

You need to use: string.equals("quit")

see Object.equals() for more information

Giorgos Gaganis
  • 635
  • 7
  • 8
0

As pointed out in the other answer you need to use .equals() to compare strings. Also, your for loop is incorrect. A while loop is what is normally used in this case:

public class end_on_quit {

   public static void main(String[] args) 
   {
      java.util.Scanner scanner = new java.util.Scanner(System.in);
      String sum = "";
      System.out.println("Type in a word");
      String string = scanner.nextLine();
      while ( ! string.equals("quit") )
      {
         sum = sum + " " + string;
         System.out.println(sum);
         System.out.println("Type in another word");
         string = scanner.nextLine();
       }
       System.out.println("Ending system");
       scanner.close();
       System.out.println("Stopping... due to the word quit");
       System.out.println("all the words typed are " + sum);
    }
}
Scooter
  • 6,802
  • 8
  • 41
  • 64
0

Easiest way to doing something like your code using do while so your code running until user write quit. I will give you simple example :

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    String sum = "";
    System.out.println("Type in a word");
    String stringextra = scanner.nextLine();

    if (stringextra.equalsIgnoreCase("quit")) {
        System.out.println("Bye bye");
        System.exit(0);
    } else {
        do {
            sum = sum + " " + stringextra;
            System.out.println(sum);
            System.out.println("Type in another word");
            stringextra = scanner.nextLine();

        } while (!stringextra.equalsIgnoreCase("quit"));

    }
    scanner.close();
    System.out.println("Stopping... due to the word quit");
    System.out.println("all the words typed are " + sum);

}

I hope this help you, good luck.

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
0

You should use equalsto compare strings. In this case for you example is better use while instead of for to loop.

When you use equalsIgnoreCase it means that is no matter if the word is upper case or lower case (is the same).

The application finishes when the user types "quit or ends or QUIT or ENDS"

Example:

public static void main(String[] args) {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    String sum = "";
    System.out.println("Type in a word");
    String string = scanner.nextLine();

    while (!string.equalsIgnoreCase("quit")) {
        sum = sum + " " + string;
        System.out.println(sum);
        System.out.println("Type in another word");
        string = scanner.nextLine();

        if (string.equalsIgnoreCase("ends")) {
            string = "quit";
        }
    }

    System.out.println("Ending system");

    scanner.close();
    System.out.println("Stopping... due to the word quit");
    System.out.println("all the words typed are " + sum);
    System.exit(0);
}

Output:

Type in a word
hi
Type in another word
this
Type in another word
an
Type in another word
example
Type in another word
ENDS
Ending system
Stopping... due to the word quit
all the words typed are  hi this an example

Process finished with exit code 0
elmigue017
  • 343
  • 2
  • 12
-3

Simply do System.exit(0) after System.out.println("Ending system");