Solved

BufferedReader

Posted on 2003-10-23
9
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:redsar
ID: 9606040
yes, it's an empty
0
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

724 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