Hentendo
Hentendo

Reputation: 23

Trying to figure out how to print a certain string, if the string contains a certain first character

Trying now to figure out how to add multiple searches to name.startsWith. So that i can find out of the elements added search for a, e, i, o and u. Instead of just "a".

"Create a method called printFizzBuzz(). This method should loop through the collection, and print out the elements (each String on one line). If the String starts with a, e, i, o or u, instead of printing the String, it should print "Fizz" on the line. If the String starts with A, E, I, O or U, instead of printing the String, it should print "Buzz". For example, if the collection contained the names "banana", "Apple", "orange", "pear", "peach", "kiwi fruit", "Grape", then the printout would look like: Fizz Buzz Fizz pear peach kiwi fruit"

 public class SchoolNames
 {
     private ArrayList<String> names;

    /**
     * Creates a collection of names.
     */
     public SchoolNames()
     {
        names = new ArrayList<>();
     }

    /**
     * Add a name to the collection.
     */
    public void addName(String Name)
    {
        names.add(Name);
    }

    /**
     * Remove a name from the collection.
     */
    public void removeName(int index)
    {
        if (index >= 0 && index < names.size()) 
        {
            names.remove(index);
            System.out.println("Name removed");
        }
        else
        {
            System.out.println("No names to remove");
        }
     }

    /**
     * Return the number of names stored in the collection.
     */
    public int getNumberOfNames()
    {
        return names.size();
    }

    public void listAllNames()
    {
        for (String name : names)
        {
            if (name.startsWith("a"))
            {
                System.out.println("Fizz");
            }
            else if (name.startsWith("A"))
            {
                System.out.println("Buzz");
            }
            else
            {
                System.out.println(name);
            }
        }
    }
}

Upvotes: 1

Views: 1121

Answers (6)

Carson Graham
Carson Graham

Reputation: 539

You might also be able to try

`String lowercase = "aeiou";
String upercase = "AEIOU";
String text = "apple";
if(lowercase.contains(text.charAt(0))){
   System.out.println("fuzz");
}else if(upercase.contains(text.charAt(0)))){
   System.out.println("buzz");
}else{
  System.out.println(text)
}`

you might have to use String.valueOf(text.charAt(0)), to have it as a string

Upvotes: 0

SkPuthumana
SkPuthumana

Reputation: 73

You may try with the following snippets below,

    String str ="Anglo"; //sample input
    String vowelinCaps = "AEIOU";
    String vowelinSmall = "aeiou";

    if (vowelinCaps.indexOf(str.charAt(0)) >= 0)
        System.out.println("Fizz");
    else if (vowelinSmall.indexOf(str.charAt(0)) >= 0) 
        System.out.println("Buzz");
    else
        System.out.println(str);

Update-

  • char java.lang.String.charAt(int index) returns the char value at the specified index.

  • int java.lang.String.indexOf(int ch) returns the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Upvotes: 1

Ravikiran S
Ravikiran S

Reputation: 9

Do you mean to do it this way?:

public class StringTest {

    public static void main(String args[]){

        String  testString = "aatest";
        if (testString.matches("(aa|bb|cc|dd|dd).*")){
            System.out.println("true");
        }

        if(testString.startsWith("aa") || testString.startsWith("bb")){ // other conditions as well
            System.out.println("true");
        }
    }
}

Upvotes: -1

Ravi Sapariya
Ravi Sapariya

Reputation: 405

You can use following regex code to replace word based on your pattern

public void findAndReplace(List<String> items) {
    StringBuffer stringBuffer = new StringBuffer();
    for(String eachItem : items) {
        if(isMatchFound(eachItem,"^[AEIOU]")) {
            stringBuffer.append("Buzz");
            stringBuffer.append(" ");
        }else if(isMatchFound(eachItem,"^[aeiou]")) {
            stringBuffer.append("Fizz");
            stringBuffer.append(" ");
        }else {
            stringBuffer.append(eachItem);
            stringBuffer.append(" ");
        }
    }
    System.out.println(stringBuffer);
} 
public Boolean isMatchFound(String item,String pattern) {
    return item.matches(pattern);
}

Regex : ^[AEIOU] check string's first(^) charactor in [AEIOU] data set or not.

Upvotes: 0

Arnaud
Arnaud

Reputation: 17524

As stated in the other answer, an else statement is indeed missing.

You also probably want to avoid having an if statement per letter (a,e,i....), so you might do something like :

for (String name : names)
{

    char firstChar = name.charAt(0);

    if ("aeiou".indexOf(firstChar)>-1)
    {
        System.out.println("Fizz");
    }
    else if ("AEIOU".indexOf(firstChar)>-1)
    {
        System.out.println("Buzz");
    }
    else
    {
        System.out.println(name);
    }
}

Upvotes: 0

Deb
Deb

Reputation: 2972

You should add an else statement for that. If the all condition fail in the if and else if then only else part will be executed, Otherwise not

if (name.contains("a")) {
    System.out.println("Fizz");
} else if (name.contains("A")) {
    System.out.println("Buzz");
} else {
    System.out.println(name);
}

Update: If the reqiurement to check the start element of a string then you should check with name.startsWith("a")

Update:

Question: YES! that worked brilliantly! The last thing i need to figure out, is how to add multiple characters to the name.startsWith. I've tried adding "a", "e". but it does not work

No. it shouldn't startsWith accept a single string paramater. You can check this with the or operator.

if (name.startsWith("a") || name.startsWith("e")//....rest of check)

Upvotes: 1

Related Questions