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 5 of 5

Thread: TreeSet to TreeMap Problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default TreeSet to TreeMap Problem

    I am working on a school project and I am having trouble with taking information from a text file, putting it into a treeset then converting it to a treemap.
    /*
     * This application reads listing file, stores the property type data
     * into a set, returns a map
     */
     
    package gtt1t2a;
    import java.util.*;
    import java.io.*;
     
     
    /**
     *
     * @author Monica l
     */
    public class t2a
    {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException
        {
            //prompt user for filename
            Scanner in1 = new Scanner(System.in);
            System.out.print("Enter the File Name[listings.txt]: ");
            String in1FileName = in1.nextLine();
     
            //Scanner objects for the class
            File in1File = new File(in1FileName);
            Scanner in2 = new Scanner(in1File);
     
            //create new hash set
            Set<String> propertyListings = new TreeSet<String>();
     
            //get the data from the listings file 
            while (in2.hasNext())
            {
                String propType = in2.nextLine();
                propertyListings.add(propType.toUpperCase());
                //add treeset collection to map
                Iterator<String> iter = propertyListings.iterator();
                Map<String, Integer> agentListings = new TreeMap<String, Integer>();
                while (iter.hasNext())
                {
                    String propNum = iter.next();
                    agentListings.put(propNum, new Integer(01));
                    String property = iter.next();
                    agentListings.put(property, Integer.getInteger(property));
                    String propPrice = iter.next();
                    agentListings.put(propPrice, Integer.getInteger(propPrice));
                    String agentNum = iter.next();
                    agentListings.put(agentNum, Integer.getInteger(agentNum));
                    Set<String> keySet = agentListings.keySet();
                    for (String key : agentListings.keySet())
                    {
                        Integer value = agentListings.get(key);
                        System.out.println(value);
                    }
                }
            }
     
            in1.close();
            in2.close();
     
        }
     
    }

    The error I get is:
    [ERROR] Exception in thread "main" java.util.NoSuchElementException
    at java.util.TreeMap$PrivateEntryIterator.nextEntry(T reeMap.java:1098)
    at java.util.TreeMap$KeyIterator.next(TreeMap.java:11 54)
    at gtt1t2a.t2a.main(t2a.java:47)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 9 seconds)[/ERROR]

    Any help is appriciated


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: TreeSet to TreeMap Problem

    Calls to Interator.next should be preceeded by a call to hasNext(). Right now the code presumes the initial TreeSet contains 4 entries, which it doesn't (looks like you are adding 1 entry for each line, then try to retrieve 4 entries). Do you need to parse the line of the input file first?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: TreeSet to TreeMap Problem

    I think that you are right. and with that said, how can I accomplish my goal?

    In each liine of the text file, there are four entries. The goal is to read each line. Then take the information from that line and map it to the correct agent # (the key). Attached is the sample text file.
    Attached Files Attached Files

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: TreeSet to TreeMap Problem

    Then just read each line, split/parse on the line on whitespace (or whatever delim you want - you could use String.split, StringTokenizer, etc...) and place that information into the Map keyed with the appropriate term. I don't necessarily see a need for a TreeSet. (note: I did not look at the file attachment, I suggest you post ann abbreviated version into a post if my response doesn't answer your question)

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: TreeSet to TreeMap Problem

    Thanks for all of your assistance. I will give it a shot. The purpose for the set is an assignment requirement. I am not obligated to use a treeset, i had thought that it would have been easiest.

    I will redo my code tonight after work.

    Thanks!

Similar Threads

  1. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM
  2. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM