Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack in java not working properly?

Tags:

java

stack

I am trying to store data into the stack.Correct me if I am wrong, stack is a Last in, first out type. So, was expecting that the output of my program will be kinda reverse. But sad to say the output is different. I am not sure what part of the codes is wrong.

Please help :|

CODE:

`

public class CorruptQueue {

public static void LineUp(){

    BufferedReader br = null;

try {

    String line;
    Stack nameStack = new Stack();
    Stack statusStack = new Stack();
    Stack stack = new Stack();

    br = new BufferedReader(new FileReader("C:/Users/user/Desktop/School Files/Project/CorruptOffice/input.txt"));

    while ((line = br.readLine()) != null) {


       StringTokenizer stringTokenizer = new StringTokenizer(line, " ");

       while (stringTokenizer.hasMoreElements()) {


        String lineup = stringTokenizer.nextElement().toString();
        String name = stringTokenizer.nextElement().toString();
        String status = stringTokenizer.nextElement().toString();



            nameStack.push(name);
            statusStack.push(status);
            System.out.println("Now serving "+ nameStack.pop() + " a " + statusStack.pop());



        /*StringBuilder sb = new StringBuilder();
        sb.append("" + lineup);
        sb.append(" " + name);
        sb.append("" + status);
        sb.append("\n\n");

        System.out.println(sb.toString());*/
       }

    }

    System.out.println("Done");

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (br != null)
            br.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

    }



public static void main(String[] args) 
{
     LineUp();

} }`

INPUT:

lineup John regular

lineup Bob regular

lineup Tom regular

lineup Sarah VIP

lineup Marie VIP

lineup Joan VIP

lineup Bea VIP

lineup Hank regular

lineup Art regular

lineup Daisy VIP

lineup Marius regular

lineup Dane VIP

OUTPUT:

Now serving John a regular

Now serving Bob a regular

Now serving Tom a regular

Now serving Sarah a VIP

Now serving Marie a VIP

Now serving Joan a VIP

Now serving Bea a VIP

Now serving Hank a regular

Now serving Art a regular

Now serving Daisy a VIP

Now serving Marius a regular

Now serving Dane a VIP

Done

like image 288
user1765990 Avatar asked Jun 15 '26 10:06

user1765990


2 Answers

your stack operations seem to be here

nameStack.push(name);
statusStack.push(status);
System.out.println("Now serving "+ nameStack.pop() + " a " + statusStack.pop());

Now your stack would be reversing the names if you pushed them all before you started popping anything, but you're pushing and popping at the same time for 1 item at a time.

so in essence, you push name and status onto the stack, but before you do antying else, you just pop them back off and print them to the console.

Your stack isn't really doing anything.


If you actually want to reverse your output, take

System.out.println("Now serving "+ nameStack.pop() + " a " + statusStack.pop());

out of your initial loop, and then put it in a new loop.

like image 70
Sam I am says Reinstate Monica Avatar answered Jun 17 '26 23:06

Sam I am says Reinstate Monica


  nameStack.push(name);
  statusStack.push(status);
  System.out.println("Now serving "+ nameStack.pop() + " a " + statusStack.pop());

OK. So, you are saying, push a name onto the stack. And then immediately pop that name from the stack. So, yes, it is LIFO, but you only ever have ONE element on your stack. Try moving the pop operations completely outside your while loop. Then have another while loop that keeps popping elements until your stack is empty.

like image 21
aquinas Avatar answered Jun 17 '26 22:06

aquinas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!