Home > Mobile >  How to print strings from an array that contain the letter 'z'?
How to print strings from an array that contain the letter 'z'?

Time:12-02

I need to write a Java program that prints out all the strings (from a given array) that contain the letter z? I need to do it without using the function .contains.

My code so far:

public static void main(String[] args) {
    System.out.println("Program prints out all the words with common letter:");
    String[] strArr = {"computers", "information systems", "test"};
    System.out.println(Lettercheck(strArr , 'x'));
    
}

public static void Lettercheck(String[] strArr , char ch) {
    for (int i = 0; i < strArr.length; i  ) {
        
        
    }
}

CodePudding user response:

You can use the .charAt() function, this return the current character in String.

eg.

String a = "Hello";
System.out.println(a.charAt(0));

This will give you 'H' as output. So your code should look something like this:

public static void Lettercheck(String[] strArr , char ch) {
    for (int i = 0; i < strArr.length; i  ) {
        for( int j=0; j<strArr[i].length();j  ){
             if(strArr[i].charAt(j)==ch)
                  System.out.println(strArr[i]);
                  break; 
        }
    }
 }

CodePudding user response:

tl;dr

List
    .of( "computers" , "information systems" , "zebra" )
    .stream()
    .filter( 
        input -> input.codePoints().anyMatch( codePoint -> codePoint == "z".codePointAt( 0 ) ) 
    ) 
    .toList()

See this code run live at IdeOne.com.

[zebra]

Obsolete char

The char type has been legacy since Java 2, essentially broken. As a 16-bit value, a char is physically incapable of representing most characters.

Code point

Instead, use code point integer numbers to work with individual characters.

Get the code point number assigned to our targeted letter, z, which is 122 decimal.

int targetCodePoint = "z".codePointAt( 0 ); // Get the code point assigned to the letter “z”, 122.

Define our inputs, and make a place to put the words/phrases that contain our target letter.

List < String > inputs = List.of( "computers" , "information systems" , "zebra" );
List < String > hits = new ArrayList <>( inputs.size() );

Loop each input word/phrase.

for ( String input : inputs )
{
    …
}

For each of those inputs, loop the code point of each character in that word/phrase.

Test each code point to see if it matches our target code point (122). If so, remember this word by adding to our hits list, and then bail out of the inner loop (the code points loop).

loopingInputs:
for ( String input : inputs )
{
    // Loop the code point of each character.
    int[] codePoints = input.codePoints().toArray();
    for ( int codePoint : codePoints )
    {
        if ( codePoint == targetCodePoint )  
        {
            hits.add( input );
            break loopingInputs;
        }
    }
}

Dump results to console.

System.out.println( "hits = "   hits );

See this code run live at IdeOne.com.

hits = [zebra]

We can slim down this code to be more compact by using streams and lambdas. See the tl;dr section at top.

  • Related