Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Okay, turns out I do need a bit of help with my test program. He wants it to be like a menu, where he can type in a command and the program will perform the function. So I decided to do it as a switch statement thinking it would be easier. So I set up a while loop, and put the switch inside there. But now I get an infinite loop. So if I type in "push" it keeps asking for user input and adding on to the stack and never goes back to the top of the loop. How can I have the loop start over and allow the user to enter a different command?
    Java Code:
    import java.util.*;
    
    public class StackTest
    {
       public static void main(String[] args)
       {      
          Stack myStack = new Stack();
          String input;
          boolean exit = false;
          
          Scanner console = new Scanner(System.in);
    
          System.out.println("Please enter \"push\", \"peek\", \"pop\", \"get size\" to perform corresponding command.");
          System.out.println("Type \"exit\" to quit and print the stack.");
          input = console.next();
          while(exit != true)
          {
             switch(input)
             {   
                case "push": 
                   System.out.println("Enter an integer to add to stack.");
                   myStack.push(Integer.parseInt(console.next()));
                   break;
                case "peek": 
                   if(myStack.isEmpty() == true)
                      System.out.println("Stack is empty, please add a value before running peek.");
                   else
                      System.out.println("The peek value is: " + myStack.peek());
                   break;
                case "pop":
                   if(myStack.isEmpty() == true)
                      System.out.println("Stack is empty, please add a value before running pop.");
                   else
                      System.out.println("The peek value is: " + myStack.pop());
                   break;
                case "get size": 
                   System.out.println("The size of the stack is " + myStack.getSize() + " nodes");             
                   break;
                case "exit": 
                   exit = true;
                   break;
                default: System.out.println("Sorry, I didn't recognize that. Please try again.");
                   break;
             }
          }        
          myStack.printStack();        
       }
    }
    EDIT: Changed the code a little since posting this. Added statements to check to see if stack was empty before running peek and pop, and added a default.
    Last edited by Cash4Gold; 03-06-2014 at 09:55 PM.

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    There needs to be code inside of the loop that changes the value of the variable that controls the looping: input.
    If its value doesn't change, the loop will go forever.
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    Okay, that makes sense. My first attempt failed though.
    Java Code:
    case "push": 
                   System.out.println("Enter an integer to add to stack.");
                   input = console.next();
                   myStack.push(Integer.parseInt(input));
                   break;
    I thought setting input to the value of the integer entered by the user and passing it into push() would work, but that just kicks it down to the default and I get "sorry I didn't rezognize that" a thousand times. Even if that did work, it wouldn't help me in the other methods since they don't even take a second user input. Is this switch just a fundamentally poor idea?

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Need help with a UML diagram involving stacks

    Where does the code set the value of input that would cause the code to change the value of exit?
    It looks like the code that does that is OUTSIDE of the loop which means it will never be executed once execution gets inside the loop. Look at moving that code inside the loop.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    Cash4Gold is offline Member
    Join Date
    Feb 2014
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a UML diagram involving stacks

    You were right, simply moving input = console.next() inside the loop fixed it. Here's my final product
    Java Code:
    import java.util.*;
    
    public class StackTest
    {
       public static void main(String[] args)
       {      
          Stack myStack = new Stack();
          String input;
          boolean exit = false;
          
          Scanner console = new Scanner(System.in);
          
          while(exit != true)
          {
             System.out.println("Please enter \"push\", \"peek\", \"pop\", \"size\" to perform corresponding command.");
             System.out.println("Type \"exit\" to quit and print the stack.");
             input = console.next();
             switch(input)
             {   
                case "push": 
                   System.out.println("Enter an integer to add to stack.");
                   input = console.next();
                   myStack.push(Integer.parseInt(input));
                   break;
                case "peek": 
                   if(myStack.isEmpty() == true)
                      System.out.println("Stack is empty, please add a value before running peek. \n");
                   else
                      System.out.println("The peek value is: " + myStack.peek() + "\n");
                   break;
                case "pop":
                   if(myStack.isEmpty() == true)
                      System.out.println("Stack is empty, please add a value before running pop. \n");
                   else
                      System.out.println("The value popped is: " + myStack.pop() + "\n");
                   break;
                case "size": 
                   System.out.println("The size of the stack is " + myStack.getSize() + " node(s)." + "\n");             
                   break;
                case "exit": 
                   exit = true;
                   break;
                default: System.out.println("Sorry, I didn't recognize that. Please try again. \n");
                   break;
             }
          }        
          System.out.println("\n");
          myStack.printStack();        
       }
    }
    Thanks so much for all your help, I couldn't have done it without you. In the future, I'll try not to use this forum as a crutch, but I really needed help with this one for whatever reason.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Assignment involving for and while loops
    By johnWeldon in forum New To Java
    Replies: 1
    Last Post: 03-05-2013, 12:35 AM
  2. error - I think involving my Import Statements
    By Sparky in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 02:31 PM
  3. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2010, 11:21 PM
  4. Error involving DecimalFormat
    By kamikaze in forum New To Java
    Replies: 4
    Last Post: 10-18-2010, 02:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •