1

So I've been looking over this for a while and can't seem to get by this issue...

Basically, there is a txt file that the user enters (max 50 rows and 20 integers in each) and I am converting that to a 2D int[][].

So, if the input is:

1 2 3 4 5 6
54 67 66
45
34 54 2

The 2D array should look like:

1  2  3  4  5  6
54 67 66 0  0  0
45 0  0  0  0  0
34 54 2  0  0  0

I am currently getting the following output:

[1, 2, 3, 4, 5, 6, 54, 67, 66, 45, 2, 3, 34, 54, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

This would be fine except the numbers all go into the first row - which they shouldn't.

Here is my code (Functions class):

package com.company;

import java.io.File;
import java.util.Scanner;

public class Functions {
    public int[][] readFile(File file) {

        try {

            Scanner scn = new Scanner(file);
            Scanner scn2 = new Scanner(file);

            //set initial count (of rows) to zero
            int maxrows = 0;

            //sets columns to 20 (every row has 20 integers - filled w zeros if not 20 inputted)
            int maxcolumns = 20;

            // goes through file and counts number of rows to set array parameters
            while (scn.hasNextLine()) {
                maxrows++;
                scn.nextLine();
            }

                // create array of counted size
                int[][] array = new int[maxrows][maxcolumns];

                //new scanner to reset
                Scanner scan1 = new Scanner(file);

                //places integer one by one into array
                for (int row = 0; row < maxrows; row++)
                    for (int column = 0; column < maxcolumns; column++) {
                        if (scan1.hasNext())
                            array[row][column] = scan1.nextInt();
                        else {
                            break;
                        }
                    }
                return array;
        }
        // general exception
        catch(Exception e){
            System.out.println("PROBLEM");
            e.printStackTrace();
            //returns null array
            return null;

        }

    }
}

Main Class:

package com.company;

import java.io.*;
import java.io.IOException;
import java.util.Arrays;
import java.io.File;



public class Main {
    public static void main(String[] args) throws IOException {
        //Creates new class2 object
        Functions o = new Functions();
        //Creates new file object
        File file = new File("src/com/company/UserInput");
        //Takes in file object as parameter
        int[][] array = o.readFile(file);

        //prints as an array
        for (int i=0; i < array.length; i++)
            System.out.println(Arrays.toString(array[i]));
    }
}
3
  • why 2 0 0 0 0 0 ? Commented Mar 18, 2016 at 16:54
  • Hint: don't use static mains to test your code. Learn about Junit; and write unit tests. Commented Mar 18, 2016 at 16:56
  • @FastSnail Thanks for catching that! It was just a typo- I had a different array earlier. Commented Mar 18, 2016 at 16:56

2 Answers 2

3

The problem is in:

for (int row = 0 ; row < maxrows ; row++) {
    for (int column = 0 ; column < maxcolumns ; column++) {
        if (scan1.hasNext()) // ← *here*
            array[row][column] = scan.nextInt();
        else {
            break;
        }
    }
}

scan1.hasNext() will not stop at the end of the line, it will return true until it exhausts the whole file. Instead you should do something like that:

for (int row = 0 ; row < maxrows ; row++) {
    Scanner lineScan = new Scanner(scan1.nextLine());

    for (int column = 0 ; lineScan.hasNextInt() ; column++) {
        array[row][column] = lineScan.nextInt();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!
0

When you use "scan1.nextInt();" you are ignoring the new line, because it, is all in a same row. What you can do is use:

Scanner scan1 = new Scanner(file).useDelimiter("\\||\\n");
while (scan1.hasNext()) {
                String nextValue = scan1.next();
                System.out.println("nextValue " + nextValue);
}

Them you have: nextValue 1 2 3 4 5 6 nextValue 54 67 66 nextValue 45 nextValue 34 54 2

You can split this line and do a manual control

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.