robertben
asked on
Cannot get java program to print record from file in the correct format.
Hello Experts,
I am having a major problem with a java program that is supposed to have the user enter a customer number and the results will be a print output of the customer number, last name, and balance. This information is retrieved from a text file created from another java program which I did create successfully. I am able output to screen the first record correctly when I enter the customer number 0003 (1st record in text file), but there is a 0 above the results for some reason unknown to me. When I enter the next customer number after that I get nothing but 0s and spaces. I need help figuring this out, please. I hope I explained the problem well enough. Below is the text file and the program code.
I went ahead and attached the text file.
Thanks
I am having a major problem with a java program that is supposed to have the user enter a customer number and the results will be a print output of the customer number, last name, and balance. This information is retrieved from a text file created from another java program which I did create successfully. I am able output to screen the first record correctly when I enter the customer number 0003 (1st record in text file), but there is a 0 above the results for some reason unknown to me. When I enter the next customer number after that I get nothing but 0s and spaces. I need help figuring this out, please. I hope I explained the problem well enough. Below is the text file and the program code.
I went ahead and attached the text file.
Thanks
package createBankFile;
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import javax.print.DocFlavor.READER;
public class ReadBankAccountsRandomly
{
public static void main(String[] args)
{
Scanner keyBoard = new Scanner(System.in);
Path file = Paths.get("C:\\JavaPrograms\\RandomCustomers.txt");
String s = "0000, ,00000.00" + System.getProperty("line.separator");
final int RECSIZE = s.length();
byte[] data = s.getBytes();
FileChannel fc = null;
String idString;
int id;
final String QUIT = "999";
try
{
fc = (FileChannel)Files.newByteChannel(file, READ);
ByteBuffer buffer = ByteBuffer.wrap(data);
System.out.print("Enter customer account number or " + QUIT + " to quit >> ");
idString = keyBoard.nextLine();
while(!idString.equals(QUIT))
{
id = Integer.parseInt(idString);
buffer = ByteBuffer.wrap(data);
fc.position(id * RECSIZE);
fc.read(buffer);
s = new String(data);
System.out.println(s);
System.out.print("Enter customer account number >> " + QUIT + " to quit >> ");
idString = keyBoard.nextLine();
}
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
RandomCustomers.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your help is much appreciated. Thank you.