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

    Default Hey can someone tell me what i could/should change in my code

    I'm fairly new to java and even the whole coding itself... it's now my second semester of Computer Science

    anyway the conclusion is that i'm not so good at it so far so I was wondering if someone can help me improve my code

    what i have to do is to creat a method that reads through a text file and sort them into product names and its reviews

    and then put them into the hashmap which will be viewed in a gui that i myself didn't code.

    product has "<p>" in it's beginning and "</p>" at the end so that it can be recognized as product

    and same goes to reviews but with "<c>","</c>"

    product might exist without any review though some products might have more than one review, which means there are no reviews by itself.(took me awhile to actually realize this)

    the thing is that I am supposed to use methods and structure based on switch, case, and state.

    But i want to finish coding with the one that i already started. So I was wondering if i can get some help from you guys.

    Though i thought i've finished coding in my own way, but i'm keep getting nullpointerexception when i run the gui

    this is my code so far... it also includes some descriptions on how to.

    ps. Help will be greatly appreciated. Also if some could show me how i should change my coding into the structure i'm supposed to use.(that is switch, case, states related methods)

    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> dStorage = new ArrayList<String>();
            List<String> d2Storage = new ArrayList<String>();
            List<String> rStorage = new ArrayList<String>();
            List<String> r2Storage = new ArrayList<String>();
            String pStarter = "<p>";
            String pEnder = "</p>";
            String rStarter = "<c>";
            String rEnder = "</c>";
            String word = "";
            String pName = "";
            while(reader.hasNext()){
                char c = reader.next();
                if(c=='<'){
                    if(word!=""){
                        if(dStorage.contains(rStarter)){
                            rStorage.add(word);
                            r2Storage.add(word);
                            word = "";}
                        else{
                            if(dStorage.contains(pStarter)){
                            pName= word;
                            }
                            
                        }
                    }
                }
                word = word+c;
                if(word==pStarter){
                    dStorage.add(word);
                    d2Storage.add(word);
                    word = "";
                }
                if(word==pEnder){
                    dStorage.add(word);
                    d2Storage.add(word);
                    word = "";
                }
                if(word==rStarter){
                    dStorage.add(word);
                    d2Storage.add(word);
                    word = "";
                }
                if(word==rEnder){
                    if(dStorage.contains(pStarter)){
                        if(d2Storage.size()==4){
                            hm.put(pName, r2Storage);
                            r2Storage.clear();
                            dStorage.clear();
                            d2Storage.clear();
                        }
                        else{
                            hm.put(pName, rStorage);
                            rStorage.clear();
                            dStorage.clear();
                        }
                    }
                    word = "";
                    
                }
            }
        
        return hm;
        }
    
    }



    again thank you so much :)
    Last edited by Fubarable; 10-21-2011 at 06:02 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Hey can someone tell me what i could/should change in my code

    You shouldn't compare Strings with == as this checks to see if one variable holds the same object as another, which you really don't care about. What you want to know is does one String object hold the same chars and in the same order as another, and for that I'd use the equals or equalsIgnoreCase.

    I also wouldn't ignore my instructor's recommendations:
    "In order to simplify the code writing experience, it is recommended
    that you use a switch statement where the case would be the state."

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

    Default Re: Hey can someone tell me what i could/should change in my code

    I understand that i should use switch statement but I want to try to finish coding my way before i do it again with switch statement.
    and can you see where i'm probably getting this nullpointerexception from? from my point of view i just don't see where i made a mistake/mistakes


    thank you replying so quick :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Hey can someone tell me what i could/should change in my code

    Quote Originally Posted by NewbieKorean View Post
    and can you see where i'm probably getting this nullpointerexception from? from my point of view i just don't see where i made a mistake/mistakes
    You can and should find this out:
    • Which line throws the NPE?
    • Using System.out.println statements, test which variables are null on that line (but test just before that line is called)
    • Using this information, look where or if you initialize the null variable.


    Please come back if you're still stuck after doing your debugging.

  5. #5
    NewbieKorean is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Hey can someone tell me what i could/should change in my code

    NPE is not coming from the class that i've posted, but the gui. What I want to do is to see what my hm(hashmap) has inside after going through the text file.

    I'm assuming System.out.printIn tells you what the hashmap has inside, but how would i execute the code? through interaction pane?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Hey can someone tell me what i could/should change in my code

    Quote Originally Posted by NewbieKorean View Post
    NPE is not coming from the class that i've posted, but the gui. What I want to do is to see what my hm(hashmap) has inside after going through the text file.

    I'm assuming System.out.printIn tells you what the hashmap has inside, but how would i execute the code? through interaction pane?
    Sorry, but I don't understand your question.

    And have you solved the NPE issue then? If not, it would be hard for us to help unless you post the code that causes the NPE and indicate which line throws the NPE.

Similar Threads

  1. Help please with a simple code change?!
    By kieran in forum New To Java
    Replies: 10
    Last Post: 01-13-2011, 05:32 PM
  2. Replies: 2
    Last Post: 01-09-2010, 07:05 AM
  3. Replies: 3
    Last Post: 04-14-2009, 05:44 PM
  4. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 05:30 AM
  5. Servlet code change
    By joeyxaza in forum Java Servlet
    Replies: 0
    Last Post: 02-20-2009, 06:15 PM

Posting Permissions

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