Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 5th, 2008, 03:21 AM
Newbie
 
Join Date: Sep 2008
Posts: 7
Default Java:Reading text file

bd.txt
D2333 How to program in Java r IN 454554 Dietal & Dietal Prentice Hall
G4547 Java in a Nut Shell c 343345 David Flagman O'Reilly 03-02-08
END

678764 John Smith #5 Hill View, Rose Hall Undergrad 5
END

//end of sample data
1. data elements are separated by Tabs
problems
1. When I try to read the text file as is give an error. I try to use "useDelimiters" but I cannot find out what the symbol for the Tab-key. However, If I take the spaces from the data for example JohnSmith, then It would read. I can read the first part of the file but unable to get the second part to read. Note: there is no date for the first line of data, this also generates an error, but this is how the data was given to me.

Expand|Select|Wrap|Line Numbers
  1. public class Book{
  2.  private String ISBN;
  3.  private String title;
  4.  private String   type;
  5.  private String status;
  6.  private int    borrowerId;
  7.  private String author;
  8.  private String publisher;
  9.  private String dateBorrowed;
  10.  
  11.  public Book( String isb, String tit, String typ, String sta, int borrid, String aut, String pub, String dat){
  12.   ISBN          = isb;
  13.   title    = tit;
  14.   type          = typ;
  15.   status        = sta;
  16.   borrowerId    = borrid;
  17.   author        = aut;
  18.   publisher     = pub;
  19.   dateBorrowed  = dat;
  20.  
  21.  }//End of Constructor
  22. public class Borrower{
  23.  private int borrowerId;
  24.  private String borrowerName;
  25.  private String borrowerAddress;
  26.  private String borrowerStatus;
  27.  private int    borrowerNoBooks;
  28.  
  29.  public Borrower( int id ){
  30.   borrowerId        = id;
  31.   borrowerName      = name;
  32.   borrowerAddress   = address;
  33.   borrowerStatus    = status;
  34.   borrowerNoBooks   = noBooks;
  35.  
  36.  }//End of Borrower's Constructor 
  37.  
  38. import java.io.*;
  39. import java.util.*;
  40. class Test{
  41.  static Book[]book = new Book[100];
  42.  static Borrower[]borrower = new Borrower[100];
  43.  public static void main( String[] args )throws IOException{
  44.  
  45.  
  46.  
  47.   Scanner in = new Scanner( new FileReader ( "db.txt" ) );
  48.   Scanner me = new Scanner( new FileReader ("db.txt" ) );//Is there a more efficient way than using the Scanner twice?
  49.  
  50.   String v = in.next();
  51.   int i = 0; //Iteratror for array
  52.  
  53.   while(!(v.equals("END"))){
  54.    book[i] = new Book(v, in.next(), in.next(), in.next(), in.nextInt(), in.next(), in.next(), in.next());
  55.    i++;
  56.    v = in.next();
  57.    if(v.equals("END")){//Breaks out of the loop when the first end is encountered
  58.     break;
  59.    }
  60.   }//End of while 
  61.  
  62.  
  63.       int count = 0;
  64.  
  65.   while(me.hasNext()){//Not getting this part to read it is generating a mismatch error
  66.    if(me.hasNextInt()){
  67.     borrower[count] = new Borrower(in.nextInt());
  68.     count++;
  69.    }else{
  70.     String l = me.next();
  71.     if(l.equals("END")){
  72.      break;
  73.     }
  74.    }
  75.   }
  76.  
  77.  
  78.   in.close();
  79.   me.close();
  80.  
  81.    for(int j = 0; j < i; j++){
  82.     System.out.println(book[j].toString());
  83.    }

Last edited by r035198x; October 6th, 2008 at 08:12 AM. Reason: added code tags
Reply
  #2  
Old October 5th, 2008, 05:17 AM
Expert
 
Join Date: Sep 2007
Posts: 849
Default

The tab character is usually '\t'.
Reply
  #3  
Old October 6th, 2008, 11:53 AM
Newbie
 
Join Date: Sep 2008
Posts: 7
Default

bd.txt
D2333 How to program in Java r IN 454554 Dietal & Dietal Prentice Hall
G4547 Java in a Nut Shell c 343345 David Flagman O'Reilly 03-02-08
END

678764 John Smith #5 Hill View, Rose Hall Undergrad 5
657933
END
Reply
  #4  
Old October 6th, 2008, 12:05 PM
Newbie
 
Join Date: Sep 2008
Posts: 7
Default

bd.txt
D2333 How to program in Java r IN 454554 Dietal & Dietal Prentice Hall
G4547 Java in a Nut Shell c 343345 David Flagman O'Reilly 03-02-08
END

678764 John Smith #5 Hill View, Rose Hall Undergrad 5
END

Thank you very much, the tab works fine. The above is an example of the data I am working with. Having gotten the tab to work, reading in the data becomes a problem. Tell me am I going wrong.
Expand|Select|Wrap|Line Numbers
  1. String v = input.next() //read first value in data
  2. int count = 0;
  3. while(!(v.equals("END")))// check for end
  4. {
  5.      book[count] = new Book (v, input.next() etc.)
  6.      count++;
  7.      v = input.next();
  8.      if(v.equals("END"){
  9.          break;
  10.      }}
  11.  
  12.      int x = input.nextInt()
  13.       while(x!=0){
  14.            borrower[i] = new Borrower(etc.)
  15.            i++;
  16.           x = input.nextInt();
  17.          String p = input.next()
  18.          if(p.equals("END"){
  19.                break;
  20.          }}
  21.  
What is wrong with my logic here? Even if I get the file to compile, when I try to print I get null.

Last edited by Nepomuk; October 7th, 2008 at 03:04 PM. Reason: Added [CODE] tags
Reply
  #5  
Old October 7th, 2008, 03:11 PM
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Age: 21
Posts: 2,003
Default

Quote:
Originally Posted by chetah
bd.txt
D2333 How to program in Java r IN 454554 Dietal & Dietal Prentice Hall
G4547 Java in a Nut Shell c 343345 David Flagman O'Reilly 03-02-08
END

678764 John Smith #5 Hill View, Rose Hall Undergrad 5
END

Thank you very much, the tab works fine. The above is an example of the data I am working with. Having gotten the tab to work, reading in the data becomes a problem. Tell me am I going wrong.
Expand|Select|Wrap|Line Numbers
  1. String v = input.next() //read first value in data
  2. int count = 0;
  3. while(!(v.equals("END")))// check for end
  4. {
  5.      book[count] = new Book (v, input.next() etc.)
  6.      count++;
  7.      v = input.next();
  8.      if(v.equals("END"){
  9.          break;
  10.      }}
  11.  
  12.      int x = input.nextInt()
  13.       while(x!=0){
  14.            borrower[i] = new Borrower(etc.)
  15.            i++;
  16.           x = input.nextInt();
  17.          String p = input.next()
  18.          if(p.equals("END"){
  19.                break;
  20.          }}
  21.  
What is wrong with my logic here? Even if I get the file to compile, when I try to print I get null.
OK, fist of all: Both r035198x and I have now added [CODE]...[/code] to your posts. Please use them yourself from now on.

Now, where do you try to print something and what printout would you expect? Oh, and I'm not sure if you know that, but input.next() will read the next Word (so whatever comes until the next whitespace character) and not more. You may want to use input.nextLine().

Greetings,
Nepomuk
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles