-3

I have this method which is supposed to check if a key is in a map and then increment a value.

  papers.add(paper); //add a paper to the paper collection
    papersOrdered.get(papers); //returns the papers
    if(!(papersOrdered.containsKey(paper))) {
        paperNo = 1;
        String noPaper = Integer.toString(paperNo); //convert the paperNo integer to a String
        papersOrdered.put(papers,noPaper); //put the rooNo and the paper collection into the HashMap
        System.out.println("'" + paper + "'" + " has been added to the paper order. " + noPaper + " copy ordered" ); //a message to say a paper has been added
    }
    else{
        paperNo = paperNo ++;
        String noPaper = Integer.toString(paperNo); //convert the paperNo integer to a String
        papersOrdered.put(papers,noPaper); //put the rooNo and the paper collection into the HashMap
        System.out.println("'" + paper + "'" + " has been added to the paper order." + noPaper + " copies ordered" ); //a message to say a paper has been added
    }
    System.out.println("======================================================="); ; //a line to seperate text 
    System.out.println(" "); //a blank line

However it does not work and I do not know why?

Jesse Luke Orange
  • 1,949
  • 3
  • 29
  • 71
  • 2
    In what way doesn't it work? – Louis Wasserman Dec 14 '12 at 18:20
  • The number does not increment if you enter a key thats already in the map. – Jesse Luke Orange Dec 14 '12 at 18:21
  • Where does `paperNo` come from? It looks like your problem is that `paperNo` isn't the number you're getting out of the map. (Also, why are you converting integers to and from strings, when you could just stick with integers?) – Louis Wasserman Dec 14 '12 at 18:22
  • i down-voted your question because of "it doesn't work". Tell us how it doesn't work. compiler error? run-time error? wrong output? what? – Sam I am says Reinstate Monica Dec 14 '12 at 18:22
  • 2
    Why are you doing `paperNo = paperNo ++;`? You can simply do `paperNo++;` and the variable will be incremented. – jonhopkins Dec 14 '12 at 18:23
  • This is because i need to put an int back into a HashMap. Also paperNo is an integer variable. – Jesse Luke Orange Dec 14 '12 at 18:23
  • 3
    You do realize that `paperNo = paperNo ++;` will never actually increment the variable right? – Perception Dec 14 '12 at 18:26
  • Why is this? Surely if its 1 and i do paperNo++ it will go up to 2? – Jesse Luke Orange Dec 14 '12 at 18:28
  • 2
    @JesseLukeOrange - you used the postfix increment operator. This operator increments a variable but returns its pre-increment value. So though you, ironically, update your variable on one hand, you immediately set it back to its original value with the other. Read up some more detail [here](http://www.xyzws.com/Javafaq/how-does-postfix-increment-operator-work-with-assignment/7). – Perception Dec 14 '12 at 18:31
  • So are you saying using ++ is breaking it? What should I use instead? – Jesse Luke Orange Dec 14 '12 at 18:32
  • The ++ isn't breaking it. The way you are using it is. You can use either `paperNo++;` as the entire line, or `paperNo += 1;`, or `paperNo = paperNo + 1;` – jonhopkins Dec 14 '12 at 18:33
  • Now, I am not getting an incremented value. It just tells me that i have listed 2 string papers ordered once, I want it to say I have orderd 1 string paper, twice – Jesse Luke Orange Dec 14 '12 at 18:38
  • That is what your code says to do. If you want it to output once for every order, you might want to try looping through your HashMap.. – jonhopkins Dec 14 '12 at 18:42
  • 1
    @JesseLukeOrange about `paperNo = paperNo ++` [this](http://stackoverflow.com/questions/7911776/what-is-x-after-x-x/7911795#7911795) may interest you. – Pshemo Dec 14 '12 at 18:43
  • I would like it to, search through the orderedpapers list and instead of it printing a duplicate, add one to the number of that particular paper that's been ordered – Jesse Luke Orange Dec 14 '12 at 18:46
  • Then you should be using integers instead of strings for the values in the HashMap. And when you get a paper order that is already inside the map, you'll need to increment the value for that key, instead of using `paperNo`, which it seems would always return to 1 when you get a new order. So, as the code currently is written, if you get 10 orders for paper1 in a row, and then one order for paper2, if the next order is for paper1, it will replace the 10 orders with 2. – jonhopkins Dec 14 '12 at 18:52
  • Can you use type Integer in a HashMap? – Jesse Luke Orange Dec 14 '12 at 18:54
  • For values, yes. I think the keys have to be strings though, which is fine for what you want anyway. – jonhopkins Dec 14 '12 at 18:58

2 Answers2

0

Based on your original code, but placing integers into the HashMap values instead of strings, and incrementing the current value for a key whenever a new order is added for that type of paper.

int paperNo;
String paper;
HashMap papersOrdered;
...
papers.add(paper); //add a paper to the paper collection
papersOrdered.get(papers); //returns the papers
if(!(papersOrdered.containsKey(paper))) {
    paperNo = 1;
    papersOrdered.put(papers, new Integer(paperNo)); //put the rooNo and the paper collection into the HashMap
    System.out.println("'" + paper + "'" + " has been added to the paper order. " + paperNo + " copy ordered" ); //a message to say a paper has been added
} else{
    paperNo = papersOrdered.get(paper) + 1; //get the current value, and add 1 to it
    papersOrdered.put(papers, new Integer(paperNo)); //put the rooNo and the paper collection into the HashMap
    System.out.println("'" + paper + "'" + " has been added to the paper order." + paperNo + " copies ordered" ); //a message to say a paper has been added
}
System.out.println("======================================================="); ; //a line to seperate text 
System.out.println(" "); //a blank line
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
0

Check out Google Guava's Multiset, it does exactly this.

Tom McIntyre
  • 3,620
  • 2
  • 23
  • 32