Monday 15 April 2013

java - JOptionDialog display Stacks How to? -



java - JOptionDialog display Stacks How to? -

okay guys need lab display stacks made in joptionmessagedialog box , have no thought how display stack. maintain displaying @jkh24k54 instead of set of integers.??

my code:

import java.util.random; import javax.swing.joptionpane; public class lab5 { public static void main(string[] arg) { random rnd = new random(); stack<integer> stack = new istack<integer>(); stack<integer> stack0= new istack<integer>(); stack<integer> stack1= new istack<integer>(); stack<integer> stack2= new istack<integer>(); int stream; for(int i=0;i<20;i++) { stream = rnd.nextint(101); stack.push(stream); stack2.push(stream); } while( !stack2.isempty()) { int x = stack2.pop(); stack.push(x); } for(int i=0; i<20; i++) { int x= stack.pop(); if(x%3==0) stack0.push(x); if(x%3==1) stack1.push(x); if(x%3==2) stack2.push(x); } while( !stack.isempty() ) system.out.print(stack.pop()+" "); system.out.println(); while( !stack0.isempty() ) system.out.print(stack0.pop()+" "); system.out.println(); while( !stack1.isempty() ) system.out.print(stack1.pop()+" "); system.out.println(); while( !stack2.isempty() ) system.out.print(stack2.pop()+" "); system.out.println(); joptionpane.showmessagedialog(null, "original stack: "+ stack.tostring()+ "\n"+ " 0%3: "+stack0.tostring()+"\n"+ "1%3: "+stack1.tostring()+"\n"+ " 2%3: "+stack2.tostring());

} }

your calling default tostring() method of object prints memory location default why getting @jkh24k54. need override tostring() method of stack class homecoming string of elements in array.

an example: add together stack class.

@override public string tostring() { string returnstring = ""; for(int = 0; < yourarray.length; i++) { if(yourarray[i] != null) { returnstring += yourarray[i]; } ////if want add together spaces null add together else { returnstring += " "; } ////the else not necessary doing } homecoming returnstring; }

java dialog stack message joptionpane

No comments:

Post a Comment