Solved

BufferedReader

Posted on 2003-10-23
9
362 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 33
map related example 6 49
hibernate example for saving data 19 43
Adding multiple JVM environments to RedHat 6 7 24
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…
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.
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

831 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