1

I want to create a multidimensional array and pass it as a parameter in a method and then fill the arrayList with elements and return the new version of the arrayList to be able to use that array in different classes but I get java.lang.NoSuchMethodError: I think the problem is about the way i return the array. I searched but I could not find . How can I do it correctly?

here is my code;

public test{

 public static List<List<String>> 2Darray=new ArrayList<List<String>>(); // TE ERROR IN THIS LINE


 public List<List<String>> fillArray(List<List<String>> array){

BufferedReader in = null;
            ArrayList<String> row = new ArrayList<String>();
            try {
                in = new BufferedReader(new FileReader("sampleFile.txt"));
                String read = null;
                    while ((read = in.readLine()) != null) {
                        String[] splited = read.split("\\s+");                       
                        for(int i=0; i<splited.length ; i++){                           
                            row.add(splited[i]);
                        }                        
                        array.add(row);
                    }                         

                } catch (IOException e) {
                System.out.println("There was a problem: " + e);
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                }
      return array;
}
1
  • please include full stack trace and tell us which line is throwing the exception Commented May 10, 2018 at 14:52

2 Answers 2

2

A little tinkering (just getting it to compile) results in this which seems not to have a problem. Perhaps your issue is elsewhere.

public List<List<String>> fillArray(List<List<String>> array) {

    BufferedReader in = null;
    ArrayList<String> row = new ArrayList<String>();
    try {
        in = new BufferedReader(new FileReader("sampleFile.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");
            for (int i = 0; i < splited.length; i++) {
                row.add(splited[i]);
            }
            array.add(row);
        }

    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return array;
}

BTW: You should really use try with resources - it is much clearer.

Sign up to request clarification or add additional context in comments.

Comments

1

Modified your code a bit so that it compiled, and replaced the reading from a text file to reading a string. There were several issues, but it seems to work. Give it a try.

The main problems I noticed were mismatching curly braces, and starting a variable name with a number.

import java.util.*;
import java.io.*;

public class Main {
  public static List<List<String>> array2D = new ArrayList<List<String>>();

  public List<List<String>> fillArray(List<List<String>> array) {

    BufferedReader in = null;
    ArrayList<String> row = new ArrayList<String>();
    try {
      String str = "Some test text";
      InputStream is = new ByteArrayInputStream(str.getBytes());
      //in = new BufferedReader(new FileReader("sampleFile.txt"));
      in = new BufferedReader(new InputStreamReader(is));
      String read = null;
      while ((read = in.readLine()) != null) {
        String[] splited = read.split("\\s+");                       
        for(int i=0; i<splited.length ; i++) {                           
            row.add(splited[i]);
        }                        
        array.add(row);
      }                         

    } 
    catch (IOException e) {
      System.out.println("There was a problem: " + e);
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      } 
      catch (IOException e) {
      }
    }
    return array;
  }

  public static void main(String[] args) {
    Main main = new Main();
    List<List<String>> test = main.fillArray(array2D);

    for(int i  = 0; i < test.size(); i++) {
      for(int j = 0; j < test.get(i).size(); j++) {
        System.out.println(test.get(i).get(j));
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.