Java storing file values into an array

RK 1 :

Currently I am trying to read all the files from .prg file into an array. Currently the code I have only reads the first line of the file. I would like to read each word within the file and store it within an array. I believe the issue is with the way I am splitting the file but I cannot seem to find a solution

Code below:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
 public class Reader {
    private Scanner reader

    public Reader(String fileName){

              try {
                   reader = new Scanner(new File(fileName));
                } catch (FileNotFoundException e){
                         e.printStackTrace();
                 }


        }
    public String[] getInput() {

    String [] input = reader.nextLine().trim().split(" ");
    return input;

}

 }
 public class Player {
     private String[] PlayerInformation;
     private String pName;


public Player(String gName) {
    this.pName = gName;

    this.reader = new 
    Reader("C:\\SpaceInvader\\Files\\PlayerInfo.prg");
    this.PlayerInformation= reader.getInput();
}
 public void ArrayPrintTest() {

    System.out.println(Arrays.toString(this.PlayerDetails));

}
public class Game {

  public Game() {

  }
  public static void main(String[] args){
             Player player = new Player("Player1");
             player.ArrayPrintTest();
             }

}

File format:

Instruction 1
Jane
ForwardBackUpDown

Current Output

[Instruction, 1]

Expected Output

[Instruction, 1, Jane, ForwardBackUpDown]
Samuel Philipp :

You are actually reading only the first line of your file, because you are running reader.nextLine() only one time.

To read the complete file you can use the Files.lines() method and use Java Streams:

List<String> words = Files.lines(Paths.get(fileName))
        .map(String::trim)
        .map(line -> line.split(" "))
        .flatMap(Arrays::stream)
        .collect(Collectors.toList());

If you want you result to be an array you can use the Stream.toArray() method instead of Stream.collect():

String[] words = Files.lines(Paths.get(fileName))
        .map(String::trim)
        .map(line -> line.split(" "))
        .flatMap(Arrays::stream)
        .toArray(String[]::new);

If you want to read the file without the Files.lines() method you can use the following:

List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
    String line;
    while ((line = br.readLine()) != null) {
        lines.add(line);
    }
}

Now you can use lines.stream() and continue with one of the first approaches.

At the end one last hint: If you want to split by any whitespace, not just " " you can use line.split("\\s+").

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive