Results 1 to 9 of 9

Thread: Hash-tables IO

  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default Hash-tables IO

    Help!!! What is wrong with my code? The write method is only writing 0 as the value for "Bremner's" and I think my read code is wrong too :(

    Java Code:
    	File file = new File("/Users/eric.manget/Desktop/points.txt");
    	BufferedReader in;
    	String read;
    	BufferedWriter out;
    	static Hashtable<String, String> fileObj = new Hashtable<String, String>();


    Java Code:
    	public static void writeB(Integer increase) {
    	
    		
    		
    		try {
    			
    			
    			PrintWriter out = new PrintWriter("/Users/eric.manget/Desktop/points.txt");
    		
    			
    			
    			if((fileObj.containsKey("Bremner's") == false)){
    			Integer zero = 0;
    				fileObj.put("Bremner's",zero.toString());
    				out.print(fileObj);
    				System.out.println("lol");
    				 System.out.println("Hashtable contains " + fileObj.size() + " key value pairs.");
    			}
    			else{
    			
    				System.out.println(fileObj.get("Bremner's"));
    				Integer val = Integer.parseInt(fileObj.get("Bremner's"));
    				Integer value = val + increase;
    				fileObj.put("Bremner's",value.toString());
    				out.print(fileObj);
    				
    			}
    			
    			out.close();
    		
    			
    		} catch (IOException e) {
    			
    		}
    	
    	
    	}
    	
    	public void readH(){
    		try{
    		 FileInputStream fileIn = new FileInputStream("/Users/eric.manget/Desktop/points.txt");
    		ObjectInputStream oos = new ObjectInputStream(fileIn);
    
    		 fileObj = (Hashtable)oos.readObject();
    		 
    		oos.close();
    		fileIn.close();
    		
    		}catch(Exception e){
    			
    		}
    		
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    Integer zero = 0;
    fileObj.put("Bremner's",zero.toString());
    Why would you expect to write anything else?

  3. #3
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    But that should only write the value as 0 if the hashtable does not have a "Bremner's" key:
    Java Code:
    if((fileObj.containsKey("Bremner's") == false)){

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Then something elsewhere in your code is failing. For example never ever do this:
    Java Code:
    }catch(Exception e){
    			
    }
    How do you know if something went wrong? If an exception was thrown for any reason what ever it was doing failed to occur but your code simply ignores this and continues on its merry way as if everything is rosy. At the very least add a call to printStackTrace to see if an exception was thrown or not.

  5. #5
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok well I put in the stack traces and it turned up with an error for the read method.
    Java Code:
    java.io.StreamCorruptedException: invalid stream header: 7B427265
    	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
    	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    	at HouseHockey.ScoreIO.readH(ScoreIO.java:254)
    	at HouseHockey.ScoreIO.<init>(ScoreIO.java:33)
    	at HouseHockey.GUI.main(GUI.java:27)

  6. #6
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    What I want to happen here is for the hashtable to read and populate itself with my data in the text file and then be available to write to.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Now what you have to do is search the net for what a StreamCorruptedException is, what causes it and how to fix it. Basically your data is not being read in correctly.

  8. #8
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    I couldn't figure it out. I looked it up and there were other solutions but non useful. :(

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

Similar Threads

  1. Having trouble understanding how to use Hash Tables
    By theBurgh22 in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2011, 11:19 PM
  2. hash tables vs binary tree
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 12-16-2010, 10:25 PM
  3. Java Hash Tables
    By Growler in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 05:50 AM
  4. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  5. Question about hash tables
    By behrk2 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •