aspose file tools*
The moose likes Java in General and the fly likes Stack issue (push, pop, peak) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Stack issue (push, pop, peak)" Watch "Stack issue (push, pop, peak)" New topic
Author

Stack issue (push, pop, peak)

jo sim
Ranch Hand

Joined: Apr 24, 2006
Posts: 37
I am not able to debug this issue. but the third stack is not displaying the info it should display Please look at these files and see if you can find out what am I doing wrong and how should I fix it. Thanks alot

import java.util.*;

public class Stack {
private ArrayList list1;
/** Creates a new instance of Stack */
public Stack() {
list1 = new ArrayList();
}
public void push(Object ryan){
list1.add(ryan);
}
public Object pop(){
return list1.remove(0);
}
public int size(){
return list1.size();
}
public Object peek(){
return list1.get(0);
}

public boolean isEmpty(){
return list1.isEmpty();
}

public void print(){
String plural;
String verb;
int numElem= list1.size();
if(numElem==0)
System.out.println("Empty");
else{
verb= numElem>1?"are":"is";
plural=numElem>1?"s":"";

System.out.print("[");
for(int x=0;x<numElem;x++){
System.out.print(list1.get(x)+(x<numElem-1?",":""));
}
System.out.println("]");
System.out.println("There "+verb+" "+numElem+" element"+plural+" in the stack.");
}
}
}

===========================================================


import java.util.*;


public class Main {
//public void StringaddLarge(String str1., String str2){}

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//String augend="1234567",addend="12345";
String str1="1234567",str2="12345";
//System.out.println(addLarge(str1,str2));
//Makes the lengths of aug and add the same
int a= str1.length(),b=str2.length();
int diff = Math.abs(a-b);
String zero= (diff==0)?"":"0";

if(a<b){
for(int m=0;m<diff;m++){
str1= new String(zero.concat(str1));
}
} else{
for(int m=0;m<diff;m++){
str2= new String(zero.concat(str2));
}
}
System.out.println(str1+" "+str2);
//pushes all the info into the 2 stacks
Stack temp1= new Stack();
Stack temp2= new Stack();

Stack aug= new Stack();
Stack add= new Stack();

String f= new String();
String g= new String();
int c= (a%3==0)? a/3: a/3+1;
int d= 3;

for(int e=0;e<c;e++){
f= str1.substring(a-d>-1?a-d:0,a-d+3);
temp1.push(f);

g= str2.substring(a-d>-1?a-d:0,a-d+3);
temp2.push(g);

d+=3;
}

for(int e=0;e<c;e++){
f= (String)temp1.pop();
aug.push(f);

g= (String)temp2.pop();
add.push(g);
}
aug.print();
add.print();
//Creates the Sum
Stack sumTemp = new Stack();
int carry= 0;
String sum1;
int n;
for(int e=0;e<c;e++){
sum1=Integer.toString(Integer.parseInt(aug.pop().toString())+
Integer.parseInt(add.pop().toString())+carry);
n=sum1.length();
carry=n<4?0:1;
sumTemp.push(sum1=n<4?sum1:sum1.substring(1,4));
}
sumTemp.print();
Stack sum= new Stack();
String finalSum= new String();
String q;
//finalSum= sumTemp.pop().toString();
for(int i=0;i<c;i++){
sum.push(sumTemp.pop());
}
sum.print();
//System.out.println(finalSum);
}
}




This is the Original OUTPUT
========================
1234567 0012345
[567,234,1]
There are 3 elements in the stack.
[345,012,0]
There are 3 elements in the stack.
[912,246,1]
There are 3 elements in the stack.
[912,246,1] // THIS NEEDS TO BE 124,691,2
There are 3 elements in the stack.

==========================
How should I change it so it displays this?

[ February 08, 2007: Message edited by: jo sim ]
[ February 08, 2007: Message edited by: jo sim ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack issue (push, pop, peak)
 
Similar Threads
List Duplicated
Applet Not initialized error
String to bytes conversion.
Applet not initialized error
traffic simulation