James Calhone
James Calhone

Reputation: 3

Read in Array and Display Array

I am new to java and I am having trouble with this.I need to make 2 different programs. one to make txt file and another to execute. First program makes grid. 2nd reads txt in method and then I am supposed to call it in the main. I can't seem to figure it out.

Initial array (txt.file):

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.Collections;

public class Grid {
    public static void main(String[] args) {
        ArrayList<Integer> indNum = new ArrayList<Integer>();
        ArrayList<Integer> elemNum = (ArrayList<Integer>) indNum.clone();
        ranNum(indNum);
        ranNum(elemNum);

        String grid[][] = mGrid();
        replaceGrid(grid, indNum.get(0), elemNum.get(0));
        replaceGrid(grid, indNum.get(1), elemNum.get(1));
        replaceGrid(grid, indNum.get(2), elemNum.get(2));
        disGrid(grid);
    }

    public static String[][] mGrid() {
        String[][] Array = { { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" }       
                           };
        return Array;
    }

    public static void disGrid(String[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
    }

    public static ArrayList<Integer> ranNum(ArrayList<Integer> ran) {
        for (int i = 0; i < 5; i++) {
            ran.add((i));
        }
        Collections.shuffle(ran);
        ran.remove(4);
        ran.remove(3);
        return ran;
    }

    public static String[][] replaceGrid(String[][] array, int num1, int num2) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                array[num1][num2] = " O";
            }
        }
        return array;
    }
}

Output:

 O * * * *
 * * * * *
 * * * * *
 * * O * *
  * * * O *

three O's are placed randomly in the grid 2nd Part Read in and display by executing the array:

package Execute;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.Collections;
import java.io.*;

  public class part2 {
    public static void main(String[] args) {
        ArrayList<String> makeGrid= new ArrayList<String>();
         readFile(makeGrid);            
        System.out.print(makeGrid); 
    }

    public static void readFile(ArrayList<String> exec) {
        try {
            Scanner reader = new Scanner(new File("Grid.txt"));
            do {
                exec.add(reader.nextLine());
            }while (reader.hasNext());
        } catch (FileNotFoundException fnf) {
            System.out.println("File was not found");
        }
    }   
}

When I run this it does not store the old grid and display it. Instead it just prints out the whole code I wrote in one line. How do i execute the code and store it in arraylist so i can manipulate it?

Upvotes: 0

Views: 69

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191738

First, you would want while (reader.hasNextLine())

You can manually add the newline back if you want the arraylist to be displayed as multiple lines

exec.add(reader.nextLine() + "\n");

Or you can actually print every line rather than only the arraylist

readFile(makeGrid);
for (String line : makeGrid) {
    System.out.println(line);
}

By the way, it's not recommended to pass in mutable collections into methods as parameters. You should return a new arraylist from readFile()

Upvotes: 1

Related Questions