Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a txt file where there is a grid of numbers but in jagged, I have to print the jagged array, I did this and it works for a normal 2d array as in the empty spaces are just coming out to zeros instead of it just being empty like the file is so it won't work for a jagged array.


Txt file: 
210 1000 490.5
100.0 30 2000 10 500
150 345 1400 800
400 240 500.3
0 1800

expected output:
210 1000 490.5
100.0 30 2000 10 500
150 345 1400 800
400 240 500.3
0 1800

ouput that's happening:
210 1000 490.5 0.0
100.0 30 2000 10 
150 345 1400 800
400 240 500.3 0.0
0 1800 0.0 0.0


What I have tried:

<pre lang="text"><pre>public static void main(String args[]) throws FileNotFoundException {
    File file = new File("input.txt");
    Scanner scan = new Scanner(file); 
    int row = 0;
    int col = 0;
    while (scan.hasNextLine()){
        String line = scan.nextLine();
        row++;     
    }
   

     scan = new Scanner (file);
    String sn = scan.nextLine();
    String [] otLine = sn.split(" ");
    for (int i = 0; i < otLine.length; i++){
        col++;
    }
    

    
     scan = new Scanner (file);
    double [][] arr = new double[row][col];   
    for(int i = 0; i < row; i++) {       
        for(int j = 0; j < col; j++) {
            arr[i][j] = scan.nextDouble();
            System.out.print("\t"+arr[i][j] );
           
        }
        System.out.println();
    }
Posted
Updated 29-Jan-22 18:43pm

Quote:
How do I print a 2d jagged array from a txt file java

Your problem is that the input have a variable number of values in each lines, and you calculate a global number of columns instead.
When printing, you need to use the number of values of currant line and this fir each line.
You need to reorganize your code in order to have the number of values of currant line when you print it.


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
The problem is that when you create your array, you are always allocating the same number of items:
double [][] arr = new double[row][col];
regardless of the number of items in the line. Then when you print them, you also always print the same number of columns in each row.
That's not a jagged array: it's a rectangular array, and you are hoping that when you read your line of data it will have enough elements for every row!

Instead, you have to create and use a Jagged Array[^] and size each row to its content.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900