Solved

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

Posted on 2012-03-14
2
510 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
ID: 37724047
you have wrong "s" definition. there must be 8 spaces, not 7
0
 

Author Closing Comment

by:robertben
ID: 37725079
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 50
solarwind tftp server 2 44
ForLoop Example 3 40
javap not working 8 36
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

919 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

21 Experts available now in Live!

Get 1:1 Help Now