Solved

BufferedReader

Posted on 2003-10-23
9
359 Views
Last Modified: 2010-03-31
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
             String line = "";
             emptyfile(line, br);
             if ((line = br.readLine()) == null)
             {
                System.out.println("NO RECORD EXISTS IN OUR SYSTEM");
                EXIT();
             }
             while((line = br.readLine()) != null)
             {
                 StringTokenizer st = new StringTokenizer(line, "," );
                 String getCusId = st.nextToken();
                 System.out.println(getCusId);
                 if (getCusId.equals(customerId))
                 {
                     test = true;
                 }


--------------------------------------------------------------------------------
if the text file is like
1212121212,brian,1212// if loop test anf fails
1111122222,chow,1122 //while loop begin
....
....
how can i make the while loop read from the beginning of the text file again thanks for help, i know that may be a very easy question..

0
Comment
Question by:redsar
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 40 total points
ID: 9605807
Why test first? Just read the file

boolean noRecords = true;
while((line = br.readLine()) != null)
{
     noRecords = false;
     // read the file and get info
}
0
 

Author Comment

by:redsar
ID: 9605940
i have tried
like this:
             boolean norecord = true;
             while((line = br.readLine()) != null)
             {
                 norecord = false;
                 StringTokenizer st = new StringTokenizer(line, "," );
                 String getCusId = st.nextToken();
                 if (getCusId.equals(customerId))
                     test = true;

              }
             if (norecord)
                //error,
          }
when the text file is null, it come up with error like this,
Exception in thread "main" java.util.NoSuchElementException
it seems it still read the element in the text file , and come upp with error
when using Tokenizer, how come this is not working,
so can i use
 catch(NoSuchElementException e)
          {
             System.out.println("NO RECORD EXIST");
             System.exit()          }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9605966
>>when the text file is null, it come up with error like this,

It can't be 'null' (i assume you mean empty) or it would not be entering the loop. Trim the line to make sure it's not just spaces
0
 

Author Comment

by:redsar
ID: 9606040
yes, it's an empty
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9606073
You can test for empty by a file size of zero.

File f = new File("input.txt");
boolean empty = (f.length() == 0);
System.out.println("File is empty? - " + empty);
0
 

Author Comment

by:redsar
ID: 9606118
thnaks CHEJ,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9606131
:-)
0
 

Author Comment

by:redsar
ID: 9606169
sorry, the problem is still there,
i want something like,
if the user select to open an account,
gets a customer Id,and other info, then write that to record.txt
and then if another user wanna open an account open the record.txt check whether the customer Id has already pick up by another user, if yes, error, if no, keep going...
0
 

Author Comment

by:redsar
ID: 9606306
i got it , thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

757 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

18 Experts available now in Live!

Get 1:1 Help Now