“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.
JavaScript
x
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:
Advertisement
Answer
JavaScript
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);
}
}
}
}