Scanner loop breaks + Regex issue - java

This task is whether a word is a palindrome, and I use java 11.
After entering of the word with spaces the code gives me an answer, but while loop breaks and the code "finishes with exit code 0" If I choose word = sc.next(). But If I write word = sc.nextLine() - It executes well and begins loop again.
I can't understand why, cause I can't debug this part of code, It's just skipped. And I don't understand why this happens. So can somebody explain to me what am I doing wrong?
Maybe there's a way to avoid this spaces?
P.S. I'm new to programming. Thanks to everyone.
try {
Pattern pattern = Pattern.compile("[Y|y][E|e][S|s]");
while (true) {
String word = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word without spaces");
if (sc.hasNext("(?>\\p{Alpha})+")) {
word = sc.next();
System.out.println(word);
System.out.println("The word is correct");
String text2;
StringBuilder sb = new StringBuilder();
text2 = sb.append(word).reverse().toString();
if (word.equalsIgnoreCase(text2)) {
System.out.println("Yes, it's a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
else {
System.out.println("No, it's not a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
else {
word = sc.next();
System.out.println("It's not a word");
System.out.println("Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
}
catch (NoSuchElementException ex) {
System.out.println(ex.getMessage());
}
}

What can you improve in your regex, [Y|y][E|e][S|s]?
Use [Yy][Ee][Ss] instead. The [] specifies a character class and all the characters inside it mean one of them i.e. by default they come with an implicit OR. Check this to learn more about it.
How can you determine if the input contains space?
Pass " " to the function, String#contains to determine it.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter a word without spaces: ");
String word = sc.nextLine();
if (word.contains(" ")) {
System.out.println("It's not a word");
} else {
System.out.println("The word is correct");
StringBuilder reverse = new StringBuilder(word).reverse();
System.out.println(word.equalsIgnoreCase(reverse.toString()) ? "Palindrome" : "Not a palindrome");
}
System.out.println("Want to try another one?");
System.out.print("Enter yes if you want to continue or any other symbol otherwise: ");
if (!sc.nextLine().matches("[Yy][Ee][Ss]")) {
break;
}
}
}
}
A sample run:
Enter a word without spaces: Hello World
It's not a word
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: yes
Enter a word without spaces: HELLO
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: YES
Enter a word without spaces: Malayalam
The word is correct
Palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: Yes
Enter a word without spaces: papa
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: n

Related

How to re-write an input loop to not contain code repetition?

I have the following code, which continues to ask the user to enter a letter as long as the letter is either "a" or "b":
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String letter;
System.out.print("Enter a letter: ");
letter = scan.nextLine();
while(letter.equals("a") || letter.equals("b"))
{
System.out.println("You entered: " + letter);
System.out.print("Enter a letter: ");
letter = scan.nextLine();
}
}
}
But the following code is repeated twice:
System.out.print("Enter a letter: ");
letter = scan.nextLine();
Is there a way to make the above code only appear one time?
while (true) {
System.out.print("Enter a letter: ");
String letter = scan.nextLine();
if (!letter.equals("a") && !letter.equals("b"))
break;
System.out.println("You entered: " + letter);
}
This is the classic example of a loop that is neither naturally while-do nor do-while — it needs to exit from the middle, if you want the same behavior and also to reduce code duplication.
(Notice also that the variable declaration letter has been moved to an inner scope since it is no longer needed in the outer scope.  This is a small positive indication.)
As an alternative to while (true) some languages allow degenerate for-loop as in for(;;).
The below reverses the logic of the conditional loop exit test, at the expense of more control flow logic.
while (true) {
System.out.print("Enter a letter: ");
String letter = scan.nextLine();
if (letter.equals("a") || letter.equals("b")) {
System.out.println("You entered: " + letter);
continue;
}
break;
}
(There is no difference between these in efficiency terms — these are equivalent at the level of machine code.)
do while loop
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String letter;
do{
System.out.print("Enter a letter: ");
letter = scan.nextLine();
System.out.println("You entered: " + letter);
}while(letter.equals("a") || letter.equals("b"));
}
}
It will loop once first, and then continue again if the statment is true.
You need to perform three sequential actions in a loop:
read the input entered by the user;
validate the input;
print the input, but only if it is valid.
That means that conditional logic must reside inside the loop before the statement that prints the input. And that makes the condition of the loop redundant. Instead, we can create an infinite loop with a break statement wrapped by a condition, that validates the input.
While loop
That's how it can be done using a while loop:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String letter;
while (true) {
System.out.print("Enter a letter: ");
letter = scan.nextLine();
if (!letter.matches("[ab]")) break;
System.out.println("You entered: " + letter);
}
}
Method matches() that expects a regular expression as argument is used in the code above to simplify the termination condition.
For more information on regular expressions, take a look at this tutorial
For loop
Regarding the advice of utilizing a for loop - that's doable, but by its nature the task of reading the user input fits better in the concept of while loop because we don't know the amount of data in advance, and the iteration can terminate at any point in time.
Also note syntax of the for statement consists of three parts separated with a semicolon:
initialization expression - allow to define and initialize variables that would be used in the loop;
termination expression - condition which terminates the execution of the loop;
increment expression - defines how variables would change at each iteration step.
All these parts are optional, but the two semicolons ; inside the parentheses always have to be present.
Because as I've said earlier, the input needs to be validated inside the loop, we can't take advantage from the termination expression and increment expression.
For the sake of completeness, below I've provided the version of how it can be achieved using a for loop:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
for (String letter = "a"; ;) {
System.out.print("Enter a letter: ");
letter = scan.nextLine();
if (!letter.matches("[ab]")) break;
System.out.println("You entered: " + letter);
}
}
The only advantage is that the scope of the variable letter was reduced. But approach of utilizing while loop is more readable and expressive.
Alternative approach
Another option is to preserve your initial structure of the code:
initialize the variable letter before the loop, at the same line where it is defined;
enter the loop if letter holds a valid input;
print the input and reassign the variable.
But in order to avoid duplication of the code line responsible for printing the prompt and reading the input will be extracted into a separate method.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String letter = readInput(scan);
while (letter.matches("[ab]")) {
System.out.println("You entered: " + letter);
letter = readInput(scan);
}
}
public static String readInput(Scanner scan) {
System.out.print("Enter a letter: ");
return scan.nextLine();
}
As Bobulous mentioned, a do-while loop is another simple solution. If duplicating the conditional is still a deal-breaker for you, though, you can also create a function that returns a boolean, and, when true, prints the extra text.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String letter;
do
{
System.out.print("Enter a letter: ");
letter = scan.nextLine();
} while(inputIsAOrB(letter));
}
public static boolean inputIsAOrB(String input) {
if (input.equals("a") || input.equals("b"))
{
System.out.println("You entered: " + input);
return true;
}
else
{
return false;
}
}
A while loop with a break after executing your second print command will limit the code to only asking for a single input.
Scanner ct = new Scanner(System.in);
String input;
System.out.print("Please enter either 'a' or 'b': ");
input = ct.nextLine();
while(input.equals("a") || input.equals("b")){
System.out.println("You entered: " + input);
break;
}
You can also view the problem as generating and processing a stream of strings. The side effects in the generator may trigger some philosophical discussions, otherwise I think it is quite clean:
Stream.generate(() -> {
System.out.print("Enter a letter: ");
return scan.nextLine();
})
.takeWhile(str -> str.equals("a") || str.equals("b"))
.forEach(str -> System.out.println("You entered: " + str));
... which will run like this:
Enter a letter: a
You entered: a
Enter a letter: b
You entered: b
Enter a letter: c
Process finished with exit code 0
Simply
List<Character> expectedChars = new ArrayList<>();
expectedChars.add('a');
expectedChars.add('b');
while(!expectedChars.contains(line = scan.nextLine())) {
System.out.println("Not expected");
}
// Now has a expected char. Proceed.
I without using any break or if-else externally (I am using ternary operator though) within control loop, you can also use below :
Scanner scanner = new Scanner(System.in);
boolean flag=true;
while (flag) {
System.out.println("enter letter");
String bv = scanner.nextLine();
flag=bv.matches("a|b");
System.out.println(flag?"you entered"+bv:' ');
}
with for loop, it can be even simpler :
Scanner scanner = new Scanner(System.in);
for (boolean flag = true; flag;) {
System.out.println("enter letter");
String bv = scanner.nextLine();
flag = bv.matches("a|b");
System.out.println(flag ? "you entered" + bv : ' ');
}
Or if you ok for having whitspace for first run:
Scanner scanner = new Scanner(System.in);
String aa=" ";
String bv=null;
for (boolean flag = true; flag;aa=(flag?"you entered: "+bv:" ")) {
System.out.println(aa);
System.out.println("enter letter");
bv = scanner.nextLine();
flag = bv.matches("a|b");
}
You can easily run a while loop until letter = "a" or letter = "b". Code will start the while loop with intial "" value of the letter and get the new value by scanner. Then it check the new value of the letter before starting the next round of the loop.
package com.company;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String letter = "";
while(!(letter.equals("a") || letter.equals("b")))
{
System.out.print("Enter a letter: ");
letter = scan.nextLine();
System.out.println("You entered: " + letter);
}
}
}
Similar as previous answer, but from a code readability perspective i would create an array of valid characters instead and use in condition. That results in the more "text like" condition below reading "if not validCharacters contains letter":
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
List<String> validCharacters = List.of("a", "b");
while (true) {
System.out.print("Enter a letter: ");
String letter = scan.nextLine();
if (!validCharacters.contains(letter)) {
break;
}
System.out.println("You entered: " + letter);
}
}
Your code just needed a little tweak to make it work, although you can use many more efficient approaches but here it is:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String letter = "a";
while(letter.equals("a") || letter.equals("b"))
{
System.out.print("Enter a letter: ");
letter = scan.nextLine();
System.out.println("You entered: " + letter);
}
}
}

User validation testing in Java while loop

I am making one of my first java projects checking if a user input word (no numbers or symbols) is a palindrome. I used the method outlined here: https://stackoverflow.com/a/4139065/10421526 for the logic. The code works per-request but I am having trouble re-prompting the user after invalid input as it seems to "block" the ability of my code to accept valid input, printing my "retry" message. I have also tried the do while version but I ended up with formatting errors. I think the way I define my stringOut variable is giving me trouble but Im not sure how to change that without making a duplicate as eclipse says. Here is the closest I could get after dozens of tries going in circles:
import java.util.Scanner;
public class PalindromTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Input word to test: ");
String stringIn = in.nextLine();
String stringOut = new StringBuilder(stringIn).reverse().toString();
while (!stringIn.matches("[a-zA-Z]+")) {
System.out.println("Invalid input, try again.");
in.next(); //stops infinite error loop
}
if ((stringIn.equalsIgnoreCase(stringOut))) {
System.out.println("Palindrome detected");
System.out.println("You entered: " + stringIn);
System.out.println("Your string reversed is: " + stringOut);
} else {
System.out.println("Not a palindrome");
}
}
}
change in.next(); in while loop to stringIn= in.next(); and after while loop add stringOut = new StringBuilder(stringIn).reverse().toString(); .
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Input word to test: ");
String stringIn = in.nextLine();
String stringOut = new StringBuilder(stringIn).reverse().toString();
while (!stringIn.matches("[a-zA-Z]+")) {
System.out.println("Invalid input, try again.");
stringIn= in.next(); //stops infinite error loop
}
stringOut = new StringBuilder(stringIn).reverse().toString();
if ((stringIn.equalsIgnoreCase(stringOut))) {
System.out.println("Palindrome detected");
System.out.println("You entered: " + stringIn);
System.out.println("Your string reversed is: " + stringOut);
} else {
System.out.println("Not a palindrome");
}
}
}
A very good use-case to use do-while loop. You use this loop when you have to make sure that your statements are executed at least once. And the subsequent execution is executed only if it matches a condition. In this case, that condition would be validating your input.
Scanner in = new Scanner(System.in);
String prompt = "Input word to test: ";
String stringIn;
do {
System.out.println(prompt);
stringIn = in.nextLine();
prompt = "Invalid input, try again.";
}
while (stringIn.matches("[a-zA-Z]+"));
If the input is non-numeric the while condition would be true and will make this loop run again, hence asking for new input. if the input is numeric the while condition will be false hence exit the while loop and will give you user input in stringIn variable.

replaceAll and loops?

My Computer Science class assignment requires that I write a program which determines if a word or phrase is a palindrome (is the same forward and backwards, ie "noon"). As part of this, I have to write a method which removes all punctuation and spaces, so they are not counted in determining if it is a palindrome. It also runs on a loop, allowing the user to input as many phrases they want until they indicate they're done. My problem is that when the word/phrase entered contains a space, somehow it terminates the loop and doesn't allow more input. The program works just fine, as long as the input has no spaces. Here's my code:
In class RecursivePalindrome:
public String removePunctuation(String s){
s = s.replaceAll("\\.","");
s = s.replaceAll("!","");
s = s.replaceAll(",","");
s = s.replaceAll(" ","");
s = s.replaceAll("'","");
s = s.replaceAll("-","");
s = s.replaceAll("\\?","");
return s;
}
public boolean isPalindrome(String s) {
s = removePunctuation(s);
String firstChar = s.substring(0,1);
String lastChar = s.substring(s.length()-1);
if (s.length() == 1){
return true;
}
if (s.length() == 2 && firstChar.equalsIgnoreCase(lastChar)){
return true;
}
if (!firstChar.equalsIgnoreCase(lastChar)){
return false;
}
return isPalindrome(s.substring(1, s.length() - 1));
}
In class RecursivePalindromeTester:
public static void main(String[]args){
//Create objects
Scanner in = new Scanner(System.in);
RecursivePalindrome palindrome = new RecursivePalindrome();
//Output
for (String again = "Y"; again.equalsIgnoreCase("Y"); again = in.next())
{
//Prompt for input
System.out.println();
System.out.print("Enter a word or phrase: ");
String phrase = in.next();
//Output
if (palindrome.isPalindrome(phrase)){
System.out.println("This is a palindrome.");
}
else
System.out.println("This is not a palindrome.");
System.out.print("Another word or phrase? (Y/N): ");
}
}
The output should be:
"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N): <input>Y
Enter a word or phrase: <input>Dog?
This is not a palindrome
Another word or phrase? (Y/N): <input>N"
Terminate
But instead I get:
"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N):"
Terminate
I really have no idea why a space would cause the loop to terminate, especially since it doesn't do this with any other punctuation.
Totally agreed with #Ilya Bursov comment,
You should use in.nextLine() instead of in.next() , there are big difference between both methods
next() can read the input only till the space. It can't read two words separated by a space. Also, next() places the cursor in the same line after reading the input.
nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line
Try like this ,
class RecursivePalindromeTester {
public static void main(String[] args) {
//Create objects
Scanner in = new Scanner(System.in);
RecursivePalindrome palindrome = new RecursivePalindrome();
//Output
for (String again = "Y"; again.equalsIgnoreCase("Y"); again = in.nextLine()) {
//Prompt for input
System.out.println();
System.out.print("Enter a word or phrase: ");
String phrase = in.nextLine();
//Output
if (palindrome.isPalindrome(phrase)) {
System.out.println("This is a palindrome.");
}
else
System.out.println("This is not a palindrome.");
System.out.print("Another word or phrase? (Y/N): ");
}
}
}

How to stop storing integers and special characters in String in Java?

I wrote a program to read username from keyboard. When I enter any integer or special characters, it is taking that values and displaying on console. But I want that it should not take any integers and special characters. It should take only letters and if any integer or special character is there, then it should give the error message and should not store that value. Can anybody help me with this problem?
The program program which I wrote is
import java.util.Scanner;
public class CheckIsEmpty {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter User Name:: ");
System.out.println();
String usn = sc.nextLine();
if (usn.trim().isEmpty()) {
System.out.println("Don't Give Space");
System.out.println();
}//if
else if (usn.isEmpty()) {
System.out.println("User Name Is Mandatory");
System.out.println();
} // if
else {
System.out.println("Hi " + usn);
System.out.println("Welcome To Java");
break;
}// else
}//while
}//main
}// class
You can use regex here. If all characters are letters then following code will return true.
usn.matches("[a-zA-Z]+")
If an input string is having any other char it will return false.
Hope it helps.
You can use pattern matching..
boolean b = Pattern.compile("[a-zA-Z]+").matcher(username).matches();

How to keep a string input within a loop

I'm making a program that takes a string from the user and separates it word by word with a delimiter, and it's almost complete. However, after the first full loop, the next input from the user doesn't pass through the last while loop.
Here's the segment of code I'm talking about:
do
{
System.out.println ("\nEntered String: " + s1 + "\n");
while (input.hasNext())
{
word++;
System.out.println ("Word #" + word + ": \t" + input.next());
}
System.out.print ("\nEnter 'q' to quit, enter string to continue: \t");
s1 = scan.nextLine();
} while (!s1.equals("q"));
I'm thinking that I need another while loop around the word increment and print line and have the continue sequence within the input.hasNext() loop, because that's how I got a similar program using int to work, but I'm not sure how that would work with strings.
Any advice?
EDIT: to clarify, right now the output of my code looks like this:
Enter a sentence: this is a sentence
Entered String: this is a sentence
Word #1: this
Word #2: is
Word #3: a
Word #4: sentence
Enter 'q' to quit, enter string to continue: another sentence
Entered String: another sentence
Enter 'q' to quit, enter string to continue:
I need 'another sentence' to print out like 'this is a sentence'
I do not understand exactly what's the problem since your code does not compile. But there is no need for another loop. Here is a bit of code that works:
Scanner scan = new Scanner(System.in);
System.out.print("\nEnter a sentence:");
String s1 = scan.nextLine();
do
{
System.out.println("\nEntered String: " + s1 + "\n");
Scanner input = new Scanner(s1);
int word = 0;
while (input.hasNext())
{
word++;
System.out.println("Word #" + word + ": \t" + input.next());
}
System.out.print("\nEnter 'q' to quit, enter string to continue: \t");
s1 = scan.nextLine();
} while(!s1.equals("q"));
scan.close();
You can try this if it works. It has the same results with the one you need.
Scanner scan = new Scanner(System.in);
String s1 = scan.nextLine();
do {
String input[] = s1.split(" ");
System.out.println ("\nEntered String: " + s1 + "\n");
for(int i = 0; i < input.length; i++) {
System.out.println ("Word #" + i+1 + ": \t" + input[i]);
}
System.out.print ("\nEnter 'q' to quit, enter string to continue: \t");
s1 = scan.nextLine();
} while (!s1.equals("q"));
You are using
while (input.hasNext())
I suppose input is a Scanner object, so you should do something like this before using it (but before entering the loop):
Scanner input = new Scanner(System.in);
Instead of using scanner.next() in while loop, I would recommend doing the following API:
String.split
Scanner.nextLine
Something like this:
while(input.hasNextLine()) {
String line = input.nextLine();
String[] words = line.split("delimeter");
if(words.length < 1 || words[words.length - 1].equals("q")) {
break;
}
}

Categories