Hello,
I'm a beginer in Java. I have writen a program but it doesn't work correctly. Problem is with input float value to variable ITEM. Plz read my code and give your advice. Thks so much!
1st file (InStack.java) (interface)
[code]
package lab1_1;
public interface InStack {
void push(float item);
float pop();
}
[/code]
2nd file (Main.java)
[code]
package lab1_1;
import java.io.*;
class DynStack implements InStack {
private float stck[];
private int tos; //top of stack
DynStack(int size) {
stck=new float[size];
tos=-1;
}
public float pop() {
if (tos<0) {
System.out.println("stack is empty");
return 0;
}
else return stck[tos--];
}
public void push(float item) {
if (tos==stck.length-1) {
float temp[]=new float[stck.length*2];
for (int i=0; i<stck.length; i++) temp[i]=stck[i];
stck=temp;
}
stck[++tos]=item;
}
};
public class Main {
public static void main(String[] args) throws IOException {
int choice;
float item;
DynStack mystack = new DynStack(5);
do {
System.out.println("Enter your choice: ");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Exit");
System.out.println("Your choice: ");
choice=(int)System.in.read();
if (choice==1) {
System.out.println("Enter value to push");
item=(float)System.in.read();
mystack.push(item);
}
else if (choice==2) {
System.out.println(mystack.pop());
}
} while (choice!=3);
}
}
[/code]