Skip to content

Exercises with java 11. For example String class, Files class, Collection interface, Lambdas, Reflection Api, others.

Notifications You must be signed in to change notification settings

andresWeitzel/Java_11_Exercises

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 

Repository files navigation

Java_11_Exercises

Exercises with java 11. For example String class, Files class, Collection interface, Lambdas, Reflection Api, others.


Index πŸ“œ

See

String Methods




Project execution πŸ”

See



String Methods

Using the isBlank method πŸ”

Check if a string has empty spaces

See solution

Code

public class TestClass {
   public static void main(String args[]) {
       
       /**
        * public boolean isBlank()

Returns true if the string is empty or contains only white space codepoints, otherwise false.

Returns:
   true if the string is empty or contains only white space codepoints, otherwise false
Since:
   11
See Also:
   Character.isWhitespace(int) 
        * 
        */
       
       
    String firstString = "First String to test";

     System.out.println("First String : "+firstString.isBlank());
     
          String secondString = " ";

     System.out.println("Second String : "+secondString.isBlank());
     
               String thirdString = "";

     System.out.println("Third String : "+thirdString.isBlank());

   }
}

Console

First String : false
Second String : true
Third String : true


Using the lines method πŸ”

Create a Java program to read a string and obtain the content as a stream of lines.

See solution

Code

import java.util.stream.Stream;

public class Main 
{
 public static void main(String[] args) 
 {
   try
   {
     String str = "A \n B \n C \n D"; 

     Stream<String> lines = str.lines();

     lines.forEach(System.out::println);
   } 
   catch (Error e) 
   {
     e.printStackTrace();
   }
 }
}

Console

A 
B 
C 
D


Using various methods πŸ”

Extract non-blank deleted lines from a multi-line string.

See solution

Code

public class ExampleClass {
   public static void main(String args[]) {
   String multilineString = "Baeldung helps \n \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");
   }
}

Console



About

Exercises with java 11. For example String class, Files class, Collection interface, Lambdas, Reflection Api, others.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published