Question

I have created an application that allows the user to enter their account number, balance(no more than 99999), and last name. The program will take this information and insert it into a .txt file at a location corresponding to the account number(acct). Here is the code for that:

import java.io.*;

public class NationalBank {
  public static void main(String[] args) throws Exception{
     InputStreamReader temp = null;
     BufferedReader input = null;
     try {
        temp = new InputStreamReader(System.in);
        input = new BufferedReader(temp);

        int acct;
        double amount;
        String name;
        RandomAccessFile file = new RandomAccessFile("bank.txt", "rw");
        while(true) {
            // Asks for input
           System.out.println("Enter Account Number (0-9999): ");
           acct = Integer.parseInt(input.readLine());
           System.out.println("Enter Last Name: ");
           name = input.readLine();
           System.out.println("Enter Balance ");
           amount = Double.parseDouble(input.readLine());

            // Making sure account numbers are between 0 and 9999
           if(acct >=0 && acct <= 9999) {
              file.seek(acct*17);
              file.write(truncateName(name));
              file.writeBytes(" " +amount);
           }
           else {
              continue;
           }
            // Asks user if more entries are needed
           System.out.println("Enter More? (y/n)");   
           if (input.readLine().toLowerCase().equals("n"))
              break;
        }
        file.close();
     }
        catch (Exception e) {  
        }
  }

// Truncate/adding spaces to name until 8 characters
  public static byte[] truncateName (String name) {
     byte[] result = new byte[8];
     for (int i = 0; i < 8; i++)
        result [i] = i < name.length () ? (byte)name.charAt (i) : (byte)' ';
     return result;
  }

}

Now, I am trying to make an application that will write back all of the accounts that have information within them(with last name and balance). I need to display the account number, balance, and last name of those accounts. So far, I have:

import java.io.*;

public class DisplayBank {
  public static void main(String[] args) throws IOException {
    FileInputStream input = new FileInputStream ("bank.txt");
     try {
        byte[] record = new byte[17];
        while (input.read(record) == 17) {
           String name = new String(record, 0, 8);
           long bits = 0;
           for (int i = 8; i < 17; i++) {
              bits <<= 8;
              bits |= record[i] & 0xFF;
           }
           double amount = Double.longBitsToDouble(bits);

           System.out.println("Account Number: " + record + " Name: " + name + ", amount: " + amount);
        }
     }

     catch (IOException e) {  
     }
     finally {
            input.close();

     }
  }
}

This currently displays only the name correctly. The balance is incorrect, and I don't know how to get the account number. In order to get the account number, I would need to get the position of name. In order to get the amount, I would need to seek name, offset 9 bytes, then read the next 8 bytes...

Was it helpful?

Solution

If you want to parse a text file that contains last names and amounts similar what you provided:

example provided

LastName 93942.12

What I would do is to try something like the following

 public void read_file(){
    try{

    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("C:\\Users\\Alos\\Desktop\\test.txt");
    // Use DataInputStream to read binary NOT text.
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    int record = 0;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {

        String[] splits = strLine.split("\t");
        String LastName = splits[0];
        String Amount = splits[1]; 
        System.out.println("Account Number: " + record + " Name: " + LastName + ", amount: " + Amount);
        record++;
    }
    //Close the input stream
    in.close();

      }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }

}

This might not be exactly what you're looking for but please take a look and update your question if you would like something different.

OTHER TIPS

I it's not a homework, I would strongly recommend to use some RDBMS like Derby or MySQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top