CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2010
    Posts
    4

    Smile How to print results to screen

    First I want to say that I am a Java beginner and this is my 2nd program.

    I created a program that will do a symbol balance test, but I need someone to help me print the results to the screen because I am getting a blank output whenever I try to find a way to do it. I am also reading into a file. Any help is appreciated please.

    test.txt looks like {{}}

    For ex,

    { { } } = the symbols are equally balanced
    { [ { } = the symbols are not equally balanced

    I need someone to help me find a way to print the results like

    {{}} are equally balanced
    or
    {[}} are not equally balanced


    I tried using this and placing it near the bottom of the program in between the last brackets




    Code:
    while (!s.empty())
    {
    System.out.println(s.pop());
    if (failed == true)
    {
    System.out.println(" symbols do not match");
    }
    else
    {
    System.out.println(" The symbols match");
    }



    Main Program:


    Code:
    import java.io.* ;
    
    public class Stackmain 
    {
    
        public Stackmain() 
        {
        	
        	
        	
        }
        public static boolean main(String expression) throws IOException 
        {
        
        final char LEFT_PARENT = '(';
        final char RIGHT_PARENT = ')';
        final char LEFT_CURLY = '{';
        final char RIGHT_CURLY = '}';
        final char LEFT_SQUARE = '[';
        final char RIGHT_SQUARE = ']';
        
        Stack s = new Stack(100);
        char ch;
        int i = 0;
        boolean failed = false;
        
        FileInputStream fstream = new FileInputStream("test.txt");
    
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
      
        while ((ch = (char)br.read()) != (char)-1 )
           if (!s.full()) 
           {
             for (i=0; !failed && (i < expression.length( )); i++)
              {
           	     switch (expression.charAt(i))
           	     {
           		  case LEFT_PARENT:
           		  case LEFT_CURLY:
           		  case LEFT_SQUARE:
           		       s.push(expression.charAt(i));
           		       break;
           		  case RIGHT_PARENT:
           		 if (s.empty() || (s.pop() != LEFT_PARENT))
           			   failed = true;
           			   break;
           	  	  case RIGHT_CURLY:
           		 if (s.empty() || (s.pop() != LEFT_CURLY))
           			   failed = true;
           			   break;
           		  case RIGHT_SQUARE:
           		 if (s.empty() || (s.pop() != LEFT_SQUARE))
           			   failed = true;
           			   break;     			 	
             	} 	
              }
           	}
           	   return (s.empty() && !failed);
         }             
    }




    Stack Class


    Code:
    class Stack {
       private int maxStack;
       private int emptyStack;
       private int top;
       private char[] items;
    
    
    
    
       public Stack(int size) {
          maxStack= size;
          emptyStack = -1;
          top = emptyStack;
          items = new char[maxStack];
       }
    
       
    
       public void push(char c) {
          items[++top] = c;
       }
    
       public char pop() {
          return items[top--];
       }
    
       public boolean full()  {
          return top + 1 == maxStack;
       }
    
       public boolean empty()  {
          return top == emptyStack;
       }
    }

  2. #2
    Join Date
    Feb 2010
    Posts
    4

    Re: How to print results to screen

    I still receive a blank screen when I try to print...

    My latest modification to try to print is below:


    Code:
    import java.io.* ;
    
    public class Stackmain 
    {
    
       // public Stackmain() 
       // {
        	
        	
       // }
        public static boolean main(String expression) throws IOException 
        {
        
        final char LEFT_PARENT = '(';
        final char RIGHT_PARENT = ')';
        final char LEFT_CURLY = '{';
        final char RIGHT_CURLY = '}';
        final char LEFT_SQUARE = '[';
        final char RIGHT_SQUARE = ']';
        
        Stack s = new Stack(100);
        char ch;
        int i = 0;
        boolean failed = false;
        
        FileInputStream fstream = new FileInputStream("test.txt");
    
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
      
      
        while ((ch = (char)br.read()) != (char)-1 )
        {
           if (!s.full()) 
           {
    
             for (i=0; !failed && (i < expression.length( )); i++)
              {
           	     switch (expression.charAt(i))
           	     {
           		  case LEFT_PARENT:
           		  case LEFT_CURLY:
           		  case LEFT_SQUARE:
           		       s.push(expression.charAt(i));	  
           		       break;
           		  case RIGHT_PARENT:
           		 if (s.empty() || (s.pop() != LEFT_PARENT))
           			   failed = true;
           			   break;
           	  	  case RIGHT_CURLY:
           		 if (s.empty() || (s.pop() != LEFT_CURLY))
           			   failed = true;
           			   break;
           		  case RIGHT_SQUARE:
           		 if (s.empty() || (s.pop() != LEFT_SQUARE))
           			   failed = true;
           			   break;    
           			   
           			   			 	
             	} 	
    
              }
         }
             
    }
    
    
    if (failed == true)
    {
    	System.out.println("Expression does not match");
    }
    if (failed == false)
    {
    	System.out.println("Expression does match");
    }
    
    return (s.empty() && !failed);
    }
    }

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to print results to screen

    You're getting a blank screen because you aren't running anything. To run your class you need a main method with this signature:
    Code:
    public static void main(String[] args)
    Whilst looking at the code I also noticed
    • You are reading in a file and then not doing anything with the character you've just read in.
    • You are using a stack but never putting anything into it.
    • A better way to write the print out is section is:
      Code:
      if (failed)
      {
      	System.out.println("Expression does not match");
      }
      else
      {
      	System.out.println("Expression does match");
      }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Feb 2010
    Posts
    4

    Re: How to print results to screen

    Thank you for the reply. I will look into it now. I appreciate it!

  5. #5
    Join Date
    Feb 2010
    Posts
    4

    Re: How to print results to screen

    3 errors:
    cannot find symbol variable expression
    cannot return a value from method whose result type is void
    variable expression might not have been initialized


    Question: When I deleted public static boolean main(String expression), I was using expression in my for loop before. Is there something that I can use to replace it?


    Here is my latest code:


    Code:
    import java.io.* ;
    
    class Stackmain 
    {
        public static void main(String[] args) throws IOException 
        {
        
        final char LEFT_PARENT = '(';
        final char RIGHT_PARENT = ')';
        final char LEFT_CURLY = '{';
        final char RIGHT_CURLY = '}';
        final char LEFT_SQUARE = '[';
        final char RIGHT_SQUARE = ']';
        
        Stack s = new Stack(100);
        char ch;
       // String expression;
        int i = 0;
        boolean failed = false;
        
        FileInputStream fstream = new FileInputStream("test.txt");
    
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
      
      
        while ((ch = (char)br.read()) != (char)-1 )
        {
           if (!s.full()) 
           {
    
             for (i=0; !failed && (i < expression.length( )); i++)
              {
           	     switch (expression.charAt(i))
           	     {
           		  case LEFT_PARENT:
           		  case LEFT_CURLY:
           		  case LEFT_SQUARE:
           		       s.push(expression.charAt(i));	  
           		       break;
           		  case RIGHT_PARENT:
           		 if (s.empty() || (s.pop() != LEFT_PARENT))
           			   failed = true;
           			   break;
           	  	  case RIGHT_CURLY:
           		 if (s.empty() || (s.pop() != LEFT_CURLY))
           			   failed = true;
           			   break;
           		  case RIGHT_SQUARE:
           		 if (s.empty() || (s.pop() != LEFT_SQUARE))
           			   failed = true;
           			   break;    
           			   
           			   			 	
             	} 	
    
              }
         }
             
    }
    
    
    if (failed == true)
    {
    	System.out.println("Expression does not match");
    }
    if (failed == false)
    {
    	System.out.println("Expression does match");
    }
    
    //return (s.empty() && !failed);
    }
    }

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to print results to screen

    I was using expression in my for loop before. Is there something that I can use to replace it?
    Code:
    public static void main(String[] args) throws IOException 
    {
    String expression = null;
    
    if ( args.length > 0 )
        {
        expression = args[0];
        }
    
    ...
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured