GwadaKing GwadaKing - 2 months ago 8
Java Question

Split Strings from an array of Strings

I want to extract lines from an array of strings, and split the ";" separator while stocking datas in an array. I know this is what the split method is supposed to do. But I can't get through.
How should I process ?

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String LON = in.next();
String LAT = in.next();
int N = in.nextInt();
in.nextLine();
String[] infoDefib=new String[N];
for (int i = 0; i < N; i++) {
String DEFIB = in.nextLine();
infoDefib[i]=DEFIB.split(";");
}
//System.out.println();
}

Answer

The split function will split the String and store each of them into a String array.

ex: String[] strs = DEFIB.split(";");

You don't have to use a for loop and store them into String[] separately. New a String[] and equal to String.split("***"). Java will do it for you.

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    //String LON = in.next();
    //String LAT = in.next();        
    String str = in.nextLine(); // use nextLine which you can read whole 
                                //line and store the data into a String
    in.nextLine();
    String[] infoDefib = DEFIB.split(";");//now you store them into a String array