简体   繁体   中英

how to get and split multiple String lines input by space then add them to arrayList in Java?

How can I get multiple lines String and split them by space then add to ArrayList in Java?

the first line of input is an integer which indicates how many lines string should be scan. This input will be used for checking anagram words and then print anti-anagram words. the words should be just split by space and dash (-) should be removed.

Sample Input:

4
sali est p-o-s-t try tset luf
set boo ins pick too let sim
set post sho kim lack
flu est test soo tick
public static Scanner scan;
public static void main(String[] args) {
     scan = new Scanner(System.in);
     int l = scan.nextInt();
     scan.nextLine();
     String[][] arr = new String[][];
     List<String> ss1 = new ArrayList<String>();
     for (int i=0; i<l; i++) {
       arr[i] = scan.nextLine().replaceAll("\\-", "").split("\\ |\\n");
            String[] dd = arr[i];
            String ddd = dd[i];
            ss1.add(ddd);
         }
      } 
      ...
}

Or this:

public static Scanner scan;
public static void main(String[] args) {
    scan = new Scanner(System.in);
    int line = scan.nextInt();
    scan.nextLine();
    String str = "";
    ArrayList<String> shd = new ArrayList<String>();
    for (int i=0; i<line; i++) {
        str += scan.nextLine();
        shd.add(str);
     }
}

This input will be used for checking anagram words and then print anti-anagram words. the words should be just split by space and dash (-) should be removed.

it should be split by space like ( [ sali, est, post, ...]

Actual Output:

[sali est p-o-s-t try tset luf, 
sali est p-o-s-t try tset lufset boo ins pick too let sim, 
sali est p-o-s-t try tset lufset boo ins pick too let simset post sho kim lack, 
sali est p-o-s-t try tset lufset boo ins pick too let simset post sho kim lackflu est test soo tick]

You mean?

Scanner io = new Scanner(System.in);
int n = io.nextInt();
io.nextLine();
List<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
    String str = io.nextLine();
    String join = String.join("", list) + str;
    list.add(join);
}

You may consider the following code

public static Scanner scan;
public static void main(String[] args) {
    ArrayList<String> shd = new ArrayList<>();
    scan = new Scanner(System.in);
    String line = scan.nextLine();
    int lines = Integer.parseInt(line);
    //Loop scanner for each line
    for (int i=0;i<lines;i++){
        String currentLine = scan.nextLine().trim().replaceAll("-", "");
        String [] words = currentLine.split(" ");
        for (String word:words){
            shd.add(word);
        }
    }
    System.out.println(shd);
}

I did a change on the Scanner way of reading the int line, as the nextInt() reads the next integer token and not the nextLine, so someone might enter 4 3. In that case, the 4 is read as Int and the 3 as the next line. Having nextLine() read the whole line and then parse it into an integer will solve this issue.

Later you loop for as many lines and the code reads the line, trims it so it removes the leading and trailing whitespaces and finally replaces the - with empty string.

Then the code splits the line into word parts on every space character and adds each word in the ArrayList.

Example:

4
sali est p-o-s-t try tset luf
set boo ins pick too let sim
set post sho kim lack
flu est test soo tick
[sali, est, post, try, tset, luf, set, boo, ins, pick, too, let, sim, set, post, sho, kim, lack, flu, est, test, soo, tick]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM