Reece Akhtar
Reece Akhtar

Reputation: 45

Difficulties with creating a 2D array and then writing it to a file (Java)

For a homework assignment, I need to create a class that that can read and write Byte arrays to/from a file. I have successfully created classes that can read and write CSV and text, however I am having some difficulty, when it comes to arrays.

I have written a method (writeByte - see below), that uses the FileOutput class (creates files, class can be found here http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileOutput.java). I am getting stuck at the nested for loop. I need the for loop to allow the user to enter a series of integers that are separated by a tab.

When the following code is run I get the following errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method writeNewline() in the type FileOutput is not applicable for the arguments (String[])

I understand what this error message is saying to me, however when I try and create a new writeNewLine method in the FileOutput class, I am none the better. I am hoping that someone will be able to advise how I can rectify this problem. I am more than happy to not use the FileOutput class, I only used it as I have successfully created other file handling programs using it.

Kind regards


public void writeByte(final String fileNameOUT){
    Scanner input = new Scanner (System.in);
    FileOutput createData = new FileOutput (fileNameOUT);
    System.out.println("How many rows?");
    int rowsOut = input.nextInt();
    System.out.println("How many columns?");
    int columnsOut = input.nextInt();


    System.out.println("Please enter data, separate each column with a tab.");


        String[] dataIn;
        int [] [] data = new int [rowsOut] [columnsOut];    
        String[] line = null;

        for (int i = 0; i < rowsOut; i++){
                    line = createData.writeNewline(dataIn = input.nextLine().split("\t"));
            for (int j = 0; j < columnsOut; j++){
                data[i][j] = Integer.parseInt(line[j]); 
                System.out.print(data[i][j]);

            } 
        }   
        createData.close();

}

Upvotes: 0

Views: 126

Answers (2)

Aksirba
Aksirba

Reputation: 111

I would suggest you to use an IDE, like Netbeans or Eclipse. Syntactical and semantical problems are marked by the IDE, so you can see where is the problem while you are just editing the code , you don't have to compile always the file. (I want it as a comment, but I can't do it still, so sorry).

Upvotes: 1

Crickcoder
Crickcoder

Reputation: 2155

By going over the code its clear that writeNewline doesn't have an argument. I think what you want to do is this :

for (int i = 0; i < rowsOut; i++){
            createData.writeNewline();
            String lineString = input.nextLine();
            line = lineString.split("\t");
            for (int j = 0; j < columnsOut; j++){
                data[i][j] = Integer.parseInt(line[j]); 
                System.out.print(data[i][j]);   
            } 
        } 

Upvotes: 1

Related Questions