Skip to content

print a jagged array from a file

i am trying to print a jagged array just like my txt file is. I have already found the rows I was told to create a 2d array now with just the rows since we don’t know the columns yet and then find the doubles there are in the file.

        double[][] arr = new double[rows][];

        Scanner scanner2 = new Scanner (file);   

        int i =  0;
        while (scanner2.hasNextLine()){
            String line = scanner2.nextLine();
            Scanner newLine = new Scanner(newLine);

            int countDoubles = 0;
            while(scanner2.hasNextDouble()){
                countDoubles++;
            }
    

            double[] doubleArr = new double[countDoubles];

            newLine = new Scanner(newLine);


            // populate doubleArr using newLine


            arr[i]=doubleArr;
            i++;

        }

        //how I counted the col on a normal 2d array file 
      

        String in = scanner.nextLine();
        String [] out = in.split(" ");
        for (int s = 0; s < out.length; s++){
            col++;
       

        //printing the array

        for(int k = 0; k < rows; k++) {       
            for(int j = 0; j < col; j++) {
                arr[i][j] = scanner2.nextDouble();
                System.out.print(arr[i][j] + " ");
               
            }
            System.out.println();
        }

Answer

There are three main issues, firstly all the column code needs to go within the while loop so that it is done for every line of the file. Secondly, you were getting stuck in a loop here while(scanner2.hasNextDouble()){, and lastly, there were a bunch of code errors that I have corrected below, but specifically you can’t reference a Scanner to itself Scanner newLine = new Scanner(newLine); it should refer to the string from the previous line Scanner newLine = new Scanner(line);

Here is a working example using as much of your code as possible:

    Scanner scanner2 = new Scanner(file);

    //Get total lines
    int count = 0;
    while (scanner2.hasNextLine()) {
        count++;
        scanner2.nextLine();
    }

    //Create the array with the correct length
    double[][] arr = new double[count][];

    //Reset the scanner:
    scanner2 = new Scanner(file);

    int i =  0;
    //Loop thorugh the file one line at a time
    while (scanner2.hasNextLine()){
        //Get the current line
        String line = scanner2.nextLine();
        //Changed this line to correctly reference "line", not "newLine"
        //Note that you don't need to use scanner here, you could just split the line and use a for loop to go through it
        Scanner newLine = new Scanner(line);

        int countDoubles = 0;
        //Changed this line to reference the correct scanner "newLine"
        while(newLine.hasNextDouble()){
            countDoubles++;
            //Consume the double so that the code advances, otherwise it will just loop for ever
            newLine.nextDouble();
        }

        double[] doubleArr = new double[countDoubles];
        //This line vas incorrectly referring to the wrong thing, it should be "line", not "newLine"
        newLine = new Scanner(line);
    
        //Use a "for" loop here inside of the while loop to store the column data per each line of the file
        for (int y = 0; y < countDoubles; y++)
        {
            doubleArr[y] = newLine.nextDouble();
        }

        // populate doubleArr using newLine
        arr[i]=doubleArr;
        i++;
    }

    //Code that deals with data needs to be inside the while loop, see the edits above
    //String in = scanner.nextLine();
    //String [] out = in.split(" ");
    //for (int s = 0; s < out.length; s++){
    //    col++;
    //
    ////printing the array
    //for(int k = 0; k < rows; k++) {       
    //    for(int j = 0; j < col; j++) {
    //        arr[i][j] = scanner2.nextDouble();
    //        System.out.print(arr[i][j] + " ");      
    //    }
    //    System.out.println();
    //}

    //Now we can print the array dynamically by using the array length like so "k < arr.length" and "j < arr[k].length"
    for (int k = 0; k < arr.length; k++)
    {
        for (int j = 0; j < arr[k].length; j++)
        {
            System.out.print(arr[k][j] + " ");
        }
        System.out.println();
    }

Read through the code comments and you should get a better understanding of how this works.

With the above code I get the following output which matches a sample file I made:

1.0 2.0 3.0 
4.0 5.0 
6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 
14.0 15.0 16.0 17.0 
18.0 19.0 
20.0 
21.0 22.0