aspose file tools*
The moose likes Java in General and the fly likes Sets and Maps - Using 'IF' Statement to Populate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sets and Maps - Using Watch "Sets and Maps - Using New topic
Author

Sets and Maps - Using 'IF' Statement to Populate

James Rennard
Greenhorn

Joined: Feb 13, 2014
Posts: 18
Hello! I've been using some other forums for other dev needs for awhile, but never had the opportunity to do much with Java. When I was looking for a new community to post some questions and eventually help out myself, I came upon you guys!

So, to start off...this is a 'homework' question. My preference is to learn and not to be given an answer, so please divulge as much info as you can. I've got this application that needs to accept a text file, read through it, and store some of the information into a set and a map. The set seems pretty straightforward, I just scan through it and grab the right info, store it to a variable and then go to the next line. However, the map is supposed to contain a total sales amount for each sales agent. Each agent may have sold multiple properties, so they may be listed two or more times in the file. The application should grab their ID, total all sales for that ID, then go to the next agent ID and do the same thing. Then it should store that information in the map.

After playing around for awhile, I noticed that I could almost make this work using a FOR statement, but it kept repeating stuff. So I looked in some forums and books and found that people use IF statements for creating maps with specific keys. Here is what I came up with for the sets and maps, then printing out the results to a new file. I'd appreciate any feedback or thoughts as I honestly am not sure why this works. If someone could explain step by step what the IF statement is actually doing that would help, the syntax is a bit confusing to me. Thanks!

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Welcome to the Ranch.

I guess you mean this if-statement:

It does the following: First, it tries to lookup a value in the map 'propertyPriceMap' with the key 'agentID'. If the map does not contain a value for that key, then propertyPriceMap.get(agentID) will return null.

So, the condition in the if-statement basically means: if propertyPriceMap contains a value for the key agentID, then do the thing between the { and }, which is: set totalSales to the value that is already in the map for the key agentId plus the value of 'price'.

There's also an 'else' block. This will be executed if the condition in the if-statements is not true. What it does there is assign the value of 'price' to totalSales.

So, everything taken together, what this does is set the value of totalSales, so that it will be equal to what's already in the map for agentID, plus 'price', or if there's nothing in the map yet for this agentID, then just set it to 'price'.

The line after that puts the value in the map, under the key agentID.

Note that '!=' means "not equal to".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi James,
Welcome to the Ranch!

The if statement syntax is explained here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

It amounts to:


Here is the pertinent lines of code from your original post:
James Rennard wrote:

In line 2, you are getting the agentID. The condition of the if statement gets the value for the agentID from the map (remember, maps are key - value pairs, your map uses an agentID as the key (or lookup value) to store totalSales as values.) If you try to get a value for a key which has not yet been entered in the map, the map.get() method will return null. The condition checks to see if the agentID has already been entered into the map by checking if the get() method returns something that is null (propertyPriceMap.get(agentID) != null can be read as "the value for the agentID in the propertyPriceMap is not null").

If the value isn't null, there is already a number stored for the agentID. So the code to execute if the condition is true (line 9) gets the value stored in the map, adds the new price to it, and stores the result in the total.

If the condition is false (the value stored for the agentID is null, or agentID has not been added to the map yet), then the work to do (line 11) is simpler: the total of all sales for this agent must be the current price - there were no previous ones, so simply store the current price as the totalSales.

Finally, after choosing how to calculate totalSales, you store the totalSales into the map as the value for the agentID (which means if the agentID is ever reached again it will be found and the total can be added to.)

Now, the condition in line 7 might not be the best way to check if the agentID has already been inserted into the map - it isn't quite readable, and you need to know a bit about Maps to see how or why it works. You should look at the Map API (found here: http://docs.oracle.com/javase/7/docs/api/) and see if you can find a different method which might provide a better, more readable means of finding if the agentID is in the Map or not.


Steve
James Rennard
Greenhorn

Joined: Feb 13, 2014
Posts: 18
Hey guys! Thanks for the responses, they were very informative. I can more clearly understand how that IF statement works in regard to the agentID and the sale value associated with it. My confusion was coming between the IF and the DO WHILE. So, the way it is setup, the first time through the IF statement, it is basically going to ignore it? I mean, there is nothing in the map to check for until propertyPriceMap.put(agentID, totalSales); is run. Is that correct?

So, then when it steps through the file again for each line, it grabs the agentID and the price, adding price to totalSales for the agentID each time?

Is there a better way of doing this than with the IF statement?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

James Rennard wrote:... My confusion was coming between the IF and the DO WHILE.

I don't see a do {} while() structure in the code you posted. Do you mean the while() {} loop?

James Rennard wrote:So, the way it is setup, the first time through the IF statement, it is basically going to ignore it? I mean, there is nothing in the map to check for until propertyPriceMap.put(agentID, totalSales); is run. Is that correct?

It can't ignore it, but you know the if condition will be false, so the else {} block will be executed. The java runtime environment, though, isn't that smart and won't know the if condition will be false, so it will always check.

James Rennard wrote:Is there a better way of doing this than with the IF statement?

Maybe there is a different way, but I think this is a good and correct way, and it works. So I don't think you should spend any time trying to work out a different scheme (unless there is some problem you see with this one).
James Rennard
Greenhorn

Joined: Feb 13, 2014
Posts: 18
I don't see a do {} while() structure in the code you posted. Do you mean the while() {} loop?


Right, that is what I meant. I don't really see any problems with it, I was just curious if the IF statement was the way to go given that it seems weird to use it to append a value. I was thinking that maybe a FOR statement may be a good way to go. Pseudcode:::

For each agentID
Add price values

Or something of that sort. Of course, I haven't tried to make it work yet. =) Thanks again!
James Rennard
Greenhorn

Joined: Feb 13, 2014
Posts: 18
I'm including my entire code set for this application. I took your advice and looked up better ways to check for the null value in the map key and changed it. However, my application now will not output the file. It runs and doesn't bug out, but the try/catch throws the error stating that they couldn't find the file. Thoughts?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sets and Maps - Using 'IF' Statement to Populate