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

Thread: Help with hashmap and heap

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with hashmap and heap

    Hello,

    I would like to read in this file: (just a small piece of it for example)

    723 Light, Matt
    70 Mankins, Logan
    391 Maroney, Laurence
    7743 Aiken, Sam
    521 Alexander, Eric
    232 Andrews, Willie
    12 Brady, Tom
    654 Britt, Wesley
    901 Smith, Le Kevin
    8222 Spach, Stephen
    281 Spann, Antwain
    965 Thomas, Adalius
    83 Welker, Wes
    751 Wilfork, Vince

    and put it into a hashmap to save it and call it for an heap. I have gotten it to read the file, and put the number as the "key" but for the value I just put a " " (space) because how I am reading in the file, I am using " " (space) to separate the information on the line.

    Is there a way to do this with Hashmap or should I use something else. Below is my code for the hashmap and it's output.

    public static Map<Integer, String> donors = new HashMap<Integer,String>();
     
    public static void main(String[] args) throws IOException {
     
    BufferedReader donorFile = new BufferedReader(new FileReader("donor.txt"));
     
                    String[] tokens;
    		String line = "";
    		int ID = -1;
     
     
    		while ((line = donorFile.readLine()) != null) {
     
    			line = line.trim();
    			tokens = line.split(" ");
    			ID = Integer.parseInt(tokens[0]);
    			donors.put(ID, " ");
    			for (int i = 1; i < tokens.length; i++) {
    				for (int j = i+1; j < tokens.length; j++) {
    					 System.out.println("Donors: " + ID+ " " + "Pair: " +
    					tokens[i] +"  "+ tokens[j]);
     
     
    				}
    			}
    		}
    		System.out.println(donors);
    		//appearances.close();
     
    	}

    //printing the donor's ID and name 
    //but since I am spiting the line by spaces it does this
    //I want the whole name to be the "value" and the number to be the "key" 
     
    Donors: 501 Pair: Vrabel, Mike
    Donors: 941 Pair: Warren, Ty
    Donors: 15 Pair: Washington, Kelley
    Donors: 816 Pair: Moss, Randy
    Donors: 614 Pair: Neal, Steve
    Donors: 68 Pair: O'Callaghan, Ryan
    Donors: 66 Pair: Paxton, Lonie
    Donors: 35 Pair: Richardson, Mike
    Donors: 367 Pair: Sanders, James
    Donors: 570 Pair: Sanders, Lewis
    Donors: 932 Pair: Seymour, Richard
    Donors: 52 Pair: Slaughter, T.J.
    Donors: 4403 Pair: Evans, Heath
    Donors: 331 Pair: Faulk, Kevin
    Donors: 97 Pair: Green, Jarvis
    Donors: 72 Pair: Gutierrez, Matt
    Donors: 6 Pair: Hanson, Chris
    Donors: 371 Pair: Harrison, Rodney
    Donors: 10 Pair: Gaffney, Jabar
    Donors: 3 Pair: Gostkowski, Stephen
    Donors: 27 Pair: Hobbs, Ellis
    Donors: 71 Pair: Hochstein, Russ
    Donors: 17 Pair: Jackson, Chad
    Donors: 22 Pair: Jones, C.J.
    Donors: 7791 Pair: Kaczur, Nick
    Donors: 671 Pair: Koppen, Dan
    Donors: 723 Pair: Light, Matt
    Donors: 70 Pair: Mankins, Logan
    Donors: 391 Pair: Maroney, Laurence
    Donors: 7743 Pair: Aiken, Sam
    Donors: 521 Pair: Alexander, Eric
    Donors: 232 Pair: Andrews, Willie
    Donors: 12 Pair: Brady, Tom
    Donors: 654 Pair: Britt, Wesley
    Donors: 901 Pair: Smith, Le
    Donors: 901 Pair: Smith, Kevin
    Donors: 901 Pair: Le Kevin
    Donors: 8222 Pair: Spach, Stephen
    Donors: 281 Pair: Spann, Antwain
    Donors: 965 Pair: Thomas, Adalius
    Donors: 83 Pair: Welker, Wes
    Donors: 751 Pair: Wilfork, Vince
    Donors: 771 Pair: Williams, Tank
    Donors: 586 Pair: Woods, Pierre
    Donors: 990 Pair: Wright, Mike
    Donors: 74 Pair: Yates, Billy
    Donors: 1022 Pair: Thomas, Santonio
    Donors: 1611 Pair: Cassel, Matt
    Donors: 38 Pair: Eckel, Kyle
    Donors: 541 Pair: Bruschi, Tedy
    Donors: 3815 Pair: Bryant, Fernando
    Donors: 73 Pair: Martin, Jimmy
    Donors: 3138 Pair: Meriweather, Brandon
    Donors: 866 Pair: Thomas, David
    Donors: 4220 Pair: Mixon, Tim
    Donors: 348 Pair: Morris, Sammy
    Donors: 41 Pair: Ventrone, Raymond
    Donors: 842 Pair: Watson, Benjamin
    Donors: 511 Pair: Webster, Jason
     
    //printing the hashmap donors
     
    {3= , 4403= , 816= , 6= , 10= , 281= , 12= , 15= , 17= , 941= , 3815= , 22= , 
    932= , 27= , 391= , 570= , 3138= , 35= , 654= , 1611= , 38= , 41= , 521= , 
    671= , 7791= , 771= , 52= , 541= , 901= , 68= , 70= , 71= , 614= , 751= ,
     66= , 1022= , 348= , 72= , 73= , 74= , 866= , 83= , 4220= , 331= , 8222= , 
    7743= , 511= , 990= , 371= , 97= , 232= , 501= , 586= , 842= , 367= , 723= , 965= }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    Is there a way to do this with Hashmap
    What do you want as the key and what as the value for the lines in the file when they are placed in the HashMap?
    Is the problem separating the String that was read in from the file into the key and value parts?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    I want the number to be the key and the name to be the value. Because the other file (below) has numbers not names.

    I 10 990
    i 38 100
    i 371 364
    i 70 335
    I 391 12
    I 3138 77
    i 12 88
    i 521 23
    I 68 1008
    i 816 21
    c 371 364 50
    C 70 335 1009
    A Section A Row 1 Seat 10
    a Section B Row 2 Seat 5
    a Section A Row 5 Seat 9
    s 12
    S 10
    L
    I 7791 109
    i 654 33
    I 97 1472
    I 4220 445
    a Section C Row 3 Seat 1
    A Section C Row 20 Seat 3
    c 654 33 3300
    a Section W Row 7 Seat 1
    A Section W Row 9 Seat 3
    L
    B 5 Section D
    l

    I need to use a heap to do the seating arrangements with the following priorities for the heap. I honestly don't understand this part but I'm taking it one step at a time.

    I donorID priority 
    Insert the donor with ID donorID and priority code priority
    C donorID oldPriority newPriority 
    Change the priority for donorID from oldPriority to newPriority
    A seat 
    Assign the seat indicated to the individual who currently has the highest priority
    L 
    List alphabetically, everyone who has been assigned a seat, along with their seat number
    S donorID 
    Lists seat assigned to donor donorID, or state that no seat has been assigned

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    I want the number to be the key and the name to be the value.
    Are you having any problems doing that?

    I need to use a heap
    Sorry, I don't know what you mean by "heap".
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    Yes. I am having problems. I only know how to set my split to spaces, seeing that I only know how to set to spaces. the key is set correctly to the number AKA the first item in the array, but the value is split into two items. The whole name is not there. which is shown in the output that I posted earlier.

    I want to be able to scan the first part of the line and set it as the key aka the number and the rest of the line as the value aka the name.

    A friend told me to look into Scanner.
    Quote:

    "Scanner- there should be something which reads a number and something to read the remainder of the line. Start by going through the file and printing something like
    number=723 name=Light, Matt"

    But I am searching for this without much luck.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    scan the first part of the line and set it as the key aka the number and the rest of the line as the value aka the name.
    What is the format of the line that has the key and the name?
    Is this it:
    key<space>name
    where <space> is the char: ' '

    The String class has methods to:
    1) find the <space>
    2) get the substring before the <space>, the key
    3) get the substring after the <space>, the name


    NOTE: <space> is a meta symbol, not the characters that are in the String. In this case it represents the char ' '
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Help with hashmap and heap

    This sounds to me that you're asking for 2 things:

    1. Use Scanner to read from a file and split the donor ID/number from the name.

    Quote Originally Posted by aiojou View Post
    "Scanner- there should be something which reads a number and something to read the remainder of the line. Start by going through the file and printing something like
    number=723 name=Light, Matt"
    Scanner can significantly simplify your code, but its API, imo, can be quite confusing if you're not familiar with it. Do read through Scanning (The Java™ Tutorials > Essential Classes > Basic I/O).

    See also Scanner (Java Platform SE 7 ). The constructor and methods that you'll need are:
    • Scanner(File source)
    • boolean hasNext()
    • int nextInt()
    • String nextLine()

    Refer to the Scanner API documentation for the description of the above methods.

    Putting them together, you can read-and-split as follows:
    Scanner scanner = new Scanner(file);
     
    while (scanner.hasNext()) {
        int donorId = scanner.nextInt();
        String name = scanner.nextLine().trim();
        System.out.println("id:" + donorId);
        System.out.println("name:" + name);
    }
     
    scanner.close();
    where
    • file: is a File object instantiated with the path to your donor.txt file
    • scanner.hasNext(): checks if there is another token available; from the Scanner class description, "A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods."
    • nextInt(): gets the next token as an int; this assumes that each line starts with an integer (i.e., the donor ID/number)
    • nextLine(): gets the rest of the line after the donor ID (that was earlier extracted using nextInt()) up to the line separator at the end of the line; since this includes the space character that is after the donor ID, the String returned by nextLine() needs to be trimmed.
    • println(): just to show that the IDs and names have been extracted correctly.
    • scanner.close(): always close the scanner once you're done with it. (Later on you should learn that the close() method should be called in a finally block.)

    By using Scanner like the above, you can do away with your file-reading code that makes use of BufferedReader, as well as the rest of the String manipulation code.

    2. Place ID and name into a HashMap

    Quote Originally Posted by aiojou View Post
    I want to be able to scan the first part of the line and set it as the key aka the number and the rest of the line as the value aka the name.
    As you were thinking in your earlier post, after extracting the donor ID and name, you can place them into a HashMap. See HashMap (Java Platform SE 7 ). The method to use to do this is "V put(K key, V value)", where the donor ID would be the key, and the name would be the value.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    Good point about using nextLine() after nextInt()
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    Thank you I figured it out just didn't reply, but your way is cleaner. Also, is there a way to do a Heap with a Hashmap?? The only examples I see are with arraylists

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Help with hashmap and heap

    A heap and a hashmap are 2 completely different things. How would you do that? Why would you even want to have a heap?

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    Thank you Cornix for your reply. I understand that they are two different things. In my second post I stated but not completely explained that I need to do the assignment as a priority heap. I thought it would be best to save the information from the file into an hashmap. But now I am running into the of creating the heap, for every example is showing to create the heap with an arraylist.

  12. #12
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Help with hashmap and heap

    This sounds like a job for a PriorityQueue.

  13. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    I am having problems with my switch statement.

    //get the other file 
    //need to read in this file, get the first token and check it 
     
    String operation; 
     
    try {  
     
    Scanner scan = new Scanner(new File("operations.txt"));  
     
        try {  
     
           while (scan.hasNextLine()) {
     
           operation = scan.nextLine(); 
     
     
           switch(operation.toUpperCase().charAt(0)) {
     
    			    	    case 'I':
    			    	    	dName = scan.nextLine();
    			    	    	System.out.println(dName);
    			    	    case 'p':
     
    			    	}   
     
    			      }  
     
    			      if (scan.ioException() != null)  
    			      {  
    			         throw scan.ioException();  
    			      }  
    			   }  
    			   finally  
    			   {  
    			      scan.close();  
    			   }  
    			   //System.out.println(donors);
    			}
    			catch (Exception bow) 
    			{  
     
    			}

    I am trying to use a switch statement to read in the First char and then if it matches make it do specific things.
    But It is skipping. This code only prints out 7 of the 14 I's.

    output

    i 38 100
    i 70 335
    I 3138 77
    i 521 23
    i 816 21
    i 654 33
    I 4220 445

    input

    I 10 990
    i 38 100
    i 371 364
    i 70 335
    I 391 12
    I 3138 77
    i 12 88
    i 521 23
    I 68 1008
    i 816 21
    c 371 364 50
    C 70 335 1009
    A Section A Row 1 Seat 10
    a Section B Row 2 Seat 5
    a Section A Row 5 Seat 9
    s 12
    S 10
    L
    I 7791 109
    i 654 33
    I 97 1472
    I 4220 445
    a Section C Row 3 Seat 1
    A Section C Row 20 Seat 3
    c 654 33 3300
    a Section W Row 7 Seat 1
    A Section W Row 9 Seat 3
    L
    B 5 Section D
    l

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    One possible problem that the compiler should be warning you about is the missing break statement.

    Every call to nextLine() reads a line. The code only prints out every other line.
    To see all the lines, add a call to println() after every call to nextLine().
    In other words print out what's in operation.

    If you only want to read the first token on a line use the next() method.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    aiojou (April 27th, 2014)

  16. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    Thank you! That fixed it!

  17. #16
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Re: Help with hashmap and heap

    Okay another problem.

    I thought I could use the switch statements to get the first char and then when received/identified do specific things like move it to certain parts of the heap...it's not doing that. lol

    I cut the code inside to show the problem that I am having with I.
    HeapPriorityQueue<Integer,Integer> test = new HeapPriorityQueue<Integer,Integer>();
                    String operation; 
    		int change;
    		String seat;
    		String [] elems;
    		//need something for da heap cuz I passes to da heap
    		try {  
    			Scanner scan = new Scanner(new File("operations.txt"));  
    			try {  
    			      while (scan.hasNextLine()) {
    			    	   operation = scan.next(); 
     
    			    	    switch(operation.toUpperCase().charAt(0)) {
     
    			    	    case 'I':
    			    	    	dName = scan.nextLine();
    			    	    	elems = dName.split(" ");
    			    	    	System.out.println("Processing: I" + dName);
    			    	    	for (int i=0; i < elems.length; i++){
    			    	    		for(int j= i+1; j< elems.length; j++){
    			    	    			test.insert(i,j);
    			    	    		}
    			    	    	}
    			    	    	System.out.println(heap);
    			    	    	break;
    			       	}   
     
    			      }  
     
    			      if (scan.ioException() != null)  
    			      {  
    			         throw scan.ioException();  
    			      }  
    			   }  
    			   finally  
    			   {  
    			      scan.close();  
    			   }  
     
    			}
    			catch (Exception bow) 
    			{  
     
    			} 
    	}
    }

    for the above input here is the output:
    Processing: I 10 990
    [(null,null), (0,1), (0,2), (1,2)]
    // it's only putting 1's and 2's into the array. 
    //not the numbers that are coming from the file
     
    //it should be 10 and 990

    so then I tried to do an if else then statement....and that's not really working out either.
    I believe an if else then statement would work better for this...but it's skipping it entirely and just saying no input.

    	Scanner scanner = new Scanner("operations.txt");
     
    		    while (scanner.hasNextLine())
    			{
     
    			    String line = scanner.nextLine();
    			    line = line.trim();
     
    			    int index;
    			    String type;
    			    String name; 
    			    char identifier = line.charAt(0);
     
    			    if (identifier == 'I') {
    			        name = scanner.next();
    			        System.out.println("Processing: I" + name);
     
    			    } else if (identifier == 'i') {
     
     
    			    } else if (identifier == 't') {
    			        index = scanner.nextInt();
    			        type = scanner.next();
    			        int creatureT = scanner.nextInt();
    			        int weightT = scanner.nextInt();
    			        int valueT = scanner.nextInt();
     
    			    } else if (identifier == 'a') {
    			        index = scanner.nextInt();
    			        type = scanner.next();
    			        int creatureA = scanner.nextInt();
     
    			    } else {
    			        System.out.println("This is not a valid line of input");
    			    } System.out.println("Identifier: " + identifier);
    			}
     
     
     
    		/**
    		int[] intArray = new int[elems.length];
        	for(int i = 0; i < elems.length; i++) {
        	    intArray[i] = Integer.parseInt(elems[i]);
        	System.out.println(intArray);
     
        	}**/
    output for the above input

    This is not a valid line of input
    Identifier: o
    //it says not a valid input. 
    //it should print out Processing: I 10 990

    could someone tell me what I am doing wrong and which would be better to do??
    Last edited by aiojou; April 28th, 2014 at 08:27 AM.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    To make it easier to see the problem, reduce the size of the input file to a minimum: say with one good entry and one that shows the problem.
    Execute the program, copy the output and paste it here and add some comments to the output saying what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    okay. I just edited my post. Hope that makes it better.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    How can the code be tested? Where is the source file to compile and execute and the input for it?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    main.txt
    operations.txt

    here are the two files.

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    The input file is too big. Can you minimize it?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #22
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap


  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    Also please post the program's output for that input file and add comments saying what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #24
    Junior Member
    Join Date
    Apr 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with hashmap and heap

    output.txt
    operations.txt

    and updated operations file that only has one input.

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with hashmap and heap

    Try doing some debugging by adding some println statements that print out everything that is read so you can see what the computer sees when the code is executed.

    What about this:
    Also please post the program's output for that input file and add comments saying what is wrong with it and show what it should be.

    No more attachments.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Binary Heap Help
    By nguyenthd97 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2014, 11:45 AM
  2. Trouble with drawing a heap
    By IHeartProgramming in forum AWT / Java Swing
    Replies: 1
    Last Post: February 27th, 2013, 08:14 PM
  3. heap size vs heap used
    By aueddonline in forum Java Theory & Questions
    Replies: 5
    Last Post: February 10th, 2012, 01:59 PM
  4. Heap Memory
    By vamsi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 19th, 2011, 12:48 PM
  5. binary heap
    By dabdi in forum Collections and Generics
    Replies: 0
    Last Post: November 16th, 2011, 05:43 PM

Tags for this Thread