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.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: Using Stacks Homework Help

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    what is actually being held in strLine after each readLine()
    To see, add a println() statement that prints out the contents of strLine after readLine() gives it a value. This line is from your code:
                  System.out.println(strLine);    //print out the code being stored

    chatAt() returns a char which is a primitive. You do not use the equals() method with primitives. Use the == operator to compare primitives.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Feb 2013
    Location
    Georgia
    Posts
    44
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Stacks Homework Help

    Ok, i did the println() portion and it looks like it is reading in the entire text file.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    If the text file only has one (or none) endline character at the end of the file then the readLine() method would read the entire text file.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Feb 2013
    Location
    Georgia
    Posts
    44
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Stacks Homework Help

    ok, here is it broken down a little better, i added another println stating the pass it was in during each loop of the while along with the pass number:

    run:
    Enter a file to be read: (EX: file.txt)
    correctFile.txt
    while (x == 32)
    Pass 0
    {
    Pass 1
    {
    	s = 34;
    Error: null
    Pass 2
    	if (s == 34)
    Pass 3
    	{
    Pass 4
    		s = s-1;
    Pass 5
    	}
    Pass 6
    }
    Pass 7
     
    Elements in stack:
    {
     
     
     
    while (x == 32)
    {
    	s = 34;
    	if (s == 34)
    	{
    		s = s-1;
    	}
    }
     
    BUILD SUCCESSFUL (total time: 4 seconds)

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    Error: null
    There needs to be a call to the printStackTrace() method in the catch block so the reason and location of the exception are printed. You need to find and fix the cause of the error.

    What is the Pass number?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Feb 2013
    Location
    Georgia
    Posts
    44
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Stacks Homework Help

    the pass number is just a variable i called x that i incremented after each time through the while and printed so i knew how many times it is reading the text file and what is being seen with each pass.

    I slightly remember the printStackTrace(), but don't remember how to implement it exactly, you mind sharing that code snippet?

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    It's a method of the Exception object passed to the catch.

    So the Pass is like the line number.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Feb 2013
    Location
    Georgia
    Posts
    44
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Stacks Homework Help

    ok, so i added it into the catch block, and found the error. it was just an error where i had placed code to display what was being stored in strLine for debugging, don't need that anymore as i added different code, error cleared. now getting:

    [/code=Java]
    run:
    Enter a file to be read: (EX: file.txt)
    correctFile.txt
    while (x == 32)
    Pass 0
    {
    Pass 1
    {
    s = 34;
    Pass 2
    if (s == 34)
    Pass 3
    {
    Pass 4
    s = s-1;
    Pass 5
    }
    Pass 6
    }
    Pass 7

    Elements in stack:
    {



    while (x == 32)
    {
    s = 34;
    if (s == 34)
    {
    s = s-1;
    }
    }

    BUILD SUCCESSFUL (total time: 5 seconds)

    [/code]

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    Is it working now?
    Why is the source printed out twice?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member
    Join Date
    Feb 2013
    Location
    Georgia
    Posts
    44
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Stacks Homework Help

    no it's still wrong, during the first pass it should find all the delimiters in the code that i'm looking for then compare them with the delimiters in the stack. It is only finding one of them instead of 5. It prints twice because I am forcing it to print the first time through so i can see how many delimiters it is suppose to find and how many it is storing. I'll repost the entire code to make it easier to read.

    package hw4vinson;
     
    import java.io.*;
    import java.util.*;
     
    //Stack class, using linked list format
    public class HW4Vinson 
    {
        public class Node
        {
            String value;
            Node next;
            Node(String val, Node n)
            {
                value = val;
                next = n;
            }
        }
     
        public Node top = null; //Top of stack
     
        //checks to see if the stack is empty
        public boolean empty()
        {
            return top == null;
        }
     
        //adds a node/element to the stack
        public void push(String s)
        {
            top = new Node(s, top);
        }
     
        //removes the head node/element from the stack, throws error if stack is empty
        public String pop()
        {
            if (empty())
            {
                throw new EmptyStackException();
            }
            else
            {
                String retValue = top.value;
                top = top.next;
                return retValue;
            }
        }
     
        //returns the head node/element from the stack, throws an error if empty
        public String peek()
        {
            if (empty())
            {
                throw new EmptyStackException();
            }
            else
            {
                return top.value;
            }
        }
     
        public String toString()
        {
            StringBuilder sBuilder = new StringBuilder();
            Node p = top;
            while(p != null)
            {
                sBuilder.append(p.value);
                p = p.next;
                if (p != null)
                {
                    sBuilder.append("\n");
                }
            }
            return sBuilder.toString();
        }
     
        //Main program
        public static void main(String[] args) 
        {
            String temp;    //store syntax
            String fileName;    //var to hold user input from keyboard
            Scanner kb = new Scanner(System.in);    //Get user input
            System.out.println("Enter a file to be read: (EX: file.txt)");
            fileName = kb.nextLine();   //Store user input from keyboard
     
            //Create a stack/linked list
            HW4Vinson st = new HW4Vinson();
     
            //What To Do:
            //1. Read in a text file and store all instances of {, [, (
            //   into the stack
            //2. Reread and print the text file, but compare the stack contents with
            //   each }, ], ) and make sure it matches.  
            //3. For instance if you read in
            //   {, {, (, {  then your pop should match with }, ), }, }
            //4. If it does not then stop the text print at the instance of bad read
            //   and display an error message stating the error and which delimeter
            //   is missing.
            //5. If no errors are found the entire text file should print
     
            try
            {
                //Open user designated file
                FileInputStream fstream = new FileInputStream(fileName);
                //Prepare files
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
     
     
                int x = 0;      //take out after debug
     
     
                //Read file line by line
                while((strLine = br.readLine()) != null)
                {
                    System.out.println(strLine);
     
                    //Used in debugging
                    System.out.println("Pass " + x);    //take out after debug
                    x++;    //take out after debug
     
     
                    //check for symbol matches on each line and push onto stack
                    if("{".equals(strLine) || "[".equals(strLine) || "(".equals(strLine))
                    {
                    //push matching symbols onto our st stack
                        if ("{".equals(strLine))
                        {
                            temp = strLine;
                            st.push(temp);
                            System.out.println(st);
                        }
                        else if ("[".equals(strLine))
                        {
                            temp = strLine;
                            st.push(temp);
                            System.out.println(st);
                        }
                        if ("(".equals(strLine))
                        {
                            temp = strLine;
                            st.push(temp);
                            System.out.println(st);
                        }
                    }
                }
                //close the input stream
                in.close();
            }
            catch(Exception e)
            {
                System.err.println("Error: " + e.getMessage());
                e.printStackTrace(System.out);
            }   
     
            //Print out all the elements being stored in the stack for
            //error checking
            System.out.println("\nElements in stack:");
            System.out.println(st);
            System.out.println("\n\n"); //skip a few lines
     
     
    //---------------------------------------------------------------------------//
    //---------------------------------------------------------------------------//
    //---------------------------------------------------------------------------//
     
            //Read file again to check for any erros in syntax
            try
            {
                //reopen user designated file
                FileInputStream fstream = new FileInputStream(fileName);
                //Prepare files
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
     
                //Read file line by line
                while((strLine = br.readLine()) != null)
                {
                    System.out.println(strLine);    //print out the code being stored
     
                    //check for symbol match
                    if("}".equals(strLine))
                    {
                        String holder = st.pop();   //pop and store head element
                        System.out.println(st);
                        //if symbol coming off stack matches the improper syntax 
                        //throw error message
                        if("(".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                        else if("[".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                    }
     
                    //check for symbol match on "]"
                    else if("]".equals(strLine))
                    {
                        String holder = st.pop();   //pop and store head element
                        System.out.println(st);
                        //if symbol coming off stack matches the improper syntax 
                        //throw error message
                        if("{".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                        else if("(".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                    }
     
                    //check for symbol match
                    else if(")".equals(strLine))
                    {
                        String holder = st.pop();  //pop and store head element
                        System.out.println(st);
                        //if symbol coming off stack matches the improper syntax 
                        //throw error message
                        if("{".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                        else if("[".equals(holder))
                        {
                            //throw an error
                            System.out.println("Error Missing Syntax");
                        }
                    }
                }
                //close the input stream
                in.close();
            }
            catch(Exception e)
            {
                System.err.println("Error: " + e.getMessage());
                e.printStackTrace(System.out);
            }    
        }
    }
     
     
    /*
    This is the file i'm using to test code:
     
    while (x == 32)
    {
    	s = 34;
    	if (s == 34)
    	{
    		s = s-1;
    	}
    } 
     
     
    running the above text file i get the output:
     
    run:
    Enter a file to be read: (EX: file.txt)
    correctFile.txt
    while (x == 32)
    Pass 0
    {
    Pass 1
    {
    	s = 34;
    Pass 2
    	if (s == 34)
    Pass 3
    	{
    Pass 4
    		s = s-1;
    Pass 5
    	}
    Pass 6
    }
    Pass 7
     
    Elements in stack:
    {
     
     
     
    while (x == 32)
    {
    	s = 34;
    	if (s == 34)
    	{
    		s = s-1;
    	}
    }
     
    BUILD SUCCESSFUL (total time: 5 seconds)
     
     */

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Stacks Homework Help

    How does the code in post#35 look at brackets that are surrounded by other code?

    What has changed? It looks like the code reads through the file two times.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Stacks
    By RoshanDiMatteo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 3rd, 2012, 11:22 AM
  2. Stacks
    By RoshanDiMatteo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 3rd, 2012, 10:21 AM
  3. stacks
    By bd0652 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 10th, 2012, 11:48 AM
  4. Help with Stacks
    By animelook in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 12:58 AM
  5. help for stacks
    By msa0127a in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 3rd, 2010, 12:11 AM