Solved

BufferedReader

Posted on 2003-10-23
9
360 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
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.

 
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

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
xampp tool 12 28
servlet filter example 37 44
Opening PDF on button click and fill new document 2 21
use lov values 2 36
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

895 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

13 Experts available now in Live!

Get 1:1 Help Now