Solved

Cannot get java program to print record from file in the correct format.

Posted on 2012-03-14
2
509 Views
Last Modified: 2012-06-22
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  
 
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);
	      }
	   }
	}

Open in new window

RandomCustomers.txt
0
Comment
Question by:robertben
2 Comments
 
LVL 9

Accepted Solution

by:
arioh earned 500 total points
Comment Utility
you have wrong "s" definition. there must be 8 spaces, not 7
0
 

Author Closing Comment

by:robertben
Comment Utility
I cannot believe one space is all it took. All those hours of frustration for one space.
Your help is much appreciated. Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now