public static Set<String> agentValue(String inputFileName)
throws FileNotFoundException
{
Set<String> tail = new TreeSet<String>();
SortedMap<String, Number> agentValues = new TreeMap<String, Number>();
Scanner in = new Scanner(new File(inputFileName));
String line = inputFileName;
String[] fields;
while (in.hasNextLine())
{
line = in.nextLine();
fields = line.split("\\s");
String agentId = (fields [3]);
Double pValue = Double.parseDouble(fields [2]);
if (agentValues.containsKey(agentId))
{
pValue += agentValues.get(agentId).intValue();
}
agentValues.put(agentId, pValue);
// Create keyMap with all keys and values
Set<String> keySet = agentValues.keySet();
for (String key : keySet)
{
Number value = agentValues.get(key);
System.out.println(key + " : " + value);
tail.add(key + " : " + value);
}
}
return tail;
}
3 replies to this topic
#1
Posted 17 February 2011 - 08:11 AM
This is a project for a class, and I am totally stuck! I've created a tree map using an input text file. I split the lines into fields so that I could work with 2 elements from the file. It appears to display correctly, but I'm getting an "ArrayIndexOutOfBoundsException: 3" error, and I'm not getting anything returned to my Set. I just can't seem to wrap my brain around this one...can someone take a look at my code and point me in the right direction please?
|
|
|
#2
Posted 17 February 2011 - 09:41 AM
I assume it's happening at this line:
String agentId = (fields [3]);
Are you sure there are 4 parts (thus, 3 spaces) in the line?
Do this after splitting it to be sure:
String agentId = (fields [3]);
Are you sure there are 4 parts (thus, 3 spaces) in the line?
Do this after splitting it to be sure:
for(int i=0 ; i<fields.length ; i++){
System.out.println(i + ": " + fields[i]);
}
#3
Posted 17 February 2011 - 11:55 AM
Hi, first: Thank you very much for your help!
That is the line it's pointing to, but I just don't get it because it is 4 parts:
110001 commercial 500000.00 101
This is what it returns with the added code you suggested:
Input file: F:\WGU\GTT1\Task2\listings.txt
0: 110001
1: commercial
2: 500000.00
3: 101
101 : 500000.0
0: 110223
1: residential
2: 100000.00
3: 101
101 : 600000.0
0: 110020
1: commercial
2: 1000000.00
3: 107
101 : 600000.0
107 : 1000000.0
0: 110333
1: land
2: 30000.00
3: 105
101 : 600000.0
105 : 30000.0
107 : 1000000.0
0: 110442
1: farm
2: 200000.00
3: 106
101 : 600000.0
105 : 30000.0
106 : 200000.0
107 : 1000000.0
0: 110421
1: land
2: 40000.00
3: 107
101 : 600000.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
105 : 30000.0
106 : 200000.0
107 : 1040000.0
0: 112352
1: residential
2: 250000.00
3: 110
101 : 600000.0
105 : 30000.0
106 : 200000.0
107 : 1040000.0
110 : 250000.0
at gtt1task2collections.Main.agentValue(Main.java:118)
at gtt1task2collections.Main.main(Main.java:59)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
Again, Thank you, any help is really appreciated!
That is the line it's pointing to, but I just don't get it because it is 4 parts:
110001 commercial 500000.00 101
This is what it returns with the added code you suggested:
Input file: F:\WGU\GTT1\Task2\listings.txt
0: 110001
1: commercial
2: 500000.00
3: 101
101 : 500000.0
0: 110223
1: residential
2: 100000.00
3: 101
101 : 600000.0
0: 110020
1: commercial
2: 1000000.00
3: 107
101 : 600000.0
107 : 1000000.0
0: 110333
1: land
2: 30000.00
3: 105
101 : 600000.0
105 : 30000.0
107 : 1000000.0
0: 110442
1: farm
2: 200000.00
3: 106
101 : 600000.0
105 : 30000.0
106 : 200000.0
107 : 1000000.0
0: 110421
1: land
2: 40000.00
3: 107
101 : 600000.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
105 : 30000.0
106 : 200000.0
107 : 1040000.0
0: 112352
1: residential
2: 250000.00
3: 110
101 : 600000.0
105 : 30000.0
106 : 200000.0
107 : 1040000.0
110 : 250000.0
at gtt1task2collections.Main.agentValue(Main.java:118)
at gtt1task2collections.Main.main(Main.java:59)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
Again, Thank you, any help is really appreciated!
#4
Posted 17 February 2011 - 03:48 PM
Post your input file, or at least until 110
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









