2D Array

LinkedList + Stack + 2D Array

Page 1 of 1

6 Replies - 2137 Views - Last Post: 23 October 2007 - 07:05 PM Rate Topic: -----

#1 heavensgate006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-October 07

2D Array

Posted 22 October 2007 - 10:55 PM

i have here a code using Stack and Linkedlist. when i pop() the items, i have to display it in a 2d array. but there's a problem with my code. here it is. i hope you can help me. thanks.


 import java.util.NoSuchElementException;
import java.util.*;
import java.io.*;
public class Bingo1
{
	static class ListNode
		{
			Object data;
			ListNode next;

			ListNode(Object data, ListNode next)
			{
				this.data = data;
				this.next = next;
			}
	}
	static class StackList
	{
		private ListNode head;
		private int size;
		private ListNode top;

		public StackList()
		{
		top=null;	
		}
		public void push(Object item)
		{
			top = new ListNode(item,top);
		}
	
		public Object pop()
		{
			if(top!=null)
			{
				
				Object item = top.data;
				top = top.next;
				return item;	
			}
			else
			{
			throw new NoSuchElementException();
			
			}
		}
		public int size()
		{
		return size;	
		}
		public void removeFirst()
		{
		if(top!=null)
			{
				top=top.next;
				--size;
			}
			else
			throw new NoSuchElementException();	
		}
		public boolean contains(Object item)
		{
			ListNode current=top;
			while(current!=null)	
			{
			if(item.equals(current.data)){
				 return true;
				}else{
				current = current.next
						}
			}
			return false;
		}
	}

	public static void main(String[]args)throws IOException
	{
BufferedReader m = new BufferedReader(new InputStreamReader(System.in));
		
		Random rand = new Random();
		StackList stk = new StackList();
		String num[][]=new String[5][5];
		int counter=0;
		int min=0;
		
		System.out.println("----------------------------------");
		System.out.println();
		System.out.println(" B \t I \t N \t G \t O");
		System.out.println();
		System.out.println("----------------------------------");
	
		try{
			int y=0;
		do
		{
			switch(y){
				case 1: min = 1;
				break;
				case 2: min = 16;
				break;
				case 3: min = 31;
				break;
				case 4: min = 46;
				break;
				case 5: min = 61;
				break;
				}
				y++;
				}while(y<6);
		
		String str = "";
		do
		{	
		int no = rand.nextInt(14)+min;
		str = Integer.toString(no);
		   
		if(stk.contains(str)==false)
			stk.push(str);
		else{
		
			counter--;
			}
			counter++;
		}while(counter<5);
		
	
		for(int x=0;x<num.length;x++)
		{
		for(int j=0;j<num[x].length;j++){
			if(x==2 && j==2){
			num[x][j]="FREE";
			}else{
			num[x][j]=stk.pop().toString();
				}
			System.out.println(num[x][j]+"\t");
			}
				System.out.println();
			}	
	}	
	   catch(Exception ex){
	System.out.println(ex.getMessage());
			}
		
		
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: 2D Array

Posted 22 October 2007 - 11:42 PM

Can you describe the problem that you are having with your code? Include any error messages that you may be getting.
Was This Post Helpful? 0
  • +
  • -

#3 heavensgate006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-October 07

Re: 2D Array

Posted 23 October 2007 - 12:06 AM

View Postjayman9, on 22 Oct, 2007 - 11:42 PM, said:

Can you describe the problem that you are having with your code? Include any error messages that you may be getting.


the problem is i have to display it using the 2d array with 4 rows and 4 columns. but it only displays 1 column. and then in the end there's a 'null'. what should i do?
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: 2D Array

Posted 23 October 2007 - 12:32 AM

The problem appears to be with the switch-case and your first do-while loop in main().

The do while loop just runs through to completion, incrementing each time. So once it's complete, the value of min is set to 61. Then, your next loop starts up, filling the stack with random integers. However, what you want to do is fill the stack each time you go through the first do-while loop, using the current value of min. This means that you have to move the while part of the loop to AFTER your second loop, so that they are nested. You will also need to set counter to zero at the start of each loop.

And there is also a problem with your printing statements...if you call println each time, every number will appear on a newline. So in the inner part of your nested forloop, you should probably be calling just print() instead - this won't add the newline.

This is a cleaned up version that I think should run in the way you are looking for:
import java.util.NoSuchElementException;
import java.util.*;
import java.io.*;

public class Bingo1 {
	static class ListNode {
		Object data;
		ListNode next;

		ListNode(Object data, ListNode next) {
			this.data = data;
			this.next = next;
		}
	}

	static class StackList {
		private ListNode head;
		private int size;
		private ListNode top;

		public StackList() {
			top = null;
		}

		public void push(Object item) {
			top = new ListNode(item, top);
		}

		public Object pop() {
			if (top != null) {

				Object item = top.data;
				top = top.next;
				return item;
			} else {
				throw new NoSuchElementException();

			}
		}

		public int size() {
			return size;
		}

		public void removeFirst() {
			if (top != null) {
				top = top.next;
				--size;
			} else
				throw new NoSuchElementException();
		}

		public boolean contains(Object item) {
			ListNode current = top;
			while (current != null) {
				if (item.equals(current.data)) {
					return true;
				} else {
					current = current.next;
				}
			}
			return false;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader m = new BufferedReader(new InputStreamReader(System.in));

		Random rand = new Random();
		StackList stk = new StackList();
		String num[][] = new String[5][5];
		int counter = 0;
		int min = 0;

		System.out.println("----------------------------------");
		System.out.println();
		System.out.println(" B \t I \t N \t G \t O");
		System.out.println();
		System.out.println("----------------------------------");

		try {
			int y = 0;
			do {
				switch (y) {
				case 1:
					min = 1;
					break;
				case 2:
					min = 16;
					break;
				case 3:
					min = 31;
					break;
				case 4:
					min = 46;
					break;
				case 5:
					min = 61;
					break;
				}

				String str = "";
				counter = 0;
				do {
					int no = rand.nextInt(14) + min;
					str = Integer.toString(no);

					if (stk.contains(str) == false)
						stk.push(str);
					else {

						counter--;
					}
					counter++;
				} while (counter < 5);

				y++;
			} while (y < 6);

			for (int x = 0; x < num.length; x++) {
				for (int j = 0; j < num[x].length; j++) {
					if (x == 2 && j == 2) {
						num[x][j] = "FREE";
					} else {
						num[x][j] = stk.pop().toString();
					}
					System.out.print(num[x][j] + "\t");
				}
				System.out.println();
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#5 heavensgate006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-October 07

Re: 2D Array

Posted 23 October 2007 - 12:54 AM

View Postjjhaag, on 23 Oct, 2007 - 12:32 AM, said:

The problem appears to be with the switch-case and your first do-while loop in main().

The do while loop just runs through to completion, incrementing each time. So once it's complete, the value of min is set to 61. Then, your next loop starts up, filling the stack with random integers. However, what you want to do is fill the stack each time you go through the first do-while loop, using the current value of min. This means that you have to move the while part of the loop to AFTER your second loop, so that they are nested. You will also need to set counter to zero at the start of each loop.

And there is also a problem with your printing statements...if you call println each time, every number will appear on a newline. So in the inner part of your nested forloop, you should probably be calling just print() instead - this won't add the newline.

This is a cleaned up version that I think should run in the way you are looking for:
import java.util.NoSuchElementException;
import java.util.*;
import java.io.*;

public class Bingo1 {
	static class ListNode {
		Object data;
		ListNode next;

		ListNode(Object data, ListNode next) {
			this.data = data;
			this.next = next;
		}
	}

	static class StackList {
		private ListNode head;
		private int size;
		private ListNode top;

		public StackList() {
			top = null;
		}

		public void push(Object item) {
			top = new ListNode(item, top);
		}

		public Object pop() {
			if (top != null) {

				Object item = top.data;
				top = top.next;
				return item;
			} else {
				throw new NoSuchElementException();

			}
		}

		public int size() {
			return size;
		}

		public void removeFirst() {
			if (top != null) {
				top = top.next;
				--size;
			} else
				throw new NoSuchElementException();
		}

		public boolean contains(Object item) {
			ListNode current = top;
			while (current != null) {
				if (item.equals(current.data)) {
					return true;
				} else {
					current = current.next;
				}
			}
			return false;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader m = new BufferedReader(new InputStreamReader(System.in));

		Random rand = new Random();
		StackList stk = new StackList();
		String num[][] = new String[5][5];
		int counter = 0;
		int min = 0;

		System.out.println("----------------------------------");
		System.out.println();
		System.out.println(" B \t I \t N \t G \t O");
		System.out.println();
		System.out.println("----------------------------------");

		try {
			int y = 0;
			do {
				switch (y) {
				case 1:
					min = 1;
					break;
				case 2:
					min = 16;
					break;
				case 3:
					min = 31;
					break;
				case 4:
					min = 46;
					break;
				case 5:
					min = 61;
					break;
				}

				String str = "";
				counter = 0;
				do {
					int no = rand.nextInt(14) + min;
					str = Integer.toString(no);

					if (stk.contains(str) == false)
						stk.push(str);
					else {

						counter--;
					}
					counter++;
				} while (counter < 5);

				y++;
			} while (y < 6);

			for (int x = 0; x < num.length; x++) {
				for (int j = 0; j < num[x].length; j++) {
					if (x == 2 && j == 2) {
						num[x][j] = "FREE";
					} else {
						num[x][j] = stk.pop().toString();
					}
					System.out.print(num[x][j] + "\t");
				}
				System.out.println();
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

	}
}



thanks for the help. my problem now is that in B column, it should only display numbers from one to 15 randomly, in I from 16 to 30, in N from 31 to 45, in G from 46 to 60 and in column O from 61 to 75.
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: 2D Array

Posted 23 October 2007 - 03:45 AM

Ah. Guess I just don't play enough bingo :D

Unless the order in the stack is absolutely critical for you, I'd recommend that you split up your array assignment and output steps. The order that you're pushing the values into the stack doesn't really lend itself to assigning and outputting in the same step, but it's pretty simple if you separate it out into a couple of sets of nested loops. Using the same order for the values in the stack, reverse the row/col indices in the array, and reverse the direction of the outer loop:
for (int x = num.length-1; x >=0; x--) {
	for (int j = 0; j < num[x].length; j++) {
		if (x == 2 && j == 2) {
			num[j][x] = "FREE";
			stk.pop();
		} else {
			num[j][x] = stk.pop().toString();
		}
	}
}
for (int x = 0; x < num.length; x++) {
	for (int j = 0; j < num[x].length; j++) {
		System.out.print(num[x][j] + "\t");
	}
	System.out.println();
}

This also has a stk.pop() call in the body of the if statement where you're assigning the "FREE" square; without it you'll wind up with all of the following values shifted by one element, because you have an extra element in the stack. With those changes, the following output results:
----------------------------------

 B 	 I 	 N 	 G 	 O

----------------------------------
7	28	37	59	70	
3	24	34	57	69	
4	27	FREE	54	61	
12	23	38	51	64	
1	16	39	55	72

Hope that helps,

-jjh

This post has been edited by jjhaag: 23 October 2007 - 03:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 heavensgate006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-October 07

Re: 2D Array

Posted 23 October 2007 - 07:05 PM

thank you so much for your help. i finally got it right. thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1