-1

I am working on an assignment for my java class and part of the assignment requires reading in a .csv file that is 20x20 and inserting each string into an array.

I am trying to convert my 1d array from the initial reading in of the file into a 2d array, but I seem to be doing something wrong in my output of the data.

I made an add method, and when running the program and calling the method I only get one column of strings and listed in reverse order, but if I do a System.out.println() I don't the output I desire. I am still fairly new to this so I'm sure I just don't see the simple error, but to me, it looks correct.

the reading in of the file

try {
    Scanner fileScanner = new Scanner(toOpen);    
    while (fileScanner.hasNext()) {
        fromFile = fileScanner.nextLine();
        String temp[] = fromFile.split(" ");
        theList.add(temp[0]);
        System.out.println(fromFile);
        String[][] arr = new String[20][20];
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = temp[i];
                System.out.print(arr);
            }
        }

        System.out.println();
    }

    fileScanner.close();

my add method

public void add(String tableValue) { // Adds a new node
    Node newNode = new Node(tableValue);
    if (isEmpty()) {
        setRoot(newNode);
    } else {
        newNode.setNext(getRoot());
        setRoot(newNode);
    }
}

and my method that prints the result

public String makeString() { // A Class that makes a string
    String theString = new String();

    if (isEmpty()) {
        theString = "List is empty";
    } else {
        Node printer = getRoot();

        while (printer != null) {
            theString += printer.getTableValue() + " ";
            printer = printer.getNext();
        }
    }

    return theString;
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
C. Demar
  • 1
  • 2
  • Update: I put another sentence into may answer, guess that will help with *printing* array content. Beyond that: please give feedback about the answers you got. It is not exactly polite to drop content and then vanish in thin air. You could *accept* the "most helpful" answer at some point for example. – GhostCat Aug 04 '17 at 09:17

3 Answers3

2

I guess your problem is here:

for (int i = 0; i < arr.length; i++) {
  for (int j = 0; j < arr[i].length; j++) {
    arr[i][j] = temp[i];

This assigns the same value (temp[i]) to all slots in arr[i]. Again guessing, I think you need something like:

int tmpIndex = 0;
for (int i = 0; i < arr.length; i++) {
  for (int j = 0; j < arr[i].length; j++) {
    arr[i][j] = temp[tmpIndex];
    tmpIndex++;

In other words: you have 400 different values in temp. But your code is only assigning the first 20 values 20 times again and again.

Beyond that: System.out.print(arr); isn't doing what you expect it to do - to learn how to print arrays correctly, see this.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

As we don't know the number of lines in a file, we can start by storing each line into an ArrayList (of String[]) and then convert it into a 2D array, e.g.:

List<String[]> lines = new ArrayList<>();
while (fileScanner.hasNext()) {
    String line = fileScanner.nextLine();
    String temp[] = line.split(" ");
    lines.add(temp);
}

Now, convert it into an array:

String[][] array = new String[lines.size()][];
for(int i=0 ; i<lines.size() ; i++){
    array[i] = lines.get(i);
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Don't iterate over the list just use `Collection::toArray` with `String[][] strings = lines.toArray(new String[theList.size()][]);` – Flown Aug 04 '17 at 08:09
0

I hevent seen where you have really used your add and makeString methods, and what is the role of the theList variable.

Also, could you please send your file Content.

any way:

If this is your calling to the add method: theList.add(temp[0]); that means that you are inside an Extended class structure that you have not shown it. but Any way you have not used it to fill the 2d Array in the for loop

the Code here is also error: you insert the same element temp[i] in every line !!!!

for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = temp[i];
                System.out.print(arr);
            }
        }

You can use a dynamic structure like ArrayList to fill the Elements...

Hasan
  • 296
  • 1
  • 8
  • 23