10 Replies Latest reply: Jul 3, 2007 1:38 PM by 807600 RSS

    A little bit of help please...

    807600
      I've been working on a project for school and I had it working pretty good, but then I did something and now it won't run. It compiles with no errors, but when I go to run it...it just doesn't do anything. Any help would be great :]
      import java.util.Scanner;
      import java.util.Stack;
       
      public class StackTest
      {    
           
           Scanner scanner = new Scanner(System.in);
          Stack<Character> stack = new Stack<Character>();
          Stack<Character> stack2 = new Stack<Character>();
          Stack<Character> junkStack = new Stack<Character>();
          String inputLine = "";
          String searchString = "";
          int junk;
                   
          public void reverse(String inputLine)
           {
                // push all characters on the stack
              for(int i = 0; i < inputLine.length(); i++)
                  {
                      char temp = inputLine.charAt(i);
                      stack.push(new Character(temp));
                  }
                  
              //Reverse the string onto another stack            
              while(stack.empty() != true)
                  {
                       stack2.push(stack.pop());
                  }
           }       
                  
                  
                  
          public void searchForString(char firstSearchLetter, char lastSearchLetter)
          {
                  for(int i = 0; i < stack2.size(); i++)
                  {
                      if(stack2.peek() == firstSearchLetter)
                      {
                            junkStack.push(stack2.pop());
                            while(stack2.peek() != lastSearchLetter)
                            {
                                 junkStack.push(stack2.pop());
                             }
                             junkStack.push(stack2.pop());
                        }
                       else
                            stack.push(stack2.pop());
                  }
            }  
            
            public int calculateJunk(Stack junkStack)
            {
                int junk = junkStack.size() - searchString.length();
                return junk;
           }
           
           
           public void main(String[] args) 
           {
              
                System.out.print("Please enter your sentence: ");
               inputLine = scanner.nextLine();
               
               StackTest st = new StackTest();
               st.reverse(inputLine);
          
               System.out.println("Stack2: " + stack2);
              System.out.println("Stack: " + stack);
      
               System.out.println("Please enter the search string: ");
              searchString = scanner.nextLine();
      
              char firstSearchLetter = searchString.charAt(0);
              char lastSearchLetter = searchString.charAt(searchString.length() - 1);
              System.out.println("first letter: " + firstSearchLetter);
              System.out.println("last letter: " + lastSearchLetter);
                       
              System.out.println("junkStack: " + junkStack);
              System.out.println("stack: " + stack);
                  
              System.out.println("junk in between: " + junk);
      
           }
      }
        • 1. Re: A little bit of help please...
          807600
          Your main() method needs to be static:
          public static void main(String [] args){ 
          Once you change the method to static you will a few compiler errors that you will need to figure out.
          • 2. Re: A little bit of help please...
            807600
            Ehh... Are you sure this ever worked? For starters, your main method is supposed to be static. Secondly, if it were static, then your main method would have to create an instance of this class in order to use it's non-static variables. I think that's it for now, I'll tell you if I think of anything else (It's just 2 AM and I'm... well... tired).
            • 3. Re: A little bit of help please...
              807600
              yeah, I agree w/ the posters above. Looks like you have static and non-static code and variables bumping heads. Think through this clearly as to what needs to be static and what doesn't.
              • 4. Re: A little bit of help please...
                807600
                the program wasn't written like this when it worked. I didn't have any methods or anything, it was just written in one class with no main or anything like that. But to do what I need it to do later on, I decided I needed to make methods so that I could call them muliple times. I'm not really good at this seeing as how I'm still new to the whole java thing. So...this may sounds like a stupid question to you, but:

                How do I create an instance of he class so that i can use the non-static variables?
                • 5. Re: A little bit of help please...
                  807600
                  How do I create an instance of he class so that i can use the non-static variables?
                  public class LikeThis {
                      private String message;
                  
                      public LikeThis(String message) {
                          this.message = message;
                      }
                  
                      public void displayMessage() {
                          System.out.println(message);
                      }
                  
                      public static void main(String[] args) {
                          LikeThis instance = new LikeThis("Hello, world!");
                          instance.displayMessage();
                      }
                  }
                  • 6. Re: A little bit of help please...
                    807600
                    okay...

                    I do have this in there: StackTest st = new StackTest();

                    What's the difference in the one I have an what you are showing me?
                    • 7. Re: A little bit of help please...
                      807600
                      Difference? I posted my code while I can't see the latest version of yours?
                      • 8. Re: A little bit of help please...
                        807600
                        public static void main(String[] args) 
                             {
                                StackTest st = new StackTest();
                        
                                System.out.print("Please enter your sentence: ");
                                 inputLine = scanner.nextLine();
                                 
                                 st.reverse(inputLine);
                            
                                 System.out.println("Stack2: " + stack2);
                                System.out.println("Stack: " + stack);
                        
                                 System.out.println("Please enter the search string: ");
                                searchString = scanner.nextLine();
                        
                                char firstSearchLetter = searchString.charAt(0);
                                char lastSearchLetter = searchString.charAt(searchString.length() - 1);
                                System.out.println("first letter: " + firstSearchLetter);
                                System.out.println("last letter: " + lastSearchLetter);
                                         
                                System.out.println("junkStack: " + junkStack);
                                System.out.println("stack: " + stack);
                                    
                                System.out.println("junk in between: " + junk);
                        
                             }
                        The only thing different from what was previously posted is that I put "static" in there and then I moved that one line up:

                        StackTest st = new StackTest();
                        • 9. Re: A little bit of help please...
                          807600
                          One thing I see you are doing wrong is that you are trying to access fields (stack1, stack2) from a static method (main). That makes no sense.
                          • 10. Re: A little bit of help please...
                            807600
                            I haven't stared at your code, but why not move the logic in your main into a method, for starters?
                            public void run() {
                                      System.out.print("Please enter your sentence: ");
                                     inputLine = scanner.nextLine();
                                     
                                     st.reverse(inputLine);
                                
                                     System.out.println("Stack2: " + stack2);
                                    System.out.println("Stack: " + stack);
                             
                                     System.out.println("Please enter the search string: ");
                                    searchString = scanner.nextLine();
                             
                                    char firstSearchLetter = searchString.charAt(0);
                                    char lastSearchLetter = searchString.charAt(searchString.length() - 1);
                                    System.out.println("first letter: " + firstSearchLetter);
                                    System.out.println("last letter: " + lastSearchLetter);
                                             
                                    System.out.println("junkStack: " + junkStack);
                                    System.out.println("stack: " + stack);
                                        
                                    System.out.println("junk in between: " + junk);
                            }
                            
                            public static void main(String[] args) {
                                StackTest st = new StackTest();
                                st.run();
                            }
                            Message was edited by:
                            BigDaddyLoveHandles