hashtables and external files

Hello all,
    I have to write a program which takes an external file reads it and puts the data into a hashtable, it then must take the data from the hash table manipulate it and output another file. I am having some trouble with this and was wondering if anyone could maybe give me a coded example of how this is done??

Thank you all,
JS
js531Asked:
Who is Participating?
 
bobbit31Connect With a Mentor Commented:
here's something to get you started:

import java.io.*;
import java.util.StringTokenizer;


public class ParseFile {

     public static void main(String[] args) {

          try {
               // open the file
               File f = new File ("test.txt");
               BufferedReader in = new BufferedReader(new FileReader(f));

               String s;
               StringTokenizer st;
               int lineNum=1;
               int wordNum=1;
               while ((s = in.readLine()) != null) {
                    // tokenize the string with space as delimiter
                    st = new StringTokenizer(s, " ");
                    while (st.hasMoreTokens()) {
                         System.out.println("Line " + lineNum + ", Word " + wordNum++ + ": " + st.nextToken());
                    }
                    wordNum = 1;
                    lineNum += 1;
               }

               in.close();
          } catch (Exception e) {
               e.printStackTrace();
          }
     }

}
0
 
bobbit31Commented:
sure, but we'll need to know the format of the input file and how you want it parsed and the requested format of the output file.

ie. maybe an example input file and output file
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Use the Properties class (a sub-class of HashTable) and its load () and list () methodds to read/ write directly from/ to the files.

Mayank.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
js531Author Commented:
bobbit31,
the format of the input file is like this(plain text file), the output will display the values of the first column along with addresses (i.e. COPY is 1000, FIRST is 1003 etc.) does this help clarify what i need to do??


COPY     START     1000        
FIRST    STL        RETADR      
CLOOP    JSUB      RDREC      
         LDA       LENGTH      
         COMP      ZERO          
         JEQ       ENDFIL    
         JSUB      WRREC      
         J         CLOOP      
ENDFIL   LDA       EOF        
         STA       BUFFER      
         LDA       THREE      
         STA       LENGTH      
         JSUB      WRREC  
         LDL       RETADR      
         RSUB                
EOF      BYTE      C'EOF'
THREE    WORD      3
ZERO     WORD      0
RETADR   RESW      1
LENGTH   RESW      1          
BUFFER   RESB      4096        
.        
.        
.
RDREC    LDX       ZERO        
         LDA       ZERO        
RLOOP    TD        INPUT      
         JEQ       RLOOP      
         RD        INPUT      
         COMP      ZERO        
         JEQ       EXIT      
         STCH      BUFFER,X    
         TIX       MAXLEN        
         JLT       RLOOP      
EXIT     STX       LENGTH      
         RSUB                  
INPUT    BYTE      X'F1'
MAXLEN   WORD      4096
.        
.        
.
WRREC    LDX       ZERO        
WLOOP    TD        OUTPUT      
         JEQ       WLOOP      
         LDCH      BUFFER,X    
         WD        OUTPUT      
         TIX       LENGTH      
         JLT       WLOOP        
         RSUB                  
OUTPUT   BYTE      X'05'      
         END        FIRST    
0
 
bobbit31Commented:
if you run the above code, you'll notice it breaks down each word in your file by line number and word number.

you can then take this information and write it to the file however you want using:

FileWriter out = new FileWriter(new File("outfile.txt"));
out.write("<some string>");
0
 
js531Author Commented:
Thanks bobbit,
i am sure this will help, i may need your help when i try to load the tokenized data into the array.  Thank you for your help so far.
js
0
 
bobbit31Commented:
sure no prob, if you have any more questions feel free to post them in this thread
0
All Courses

From novice to tech pro — start learning today.