Blastoise
Blastoise

Reputation: 13

How can I do the input loop that exits the program?

"Enter a word or press 'Q' to quit" but I don't know how to do it. It seems confusing to me a little bit.

This is my first time coding in Java and I'm still learning.


public class RemSpecialChar {
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {

            String stringArray = "";
            
            do {
                System.out.print("Enter a word or 'Q' to quit: ");
                String str = scan.nextLine();
                for (int i = 0; i < str.length(); i++) {
                    if (str.charAt(i) > 64 && str.charAt(i) <= 122) { //returns true if both conditions returns true    
                        //adding characters into empty string
                        stringArray = stringArray + str.charAt(i);
                    }
                    System.out.print("Input string without special characters: " + stringArray); //string results
                }
            }
            while (stringArray != "q" || stringArray != "Q");
        }
    }
}


THIS IS THE SAMPLE OUTPUT:
Enter a word or 'Q' to quit: Black?204123,.Scoop (input)
Input string without special characters: BlackScoop (output)
Enter a word or 'Q' to quit: q (input)
(end program)


terminal output:
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit: q
Enter a word or 'Q' to quit:

Upvotes: 1

Views: 104

Answers (3)

divyang4481
divyang4481

Reputation: 1783


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        try (Scanner scan = new Scanner(System.in)) {
            while (true) {
                String inputdata = scan.nextLine();

                if (inputdata.length() == 1 && (inputdata.charAt(0) == 'Q' || inputdata.charAt(0) == 'q')) {
                    break;
                }

                String stringArray = "";
                for (int i = 0; i < inputdata.length(); i++) {
                    if (inputdata.charAt(i) > 64 && inputdata.charAt(i) <= 122) { // returns true if both conditions
                                                                                  // returns true
                        // adding characters into empty string
                        stringArray = stringArray + inputdata.charAt(i);
                    }

                }
                System.out.print("Input string without special characters: " + stringArray);

            }

        }
    }
}

Upvotes: 0

Melron
Melron

Reputation: 579

Use a while loop to read the inputs and break the loop if the input is 'Q'.

public static void main(String[] args) 
{
    try(Scanner sc = new Scanner(System.in))
    {
        while(true)
        {
            System.out.print("Enter a word or press 'Q' to quit: ");
            String word = sc.nextLine();
            
            if ("q".equalsIgnoreCase(word))
                break;
            
            System.out.println(word);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    System.out.println("Exiting... ");
}

Upvotes: 1

Possseidon
Possseidon

Reputation: 582

Take a look at this line:

while (stringArray != "q" || stringArray != "Q")

This repeats while stringArray is not "q" or not "Q". In other words, it must be both "q" and "Q" at the same time for it to exit, which is not possible.

What you actually wanted is to use &&:

while (stringArray != "q" && stringArray != "Q")

Repeat while not "q" and also not "Q".

While this is the root of your problem, I'll still advice you to use the answer that you accepted already.

Edit: As commented, you also shouldn't be comparing strings with == or !=.

while (!"q".equals(stringArray) && !"Q".equals(stringArray))

And you might as well use equalsIgnoreCase like in the other answer then. I simply wanted to point out the main flaw in the logic of using || instead of && here.

Upvotes: 0

Related Questions