Click here to Skip to main content
15,889,281 members
Home / Discussions / Java
   

Java

 
GeneralRe: POI ShrinkToFit Solution Pin
42774801-Oct-09 9:43
42774801-Oct-09 9:43 
GeneralImage in Excel Header/Footer Solution Continuation Pin
42774803-Oct-09 4:13
42774803-Oct-09 4:13 
GeneralRe: Image in Excel Header/Footer Solution Continuation Pin
42774803-Oct-09 9:00
42774803-Oct-09 9:00 
Questionhow to set thread affinity in JAVA Pin
evyatar.v25-Sep-09 11:13
evyatar.v25-Sep-09 11:13 
AnswerRe: how to set thread affinity in JAVA Pin
427748025-Sep-09 19:29
427748025-Sep-09 19:29 
GeneralRe: how to set thread affinity in JAVA Pin
evyatar.v25-Sep-09 21:40
evyatar.v25-Sep-09 21:40 
GeneralRe: how to set thread affinity in JAVA Pin
427748026-Sep-09 0:05
427748026-Sep-09 0:05 
QuestionNeed help with hash table Pin
snssewell24-Sep-09 22:21
snssewell24-Sep-09 22:21 
We have to write a program that adds records to, searches records in, and deletes records from a hash table. So I have a little something but it is acting weird. Sometimes it only lets me add like 4 names in a row and then it will stop. Also in the example I have in the book they use a delete marker '#' to delete a record. I can mark the record to be deleted but it is not deleting it. Another problem I have is that if the user a enters a name that does not exist, it is supposed to display " record not found" but it does not do that. And last issue is that I would like the names to print in one column
First Name
Second Name
Third Name
Fourth Name ....etc.

Instead of the way it is printing it as a table. Is that possible? I got as far as I did thanks to an example in my book which was a bit different so there may still be some code in there that don't need to be. But I had to start somewhere....doing Baby Steps here. Any help is appreciated.

Here is what I got, it compiles and runs somewhat....at least until it doesn't do anything anymore. Laugh | :laugh:

import java.io.*;
   import java.io.File;

    public class HashingUnit8{
      private final int bucketSize = 2, tableSize = 3, stringLength = 20;
      private final int recordLength = stringLength;
      private final byte empty = ' ', delMarker = '#';
      private long[] positions;
      private BufferedReader buffer = new BufferedReader(
                                    new InputStreamReader(System.in));
      private RandomAccessFile outfile;
   
       public HashingUnit8() {
      }
   
       private void print(byte[] s) {
         for(int k = 0; k < s.length; k++)
            System.out.print((char)s[k]);
      }
   
       private long hash(byte[] s) {
         long xor = 0, pack;
         int i, j, slength; 
         for (slength = s.length; s[slength-1] == ' '; slength--);
         for (i = 0; i < slength; ) { 
            for (pack = j = 0; ; j++, i++) {
               pack |= (long) s[i];   
               if (j == 3 || i == slength - 1) { 
                  i++;
                  break;
               }
               pack <<= 8;
            }             
            xor ^= pack;  
         }                 
         return (xor % tableSize) * bucketSize * recordLength;
      }
   
       private byte[] getName() throws IOException {
         System.out.print("Enter customers name: ");
         String s = buffer.readLine();
         for (int i = s.length(); i < recordLength; i++)
            s += ' ';
         return s.getBytes();
      }
   
       private int comparesTo(byte[] s1, byte[] s2) { 
         for (int i = 0; i < s1.length; i++)         
            if (s1[i] != s2[i])                   
               return s1[i] - s2[i];
         return 0;
      }
   
       void insert() throws IOException {
         insertion(getName());
      }
   
       void insertion(byte[] line) throws IOException {
         byte[] name = new byte[recordLength];
         boolean done = false, inserted = false;
         int counter = 0;
         long address = hash(line);
         outfile.seek(address);
         while (!done && outfile.read(name) != -1) {
            if (name[0] == empty || name[0] == delMarker) {
               outfile.seek(address+counter*recordLength);
               outfile.write(line);
               done = inserted = true;
            }
            else if (comparesTo(name,line) == 0) {
               print(line);
               System.out.println(" is already in the file");
               return;
            }
            else counter++;
            if (counter == bucketSize)
               done = true;
            else outfile.seek(address+counter*recordLength);
         }
         if (!inserted) {
            done = false;
            counter = 0;
           
            while (!done ) {
               if (name[0] == delMarker)
                  done = true;
               else if (comparesTo(name,line) == 0) {
                  print(line);
                  System.out.println(" is already in the file");
                  return;
               }
               else counter++;
            }
          
         }
      }
   
       private void delete() throws IOException {
         byte[] line = getName();
         long address = hash(line);
         outfile.seek(address);
         int counter = 0;
         boolean done = false, deleted = false;
         byte[] name = new byte[recordLength];
         while (!done && outfile.read(name) != -1) {
            if (comparesTo(line,name) == 0) {
               outfile.seek(address+counter*recordLength);
               outfile.write(delMarker);
               done = deleted = true;
            }
            else counter++;
            if (counter == bucketSize)
               done = true;
            else outfile.seek(address+counter*recordLength);
         }
         if (!deleted) {
            done = false;
            counter = 0;
           
            while (!done ) {
               if (comparesTo(line,name) == 0) {
                  done = deleted = true;
               }
               else counter++;
            }
         }
         if (!deleted) {
            print(line);
            System.out.println(" is not in database");
         }
      }
     
         
       public void processFile(String fileName) {
         char command = '1';
         byte[] line = new byte[recordLength];
         String commandLine;
         try {
            (new File(".\\","outfile")).delete();
            RandomAccessFile fIn = new RandomAccessFile(fileName,"rw");
            outfile = new RandomAccessFile("outfile","rw");
         
            for (int i = 1; i <= tableSize*bucketSize*recordLength; i++) 
               outfile.write(empty);       
            while (fIn.read(line) != -1)    
               insertion(line);
            printFile("Customers:",outfile);
         
            while (command != '3') {
               System.out.print("Enter your choice "
                             + "(1. insert, 2. delete, 3. exit): ");
               commandLine = buffer.readLine();
               command = commandLine.charAt(0);
               if (command == '1')
                  insert();
               else if (command == '2')
                  delete();
               else if (command != '3')
                  System.out.println("Wrong command entered, please retry.");
               printFile("Customers:",outfile);
            
            }
               
            outfile.close();
            fIn.close();
         
         } 
             catch (IOException ioe) {
            }
      }
   
       private void printFile(String name, RandomAccessFile f) throws IOException {
         byte ch = '1';
         RandomAccessFile outf = new RandomAccessFile("hash.out","rw");
         outf.seek(outf.length());
         System.out.println(name);
         outf.writeBytes(name + "\n");
         f.seek(0);
         while (true) {
            for (int i = 1; i <= bucketSize; i++) {
               for (int j = 1; j <= recordLength; j++) {
                  try {
                     ch = f.readByte();
                  } 
                      catch (EOFException e) {
                        System.out.println();
                        outf.write('\n');
                        outf.close();
                        return;
                     }
                  System.out.print((char)ch);
                  outf.write(ch);
               }
            }
            outf.write('\n');
         }
      }
   
       static public void main(String args[]) {
         String fileName = "";
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader buffer = new BufferedReader(isr);
         HashingUnit8 hashClass = new HashingUnit8();
         try {
            if (args.length == 0) {
               System.out.print("Enter a file name: ");
               fileName = buffer.readLine();
            }
            else fileName = args[0];
         } 
             catch(IOException io) {
               System.err.println("Cannot open " + fileName);
            }
         hashClass.processFile(fileName);
         
      }
   }

AnswerRe: Need help with hash table Pin
427748025-Sep-09 0:39
427748025-Sep-09 0:39 
GeneralRe: Need help with hash table Pin
snssewell25-Sep-09 19:30
snssewell25-Sep-09 19:30 
QuestionRead SDF using Java Pin
jason975424-Sep-09 21:30
jason975424-Sep-09 21:30 
AnswerRe: Read SDF using Java Pin
427748025-Sep-09 0:36
427748025-Sep-09 0:36 
GeneralRe: Read SDF using Java Pin
jason975425-Sep-09 4:12
jason975425-Sep-09 4:12 
GeneralRe: Read SDF using Java Pin
427748025-Sep-09 8:25
427748025-Sep-09 8:25 
GeneralRe: Read SDF using Java Pin
jason975425-Sep-09 23:47
jason975425-Sep-09 23:47 
GeneralRe: Read SDF using Java Pin
427748026-Sep-09 1:32
427748026-Sep-09 1:32 
GeneralRe: Read SDF using Java Pin
David Skelly28-Sep-09 22:29
David Skelly28-Sep-09 22:29 
QuestionJava Graphics and Desktop Pin
sharkbc24-Sep-09 17:45
sharkbc24-Sep-09 17:45 
AnswerRe: Java Graphics and Desktop Pin
427748024-Sep-09 18:51
427748024-Sep-09 18:51 
GeneralRe: Java Graphics and Desktop Pin
sharkbc24-Sep-09 19:48
sharkbc24-Sep-09 19:48 
GeneralRe: Java Graphics and Desktop Pin
sharkbc24-Sep-09 20:27
sharkbc24-Sep-09 20:27 
GeneralRe: Java Graphics and Desktop Pin
427748025-Sep-09 0:34
427748025-Sep-09 0:34 
GeneralRe: Java Graphics and Desktop [modified] Pin
sharkbc25-Sep-09 15:55
sharkbc25-Sep-09 15:55 
QuestionJ2ME Pin
Matt Cavanagh24-Sep-09 9:51
Matt Cavanagh24-Sep-09 9:51 
AnswerRe: J2ME Pin
427748024-Sep-09 18:50
427748024-Sep-09 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.