Skip to content
Advertisement

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.

JavaScript

Advertisement

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement