Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to print results to screen

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile How to print results to screen

    Hi! 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:



    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:



    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


    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
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to print results to screen

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

    my latest modification to print is this....


    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);
    }
    }

Similar Threads

  1. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 08:35 AM
  2. clear screen.
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 5th, 2009, 07:27 AM
  3. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM
  4. Football Results
    By RSYR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2009, 07:24 PM
  5. Is it possible to print a spade, club, heart, or diamond in java?
    By BC2210 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2009, 08:35 PM