-2

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.");
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • Welcome to Stack Overflow. Please read [ask] and [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822), and make sure to **ask a question**. "My code below isn't working." is [not a question](https://meta.stackoverflow.com/questions/284236), because it does not identify a specific problem, and it does not let us know what you don't understand about the problem. – Karl Knechtel Dec 31 '22 at 01:59
  • 1
    At what point are you returning True? – Justin Adams Dec 31 '22 at 02:00
  • Try this one line instead: `return name.matches("\\w*a\\w*");` – Bohemian Dec 31 '22 at 02:24
  • 1
    @Bohemian I don't think that's a great solution for a learner. – tgdavies Dec 31 '22 at 02:40
  • Is the requirement a string which contains a lowercase a, or just any lowercase letter? – tgdavies Dec 31 '22 at 02:41
  • it requires to contain a lowercase a – Anjola Adejoro Dec 31 '22 at 03:45
  • 1. _every_ `return` statement returns `false`. 2. 'higher than a' is not 'a lowercase letter'. There are things after 'z' that you presumably wouldn't want. `name.charAt(i) >= 'a' && name.charAt(i) <= 'z'` is what you want. 3. `return` __immediately__ stops looping. You'd want to `return false` if you detect a non-letter, and keep going (do nothing - do _not_ `return`) otherwise. At the end, when the for loop is done - that means no letter caused that early return. Thus, at the end, `return true;`. – rzwitserloot Dec 31 '22 at 03:46
  • @JustinAdams if the string they enter has at least one lowercase a then it is true. – Anjola Adejoro Dec 31 '22 at 03:46
  • Why do you have `>='a'` if you are looking for *only* `a`? – tgdavies Dec 31 '22 at 03:58
  • @rzwitserloot I think you meant "you'd want to return true if you detect a lowercase `a` and keep going otherwise, ... at the end, return `false`". Which doesn't match the name `isAlpha` but seems to be what the OP wants the code to do. – tgdavies Dec 31 '22 at 04:00
  • @tgdavies yes exactly im just confused honestly – Anjola Adejoro Dec 31 '22 at 04:17
  • So Anjola why did you use `>='a'` in your code? Rather than just `=='a'`? – tgdavies Dec 31 '22 at 04:20
  • You're trying to parse over a string to return true on an 'a', but it returns false whenever it isn't 'a'. It isn't given a chance to go through the entire string right, as it will return false on the first char that isn't 'a'? – Justin Adams Dec 31 '22 at 04:51
  • @JustinAdams can you please help me with this question. The user must enter a string that is between 5 and 15 (inclusive) characters long, and does not contain the letter “z”. I'd really appreciate it. I have some but it's not working and Stack OverFlow isn't allowing me to post anymore. – Anjola Adejoro Jan 02 '23 at 05:59

3 Answers3

0

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.

0

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.
Abra
  • 19,142
  • 7
  • 29
  • 41
0

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;
}