Results 1 to 3 of 3
  1. #1
    NewbieKorean is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Hey ya'll can i has some help pweese

    I've posted same kind of problem about a half a week ago. I've failed to code this homework my way(without switch method) and decided to follow my instructor's advice and actually use switch statement method. Basically what I need to do is to put a key(string) and it's reviews(list<String>) into hashmap. key is marked with <p> and </p>. review is marked with <c> and </c>. some keys doesn't have any reviews for it, while reviews can't exist without the key. Also anything outside of these <p>,</p>,<c>, and </c> should be ignored. I know this shouldn't be a problem to code, but my coding force is apparently still weak. So i was wondering if you guys can give my code a brief look and point out things that might be causing a problem. Here is my code:

    Java Code:
    package code;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    /**
     * CSE116 Fall 2011 Homework #5
     * 
     * Like previous homework #3, this homework involves character by character
     * processing with the characters being read from a file.
     * As in homework #3 you will use the CharacterFromFileReader class to read
     * characters from a file.
     * 
     * The pedagogical goals of this homework are for you to:
     *   - practice writing loops,
     *   - practice working with the primitive type char,
     *     in a more involved way than in the previous homeworks,
     *   - improve your understanding of java.util.HashMap<K,V>,
     *   - learn to use a switch statement,
     *   - reinforce your individual code-writing skills,
     *   - improve your understanding of the usefulness of state diagrams,
     *   - have fun!
     * 
     * Your task is to define the method(s) in this class so that their
     * functionality matches what is described in their comments. The functionality
     * of your definitions will in the end be verified by JUnit tests defined
     * the in the tests package. However, the tests will not be provided until
     * next week, at which point you may run the tests from your local workspace.
     * This week we encourage you to try to write some simple tests on your own,
     * though you are not required to do so.
     * 
     * This HW will work a little bit differently that the ones to date.
     * You may work as much as you want during the week. You will be required to
     * submit YOUR solution PRIOR to coming to recitation next week.
     * In recitation next week we will introduce you to a general technique for
     * solving problems such as this.  You will then have ONE MORE WEEK to work
     * on a solution, using this new technique.
     * 
     * You write up your new solution in recitation the following week, starting
     * with an EMPTY WORKSPACE. In other words, you must understand the solution
     * well enough that you can write the solution code without any external
     * assistance. You will not be permitted to consult any notes while you write
     * up your solution in recitation.
     * 
     * Overall, the project is a review viewer for various products. The information
     * about the reviews and products will be stored in a text file in the following
     * format:
     * 
     * Products start with <p> and end with </p>
     * Reviews start with <c> and end with </c>
     * So given the input: <p>Mkay</p><c>Brown and hairy.</c>
     * The product would be: "Mkay"
     * The review would be: "Brown and hairy."
     * 
     * After a product P is encountered, all reviews up to the next product
     * are associated with P in the HashMap.  Any review without an associated
     * product should be ignored.  It is possible to have a product without 
     * any reviews.
     * 
     * Any text not enclosed in either the <p> </p> tags or the <c> </c> tags
     * must be ignored.
     * 
     * The products and reviews must be stored in a HashMap<String, List<String>>
     * where the key is the product and the value is a List containing all of the
     * reviews for the product.
     * 
     * The products and reviews and then displayed in a beautiful (light grey
     * and yellow) graphical user interface. The user can use this to browse
     * the available products and read their reviews. The user interface will
     * be supplied for you, so all you need to take care of is putting the
     * appropriate information in the HashMap.
     * 
     * Notice the perhaps unexpected behavior of the Next and Previous buttons
     * (for both products and reviews) when you change the direction of
     * browsing.  Can you explain why this happens?  If you can you have a good
     * understanding of the iterators work.  If you don't know or aren't sure,
     * ask us!  
     * 
     * Enjoy!
     * 
     */
    
    
    public class TagFileParser {
    
        /**
         * Reads a file (identified by inputFilePath), one character at a time.
         * Products start with <p> and end with </p> as explained above. 
         * Reviews start with <c> and end with </c> as explained above.
         * Any text not inside of one of these tags should be ignored.
         * 
         * You may use only CharacterFromFileReader to read characters from the
         * input file.
         * 
         * In order to simplify the code writing experience, it is recommended
         * that you use a switch statement where the case would be the state.
         * This way, you only need to worry about what happens when you are at
         * that state. You should, however, fully understand the state diagram
         * as a whole and in parts, as you will be required to complete this
         * assignment next week at the beginning of lab.
         * 
         * @param String
         *            inputPath the path on the local filesystem to the input file
         * @returns a HashMap containing the product->list of reviews mappings.
         */
        
        public HashMap<String, List<String>> fillHashMap(String inputPath) {
            CharacterFromFileReader reader = new CharacterFromFileReader(inputPath);
            HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
            List<String> rStorage = new ArrayList<String>();
            String word = "";
            String pName = "";
            int state=0;
            while(reader.hasNext()){
                char c = reader.next();
                switch (state){
                case 0: 
                    if(c=='<'){ state=1;}
                    break;
                case 1:
                    if(c=='p'){ state=2;
                        }
                    else {
                        if(c=='c'){ state=3;}
                        else{
                            if(c=='/'){ state=4;}
                            else{state=0;}
                        }
                    }
                    break;
                case 2:
                    if(c=='>'){ state=5;
                    pName="";}
                    else{state=0;}
                    break;
                case 3:
                    if(c=='>'){ state=6;}
                    else{state=0;}
                    break;
                case 4:
                    if(c=='p'){state=7;}
                    if(c=='c'){state=8;}
                    if(c!='p'||c!='c'){state=0;}
                    break;
                case 5:
                    if(c=='<'){ state=1;
                    }
                    else{ state=5;
                    word = word+c;}
                    break;    
                case 6:
                    if(c=='<'){ state=1;
                    }
                    else{ state=6;
                    word = word+c;}
                    break;
                case 7:
                    if(c=='>'){ 
                    pName= word;
                    hm.put(pName, null);
                    state=0;
                    }
                    else{ state=0;}
                    break;
                case 8:
                    if(c=='>'){ state=0;
                        rStorage.add(word);
                        hm.put(pName, rStorage);            
                    }
                    else{ state=0;}
                    }
                    break;
            }    
            
        return hm;
        }
    
    }
    Last edited by pbrockway2; 10-25-2011 at 12:20 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Hey ya'll can i has some help pweese

    When posting code, use the "code" tags: you put [code] at the start of the code and [/code] at the end. That way the code will be readable when it appears in a browser.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Hey ya'll can i has some help pweese

    Do you have a specific question? Homework dumps won't attract any (positive) attention, and I'm sure you didn't intend that.

    So does the code compile? If not, and you can't understand the compiler's messages, post them. If the program runs but doesn't do what you expect or intend at runtime, describe what behaviour you expected and what you observed (including any runtime error).

    Bear in mind that we can't see your CharacterFromFileReader and have no idea (save guesses) what it is doing. Also your data file. So an adaquate description might involve steping back from the code and constructing something that can actually be run (one approach might be to replace the fillHashMap with something that takes a CharacterSequence or String argument and adding a main() method that calls it using some data that illustrates how the method fails.)

    You should, however, fully understand the state diagram
    * as a whole and in parts
    Indeed! And, again, we can't see that diagram.

Posting Permissions

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