简体   繁体   中英

Want to read .txt file and load it into a 2D array, and then print it as is

Working on an assignement where I have to read a .txt file and place it into a 2D array as is. Note ts HAS TO BE A 2D ARRAY.

I then have to print it like it is again.

The .txt input looks like this:

WWWSWWWW\n
WWW_WWWW\n
W___WWWW\n
__WWWWWW\n
W______W\n
WWWWWWEW\n

Here's the code I have currently, I have an error that says that it cannot resolve method 'add'. Probably has to do with the array initializer

public static void main(String[] args) throws FileNotFoundException {

  Scanner s = new Scanner(new File("D:/trabalho/maze.txt"));
  String[][] list = new list[][];
  while (s.hasNextLine()){
      list.add(s.nextLine());

  }
  s.close();
  System.out.println(list);


}

Then the print output has to be

WWWSWWWW
WWW_WWWW
W___WWWW
__WWWWWW
W______W
WWWWWWEW

Any help? Thanks!

Assuming the reason for using 2D array is that each character is saved in a separate String object. In case we know absolutely nothing regarding the text file, I would implement like this:

public static void main(String[] args) throws FileNotFoundException {
  File textFile = new File("D:/trabalho/maze.txt");
  Scanner rowsCounter = new Scanner(textFile));

  int rows=0;
  while (rowsCounter.hasNextLine()) {
    rowsCounter.nextLine();
    rows++;
  }
  String[][] data = new String[rows][];

  Scanner reader = new Scanner(textFile);
  for (int i = 0; i < rows; i++) {
    String line = reader.nextLine();
    data[i] = new String[line.length()];
    for (int j = 0; j < line.length(); j++) {
      data[i][j] = line.substring(j, j+1);
    }
  }

  reader.close();
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < data[i].length; j++) {
      System.out.print(data[i][j]);
    }
    System.out.println();
  }
}

This implementation can handle unknown number of lines and unknown length of each line.

Here you go!

public static void main(String[] str){

    Scanner s = null;
    try {
        s = new Scanner(new File("path\\text.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      List<String> list = new ArrayList<String>();
      while (s.hasNextLine()){
          list.add(s.nextLine());

      }
      s.close();
      Iterator<String> itr= list.listIterator();

      while(itr.hasNext()){
          System.out.println(itr.next().toString());
      }

}

If you wanna stick with your Array a possible solution would be

public static void main(String[] args) throws FileNotFoundException {

  Scanner s = new Scanner(new File("D:/trabalho/maze.txt"));
  String[][] list = new String[10][5];
  for(int x = x; s.hasNextLine();x++ ){
   for(int i = 0; i < 5 ; i++){
      list[x][i] = s.nextLine();
   }
 }
  s.close();
  System.out.println(list);

}

So you don't even need a 2D array Here because the String Class acts like an char Array in C++.

Another solution would be to use ArrayLists

public static void main(String[] args) throws FileNotFoundException {

  Scanner s = new Scanner(new File("D:/trabalho/maze.txt"));
  ArrayList<String> list = new ArrayList<String>;
  while (s.hasNextLine()){
      list.add(s.nextLine());
  }
  s.close();
  System.out.println(list);
}

So now you have a list that grows with your amount of Data and also you can just use add Method. the line ArrayList<String> means that your arrayList just can store data from class String

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