Giter VIP home page Giter VIP logo

java_11_exercises's Introduction

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



java_11_exercises's People

Contributors

andresweitzel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.