How to modify/split invidual string in an array

James

New Member
#1
How to create a method send in an array of names in “firstName lastName” form(ex:Anna Hale). The method returns the name(through a return statement) in the form “first---last”. Below is my full name array
Mã:
public static void readData(String file) throws FileNotFoundException {
    x = new Scanner(new File(file));

    n = 0;
    while(x.hasNextLine()) {
        n++;
        x.nextLine();
    }
    name = new String[n];
    Scanner x1 = new Scanner(new File(file));

    for(int i = 0; i < name.length; i++ )
        name[i] = x1.nextLine();
        System.out.println(Arrays.toString(name));
}
 

Admin

Administrator
Thành viên BQT
#2
Here is the full code assuming you want to read from file. Please correct the compile time errors in your code posted. Since you do not know the length of array in advance you can't initialise the array or you can change the logic to get the number of lines then initialise array.

The file contents looks like below:
Mã:
abc xyzzy
And sdff
Asdf sdfw
The output looks like below
Mã:
[abc--xyzzy, And--sdff, Asdf--sdfw]
In the below code name.replace(" ", "--"); replaces space " " with --

Code:
Mã:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Post1 {

    public static void main(String[] args) throws FileNotFoundException {
         String[] transformedNames = readData("/Users/kuma/Desktop/post1.txt");
         System.out.println(Arrays.toString(transformedNames));
    }

    public static String[] readData(String file) throws FileNotFoundException {
        Scanner x = new Scanner(new File(file));
        List<String> list = new ArrayList<>();

        while(x.hasNextLine()) {
            list.add(x.nextLine());
        }
        x.close();
        String[] name = new String[list.size()];

        list.toArray(name);

        String[] transformedNames = new String[list.size()];

        for(int i = 0; i < name.length; i++ )
            transformedNames[i] = name[i].replace(" ", "--");

        return transformedNames;
    }

}
The same is being achieved in Java 8 using stream API:
Mã:
private static String[] readDataUsingJava8() {
        String[] transformedNames = null;
        try (Stream<String> stream = Files.lines(Paths.get( "/Users/kuma/Desktop/post1.txt"))) {
            transformedNames =
             stream.map(new Function<String, String>() {

                @Override
                public String apply(String t) {
                    // TODO Auto-generated method stub
                    return t.replace(" ", "--");
                }
            }).toArray(String[]::new);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return transformedNames;
    }
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top