?
Solved

hashtables and external files

Posted on 2003-03-25
7
Medium Priority
?
180 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:js531
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8204351
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208347
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
 

Author Comment

by:js531
ID: 8210383
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Accepted Solution

by:
bobbit31 earned 1000 total points
ID: 8211787
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8211818
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
 

Author Comment

by:js531
ID: 8217687
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8218306
sure no prob, if you have any more questions feel free to post them in this thread
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question