0

I am taking a course and I have the programs run in a loop so you can easily exit by entering "Quit". I am running into trouble working with arrays. This has the user type in sentences and then at the end shows the user what they typed. I want to have the program check each input the user types in and if it is "Quit", I want to exit the program. I am new to Java so looking for something that is within my understanding without using a break if possible.

I have attempted to use a boolean in my while loop to quit when it is set to false.

public static void main(String[] args)
{
    String [] Responses = new String [10];
    boolean ExitLoop = true;

    do  
    {
        Scanner Input = new Scanner(System.in);
        int n = 0;

        for (int i = 0; i < 10; i++)
        {
            System.out.println("Please enter sentence " + (i+1) + ": ");

            Responses[n] = Input.nextLine();
            if (Responses[n] == "Quit")
            {
                ExitLoop = false;
            }

            n++;
        }

        System.out.println();

        for (int j = 0; j < 10; j++)
        {
            System.out.println("Sentence " + (j+1) + " " + Responses[j]);
        }

   }
    while (ExitLoop);
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user121582
  • 49
  • 1
  • 1
  • 4
  • 3
    On a sidenote, see [comparing strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – SqueezyMo Aug 15 '15 at 21:20
  • i think better way is throwing exception. `System.exit()` is bad way – Andrew Tobilko Aug 15 '15 at 21:45
  • @AndrewTobilko Why would you ever throw an exception when nothing is wrong? If the application is supposed to be exiting at that time, there is no need for an exception. – Matt Rowland Aug 15 '15 at 22:53
  • Hi @user121582 if any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Also, if no answer has helped you, think about adding a comment to signal for more assistance. – Matt Rowland Aug 16 '15 at 22:35

3 Answers3

0

To exit the application you can call the following line from anywhere.

System.exit(0);

Integrated into you code it would look like this.

for (int i = 0; i < 10; i++)
{
    System.out.println("Please enter sentence " + (i+1) + ": ");

    Responses[n] = Input.nextLine();
    if (Responses[n].equals("Quit"))
    {
        System.exit(0); //add this to exit the application
    }

    n++;
}

If you wanted to keep separation of concerns you could make a method that exits the application.

public void ExitApplication()
{
    //you can add pre-exit checks and other items here
    System.exit(0);
}

Then you could simply call the method from inside you loop.

if (Responses[n].equals("Quit"))
{
    ExitApplication();
}
Matt Rowland
  • 4,575
  • 4
  • 25
  • 34
0

The '==' operator only checks if the strings refer to the same memory location, which they do not. Try using the string.equals() method for comparing the actual string values.

if (Responses[n].equals("Quit")) {
    ExitLoop = false;
}
Alexey Nikitenko
  • 2,047
  • 2
  • 20
  • 30
Jon Snow
  • 230
  • 1
  • 9
0

You can use one variable for iteration, and you need to use equals for comparing string. Try that:

public static void main(String[] args) {    
    final int countSentences = 10;
    final String[] sentences = new String[countSentences];
    final Scanner scanner = new Scanner(System.in);

    for (int i = 0; i < countSentences; i++) {    
        System.out.println("Please enter sentence "+(i+1)+": ");    
        sentences[i] = scanner.nextLine();
        if (sentences[i].equals("Quit")) System.exit(0);
    }

    System.out.println();

    for (int j = 0; j < countSentences; j++)
        System.out.println("Sentence "+(j+1)+" "+sentences[j]);
}          
Alexey Nikitenko
  • 2,047
  • 2
  • 20
  • 30