How can i use string.split method to add empty entries into the empty fields ?

69 Views Asked by At

i've tired multiple times to add empty entries into the empty fields by using the string.split method but i still couldn't pass on eclipse, can someone please help me / guide me?

Here is the code:

public void test_incomplete_lines() throws Exception {
    /**
     * Test scenarios: 
     * 
     * ,l1c2,l1c3,l1c4
     * l2c1,,l2c3,l2c4
     * l3c1,l3c2,,l3c4
     * l4c1,l4c2,l4c3,
     * ,l5c2,l5c3,
     * l6c1,,l6c3,
     * l7c1,l7c2,,
     * ,,l8c3,l8c4
     */
1

There are 1 best solutions below

2
On

I took % symbol so after eliminating this symbol I will get whitespace. you can take another symbol too.

import java.util.*;

public class BlankInsert {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter  A String :    ");
    String blank = scan.next();
    String[] space = blank.split("%");
    System.out.println(space[0]);
    System.out.println("printed");
}
}

OUTPUT

Enter  A String :

Just Enter Any Character With % And Press Enter You Will See A Blank Line With Which Is Generated Cause Of Whitespace.

%s%

printed

I just printed another String printed for the confirmation that Whitespace is printed.