Home > database >  The user must enter a string that contains at least one lowercase “a”
The user must enter a string that contains at least one lowercase “a”

Time:12-31

The user must do the question above or the question keeps repeating so I need a while loop. I need to do this using a subroutine too. My code below isn't working.

public static boolean isAlpha(String name) {
    char[] chars = name.toCharArray();
    for (char c : chars) {
        if (!Character.isLetter(c)) {
            return false;
        }
        else {
            for (int i = 0; i < name.length(); i  ) {
                if (name.charAt(i) >= 'a') {
                    return false;
                }
                else {
                    return false;
                }
            }
        }
    }
    return false;
}

This is the second part:

System. out.print ("Please enter a string that contains at least one lowercase a. ");
String name = input.next ();
if (isAlpha(name)) {
    System.out.println("That is a valid string onto stage 2.");
}
else {
    System.out.println("That is an invalid string. Try again.");
}

CodePudding user response:

You're passing a String to the isAlpha method, which iterates over the String and checks each letter to be 'a' or not. You're returning false for every char that isn't 'a', and returning false if you iterate through the entire String.

An easier way to handle this would be to return true upon finding the first 'a', or returning false after iterating over the entire String. It will make scaling easier as well if you reduce the number of return statements in a single method.

CodePudding user response:

Here are three different ways to check whether a string contains at least one lowercase a. The first way uses a for loop as you have tried to do in the code in your question.

The second way uses regular expressions and the last way uses streams.

The code also contains a main method which contains a while loop as requested in your question.

do the question above or the question keeps repeating

import java.util.Scanner;

public class Solution {
    public static boolean isAlpha(String name) {

        /* Using a loop. */
        char[] chars = name.toCharArray();
        for (char ch : chars) {
            if (ch == 'a') {
                return true;
            }
        }
        return false;

        /* Using regular expression. */
//        return name.matches("^.*a.*$");

        /* Using stream API. */
//        return name.chars()
//                   .filter(c -> c == 'a')
//                   .findFirst()
//                   .isPresent();
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a string that contains at least one lowercase 'a': ");
        String str = input.nextLine();
        while (!isAlpha(str)) {
            System.out.println("That is an invalid string. Try again.");
            str = input.nextLine();
        }
        System.out.println("That is a valid string. On to stage 2.");
    }
}

Here is a sample run:

Please enter a string that contains at least one lowercase 'a': 1 is the lonliest number.
That is an invalid string. Try again.
2 can be as bad as 1
That is a valid string. On to stage 2.

CodePudding user response:

A couple of mistakes were made. Firstly, your method only returns false, there is no way in which it could be true. Secondly, your code here loops through the entire array for every single character.

public static Boolean isAlpha(String name) {
char[] chars = name.toCharArray();
for (char c : chars) {
    if (!Character.isLetter(c)) {
        return false;
    }
    else {
        for (int i = 0; i < name.length(); i  ) {
            if (name.charAt(i) >= 'a') {
                return false;
            }
            else {
                return false;
            }
        }
    }
}
return false;

}

Try this instead.

public static Boolean isAlpha(String name) {
    char[] chars = name.toCharArray();
    for(char c : chars) {
        if(c=='a') {
            return true;
        }
    }
    return false;
}
  • Related