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.
Code :
/*
* 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
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?
1 Attachment(s)
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.
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)
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!:-bd