简体   繁体   中英

How to split file input into 2 different arrays java

How do I split a file input text into 2 different array? I want to make n array for the names, and an array for the phone numbers. I managed to do the file input, but ive tried everything and cant seem to split the names and the numbers, then put it into 2 different arrays. Im noob pls help

here is how the phonebook.txt file looks like

Bin Arry,1110001111
Alex Cadel,8943257000
Poh Caimon,3247129843
Diego Amezquita,1001010000
Tai Mai Shu,7776665555
Yo Madow,1110002233
Caup Sul,5252521551
This Guy,7776663333
Me And I,0009991221
Justin Thyme,1113332222
Hey Oh,3939399339
Free Man,4533819911
Peter Piper,6480013966
William Mulock,9059671045

below is my code

   import java.io.*;
   import java.util.Scanner;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.io.InputStreamReader;
   import java.io.BufferedReader;
   public class demos {
   public static void main(String[]  args){

    FileInputStream Phonebook;  
    DataInputStream In;
    int i = 0;

     String fileInput;  
      try  
      { 

            Phonebook = new FileInputStream("phonebook.txt"); 

            FileReader fr = new FileReader("phonebook.txt");
            BufferedReader br = new BufferedReader(fr);
            String buffer;
            String fulltext="";
            while ((buffer = br.readLine()) != null) {

                fulltext += buffer;

               // System.out.println(buffer);

                String names = buffer;
                char [] Y ; 
                Y = names.toCharArray();
                System.out.println(Y);

      }}
         catch (FileNotFoundException e)  
            { 
            System.out.println("Error - this file does not exist"); 
         }  
        catch (IOException e)   
        { 
            System.out.println("error=" + e.toString() ); 

         }

You could simplify it if you are using Java 8:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

public class Test {
    static ArrayList<String> names = new ArrayList<String>();
    static ArrayList<String> numbers = new ArrayList<String>();

    /**
     * For each line, split it on the comma and send to splitNameAndNum()
     */
    public static void main(String[] args) throws IOException {
        Files.lines(new File("L:\\phonebook.txt").toPath())
        .forEach(l -> splitNameAndNum(l.split(",")));
    }

    /**
     * Accept an array of length 2 and put in the proper ArrayList
     */
    public static void splitNameAndNum(String[] arr) {
        names.add(arr[0]);
        numbers.add(arr[1]);
    }
}

And in Java 7:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ArrayList;

public class Test {
    static ArrayList<String> names = new ArrayList<String>();
    static ArrayList<String> numbers = new ArrayList<String>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("L:\\phonebook.txt")));

        String line;

        while ((line = br.readLine()) != null) {
            splitNameAndNum(line.split(","));
        }
    }

    /**
     * Accept an array of length 2 and put in the proper ArrayList
     */
    public static void splitNameAndNum(String[] arr) {
        names.add(arr[0]);
        numbers.add(arr[1]);
    }
}

For a full functionnal (rather than imperative) solution I propose you this one :

public static void main(String[] args) throws IOException {
    Object[] names = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[0]).toArray();
    Object[] numbers = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[1]).toArray();


    System.out.println("names in the file are : ");
    Arrays.stream(names).forEach(System.out::println);
    System.out.println("numbers in the file are : ");
    Arrays.stream(numbers).forEach(System.out::println);
}

output names in the file are : Bin Arry Alex Cadel Poh Caimon Diego Amezquita Tai Mai Shu Yo Madow Caup Sul This Guy Me And I Justin Thyme Hey Oh Free Man Peter Piper William Mulock numbers in the file are : 1110001111 8943257000 3247129843 1001010000 7776665555 1110002233 5252521551 7776663333 0009991221 1113332222 3939399339 4533819911 6480013966 9059671045

As you can see functionnal programming is short and smart …. and easy when you're accustomed

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