user3414479 user3414479 - 2 months ago 11
Java Question

Saturday Night Java Fun with 2D arrays, and reading a text based tab delimited file to populate such an array

Hey guys and gals hope everyones Saturday night is going as swimmingly (preferably more) than mine own.

I'm a java noob so bear with me.

We are told to export the an excel sheet from Open Office into a .txt (Tab Delimited)
It ends up looking like this

1 2 3
4 5 6
7 8 9
10 11 12


Where all values are separated by a tab.. (something I haven't encountered yet and are Integer values)
I can see one option, as i type this as I could capture each line, then string split the line by?? whitespace or /t ... and then assign the values to the respective positions in the [30][10] array...

(which ends up being a .csv and load it into java.

So Job 1 is to populate a 2D array with the files from the tab delimited file.

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

public class Nitrogen
{

private int elevations[][] = null;
private String filename = "location.txt";

public Nitrogen()
{
int [][]elevations = new int[30][10];

}


public void run()
{

try{
File file = new File(filename);
Scanner input = new Scanner(file);

int rows = 30;
int columns = 10;
int[][] elevations = new int[30][10];

for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < columns; ++j)
{
if(input.hasNextInt())
{
elevations[i][j] = input.nextInt();
}
}

for (int h=0; h < rows; h++) {
for (int g=0; g < columns; g++)
System.out.print(elevations[h][g] +" ");
}
System.out.println("");
}

}
catch (java.io.FileNotFoundException e) {
System.out.println("Error opening "+filename+", ending program");
System.exit(1);}

}
public static void main(String[] args)
{
Nitrogen n = new Nitrogen();
n.run();

}

}


So, this prints out 30 lines of line 1, then a line of 0's on top of 29 lines of line 2, then 2 lines of 0's on top of 28 lines of line 3, You get the point....all moving left to right.

Not quite sure....tis getting late and i might give up for the evening.

Alright!! Here is the solution... persistence pays of thanks for the help everyone

public void populate()
{

try{
File file = new File(filename);
Scanner input = new Scanner(file);

int rows = 30;
int columns = 10;
int[][] elevations = new int[30][10];

for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j)
{
if(input.hasNextInt())
{
elevations[i][j] = input.nextInt();
}
}
}
for (int h=0; h < rows; h++){ //This was just to show I had it
for (int g=0; g < columns; g++) { //in there correctly

System.out.print(elevations[h][g] +" ");
}
System.out.println(""); }

}

catch (java.io.FileNotFoundException e) {
System.out.println("Error opening "+filename+", ending program");
System.exit(1);}

}

Answer

If you are trying to print out the array, I would suggest the following amendment to your code:

for (int h=0; h < rows; h++) {
        for (int g=0; g < columns; g++)
            System.out.print(elevations[h][g] + " ");
        }
        System.out.println("");
 }

The above will produce an output such as: Row1, Row1, Row1 Row2, Row2, Row2

Because it prints out the columns, with a space in between each element, then a new line between the rows.

Comments