Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

BufferedReader

Posted on 2003-10-23
9
Medium Priority
?
372 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 160 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

597 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