Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Tis annoying (java)

Options
  • 17-12-2001 6:45pm
    #1
    Registered Users Posts: 1,684 ✭✭✭


    ok here is the code.
    import java.io.*;
    public class hashtable
    {
      static int [ ] table; 	//  hash table array
      static int divisor;			//  hash function divisor
    
    
    	public static void main(String[] args) throws Exception
    	{
      BufferedReader kbd = new BufferedReader(new InputStreamReader (System.in));
    
    	int eventType;
      int num = 0;
      int j;
      int i;
      int HashEntry;
      int size;			//  number of elements in table
      int index;
    
            System.out.println("Program working correctly");
     System.out.println("\n\nHow many buckets do you want in the hash table?\n enter number of buckets must be between 5 and 15: ");
     divisor =     Integer.parseInt (kbd.readLine());
               table = new int [divisor];
               do {
      System.out.println("..................................");
    	System.out.println("Enter your option ");
    	System.out.println("1. Add integer ");
    	System.out.println("2. Remove integer ");
    	System.out.println("3. Search for integer ");
      System.out.println("4. Exit ");
      System.out.println("..................................");
    
    	eventType = Integer.parseInt (kbd.readLine());
    
    	switch(eventType)
    	   {
    
    	  case 1:
    		System.out.println("The number you want to add is?\n please eneter number");
        num = Integer.parseInt (kbd.readLine());
        put(num);
                   display();
    		break;
    	   case 2:
    		System.out.println("Which integer are you searching for\n\n enter the integer to begin search ");
        num = Integer.parseInt (kbd.readLine());
        index = get(num);
    
    		break;
        case 3:
    		System.out.println("select integer to be removed ");
          num = Integer.parseInt (kbd.readLine());
    
          display();
    		break;
    
        
        case 4:
    
         break;
    	   default:
    		System.out.println("Invalid selection ");
    	}      //  end of switch
      }
      while ( num != 4);
    }  //  end of main method
    ////////////////////////////////////////////////////////////////////////////////
                  //  method put
    public static void put(int theKey)
    {
    	//search the table for a matching element
    	int b = search(theKey);
    
    	//  check if matching element found
    	if (table[b] == 0)
    	{
    		//  no matching element and table is not full
    		table [b] = theKey;
    	}
    	else
      {
    		 	throw new IllegalArgumentException("table is full");
      }
    } //  end of put
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    //  Display the array contents
    
       public static void display()  {
    
    
    
          for(int j=0; j<divisor; j++)           // for each element,
             System.out.print(table[j] + "  ");   // display it
          System.out.println("");
       }  //  end display method
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
        //  method search
    private static int search(int theKey)
    {
    	int i = (theKey) % divisor;  	// home bucket
    	int j = i;					// start at home
    	do
    	{
    		if (table[j] == 0 || table[j] == theKey)
    			return j;
    		j = (j+1) % divisor;		//  next bucket
    	} while (j!= i);			//  returned to home bucket
    	return j;				// table full
    }
    ////////////////////////////////////////////////////////////////////////////////
    
    
    ////////////////////////////////////////////////////////////////////////////////
    //  method get
    public static int get(int theKey)
    {
    	//search the table
    	int b = search(theKey);
    
    	//  see if a match was found at table [b]
    	if (table[b] == 0 || table[b] != theKey)
    		return -1;			//  no match
    
    	return b;		//  matching element
    }
    
    }
    ////////////////////////////////////////////////////////////////////////////////
    
     //  end of class
    
    problem it wont work properly and i know i have some stuff spelt wrong but only in print out lines? any ideas lads help would be nice after me work still have to add some stuff to it like if int != 5 or 15 for it to say no u bitch wrong number.


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    First off your menu is wrong Remove and Search are mixed up. Only a little thing.

    Not sure what you mean by it's not working either because it does run, everything else is just symantics.

    The add works just fine, but the remove is just a 'get' it doesn't actually remove the number. Write a method called remove(int n) that just sets table[n] to 0.

    Your search seems to be ok too, didn't take much of a look at it.

    For case 4 just call System.exit(0);
    That will quit the program, watch out though, if someone enters four at any time the program will exit. That's because the loop is while (num != 4)


    Another thing, usually a put in a hashtable returns the value it is replacing, if it is replacing something. Can be useful.


  • Registered Users Posts: 1,684 ✭✭✭Kraken


    Yeah i coped the messed up in remove and search.
    as for it working well the case 4 thing is ****ed up.
    the remove wasent working. have fixed some of these errors. but still not right. oh wel have to plough on like a big farmer i guess.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Why not use case 0 to exit? Or 999 or something that no one would be typing in for the duration of the application.


  • Registered Users Posts: 1,684 ✭✭✭Kraken


    while ( eventType != 4);

    simple syntax error worked the other way but now at least it wont exit every time u enter 4.

    remove wont work here ill post the new code. still unfinished
    import java.io.*;
    public class hashtable
    {
      static int [ ] table; 	//  hash table array
      static int divisor;			//  hash function divisor
    
    
    	public static void main(String[] args) throws Exception
    	{
      BufferedReader kbd = new BufferedReader(new InputStreamReader (System.in));
    
    	int eventType;
      int num = 0;
      int j;
      int i;
      int HashEntry;
      int size;			//  number of elements in table
      int index;
    
            System.out.println("Program working correctly");
     System.out.println("\n\nHow many buckets do you want in the hash table?\n enter number of buckets must be between 5 and 15: ");
     divisor =     Integer.parseInt (kbd.readLine());
               table = new int [divisor];
               do {
      System.out.println("..................................");
    	System.out.println("Enter your option ");
    	System.out.println("1. Add integer ");
    	System.out.println("2. Remove integer ");
    	System.out.println("3. Search for integer ");
      System.out.println("4. Exit ");
      System.out.println("..................................");
    
    	eventType = Integer.parseInt (kbd.readLine());
    
    	switch(eventType)
    	   {
    
    	  case 1:
    		System.out.println("The number you want to add is?\n please enter number");
        num = Integer.parseInt (kbd.readLine());
        put(num);
                   display();
    		break;
    	   case 2:
    		System.out.println("Which integer are you want to remove\n\n enter the integer to begin search ");
        num = Integer.parseInt (kbd.readLine());
                 display();
    
    		break;
        case 3:
    		System.out.println("select integer to be searched for \n\n enter the integer to begin search ");
              num = Integer.parseInt (kbd.readLine());
                 index = get(num);
                     if (index == -1)
                     System.out.println("Key not found");
                     else
    		break;
    
    
        case 4:
    
         break;
    	   default:
    		System.out.println("Invalid selection ");
    	}      //  end of switch
      }
      while ( eventType != 4);
    }  //  end of main method
    ////////////////////////////////////////////////////////////////////////////////
                  //  method put
    public static void put(int theKey)
    {
    	//search the table for a matching element
    	int b = search(theKey);
    
    	//  check if matching element found
    	if (table[b] == 0)
    	{
    		//  no matching element and table is not full
    		table [b] = theKey;
    	}
    	else
      {
    		 	throw new IllegalArgumentException("table is full");
      }
    } //  end of put
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    //  Display the array contents
    
       public static void display()  {
    
    
    
          for(int j=0; j<divisor; j++)           // for each element,
             System.out.print(table[j] + "  ");   // display it
          System.out.println("");
       }  //  end display method
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
        //  method search
    private static int search(int theKey)
    {
    	int i = (theKey) % divisor;  	// home bucket
    	int j = i;					// start at home
    	do
    	{
    		if (table[j] == 0 || table[j] == theKey)
    			return j;
    		j = (j+1) % divisor;		//  next bucket
    	} while (j!= i);			//  returned to home bucket
    	return j;				// table full
    }
    ////////////////////////////////////////////////////////////////////////////////
    
    
    ////////////////////////////////////////////////////////////////////////////////
    //  method get
    public static int get(int theKey)
    {
    	//search the table
    	int b = search(theKey);
    
    	//  see if a match was found at table [b]
    	if (table[b] == 0 || table[b] != theKey)
    		return -1;			//  no match
    
    	return b;		//  matching element
    }
    
    }
    ////////////////////////////////////////////////////////////////////////////////
    
     //  end of class
    

    if u could post the correct code for the areas it would be great.
    System.out.println("select integer to be searched for \n\n enter the integer to begin search ");
              num = Integer.parseInt (kbd.readLine());
                 index = get(num);
                     if (index == -1)// this here is only the start of a bigger piece cant rember off hand why but think it is supposed to say something like if thekey u entered is != to any key in table print not found else print thekey +position just cant figure out how exactly to do that.YET
                     System.out.println("Key not found");
                     else
    		break;
    


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    OK your put should be something like this:
    [PHP]
    public static int put(int key, int value) throws IllegalArgumentException
    {
    int previousValue = 0;
    if (key > table.size)
    {
    throw new IllegalArgumentException("Key too big");
    }
    else
    {
    int previousValue = table[key];
    table[key] = value;
    }
    return previousValue;
    }
    [/PHP]
    This just checks to see if the key (index) will fit in the table. If it's ok it will store the previous value (so it can be returned) and sets the new value.

    Your search method would be something like this:
    [PHP]
    public static int search (int key) throws IllegalArgumentException
    {
    int returnValue = 0;
    if (key > table.size)
    {
    throw new IllegalArgumentException("Key too big");
    }
    else
    {
    returnValue = table[key];
    }
    return returnValue;
    }

    .
    .
    .
    // use it like this
    try
    {
    if (search(key) >= 1)
    {
    // on the premise that no one's going to 'put' a value of 0
    System.out.println("Found it");
    }
    }
    catch (IllegalArgumentException iae)
    {
    // whatever
    }
    [/PHP]

    Your remove should be simple enough:
    [PHP]
    public static int remove(int key) throws IllegalArgumentException

    {
    int previousValue = 0;
    if (key > table.size)
    {
    throw new IllegalArgumentException("Key too big");
    }
    else
    {
    previousValue = table[key];
    table[key] = 0;
    }
    return previousValue;
    }
    [/PHP]

    Hope this helps.


  • Advertisement
  • Registered Users Posts: 1,684 ✭✭✭Kraken


    Cheers mate will try it out now.
    Looks good. understand it all which is even better. dont know why it did not dawn on me to try it b4.

    cheers again


Advertisement