Hello everyone,
For the past couple of weeks, I have been working on a final project for my Java Introduction course. I pretty much have it completed, but since I have a couple of weeks before it is due, I wanted to try and improve upon it and possible get some extra credits. Before I change any code in the program, I thought I would start a basic program to to see if I could get the basic principal to work. To start, here is the code I have been practicing with.
import java.util.ArrayList; import java.util.Random; public class AnimalArrays { int index; int sheep = 2; String xPos; String yPos; String positions; ArrayList<Integer> list = new ArrayList<Integer>(5); String[] sheepCreate = new String[sheep]; public static void main(String[] args) { AnimalArrays newAnimal = new AnimalArrays(); } public AnimalArrays() { // creates a list from 1 to 5 for(int i = 1; i <= 5; i++) { list.add(i); } Random rand = new Random(); for(int count = 0; count < sheep; count++) { // creates a String[] with x and y positions for each sheep index = rand.nextInt(list.size()); xPos = Integer.toString(list.remove(index)); index = rand.nextInt(list.size()); yPos = Integer.toString(list.remove(index)); positions = xPos + "," + yPos; sheepCreate[count] = positions; System.out.println(positions); } // removes unused items from the list for(int count = 0; count < list.size(); count++) { list.remove(count); } startingGrid(); } public void startingGrid(){ // writes the numbers 1-5 along the top for (int xValue = 1; xValue < 6; xValue++) { System.out.print(" " + xValue); } // end of for statement System.out.println(""); // writes the numbers 1-5 down the left side for (int yValue = 1; yValue < 6; yValue++) { System.out.print(yValue + " "); for (int xValue = 1; xValue < 6; xValue++) { //for (int sc = 0; sc < 2; sc++) { if (xValue == (Integer.parseInt(String.valueOf(sheepCreate[0].charAt(0)))) && yValue == Integer.parseInt(String.valueOf(sheepCreate[0].charAt(2)))) { System.out.print("S "); } // end of if statement else System.out.print("* "); //} // end of inner for statement }// end of middle for statement System.out.println(""); } // end of outer for statement } // end of startingGrid method }
You sill see inside the startingGrid method, I have 3 for loops nested together (1 is commented out). As it sits, it prints the grid perfectly with the one sheep("S") in its correct position, however if I uncomment the last for loop, and change the 0 in the sheepCreate[0] to sc to get both sheep to show up, this is were it messes everything up.
I have been banging my head the entire day trying to understand why. Can anyone give me an explanation on where I went wrong.
My thought process for the above code is;
first for loop sets Y to 1
Second for loop sets X to 1
third for loop would goes through the the sheep array (2 in total) and compares the 2 values stored to the x and y value, if true, it puts an 'S' there other wise it puts a '*' there.
as the third loop is done, second loop increases by 1 until it is done then first loop increases by 1 till its done (hopefully that made sense)
The final product is to have a grid with multiple sheep shown on it.
Any help you can give will help greatly.
Thanks