Java:Reading text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chetah
    New Member
    • Sep 2008
    • 10

    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 "useDelimit ers" 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.

    [CODE=java]public class Book{
    private String ISBN;
    private String title;
    private String type;
    private String status;
    private int borrowerId;
    private String author;
    private String publisher;
    private String dateBorrowed;

    public Book( String isb, String tit, String typ, String sta, int borrid, String aut, String pub, String dat){
    ISBN = isb;
    title = tit;
    type = typ;
    status = sta;
    borrowerId = borrid;
    author = aut;
    publisher = pub;
    dateBorrowed = dat;

    }//End of Constructor
    public class Borrower{
    private int borrowerId;
    private String borrowerName;
    private String borrowerAddress ;
    private String borrowerStatus;
    private int borrowerNoBooks ;

    public Borrower( int id ){
    borrowerId = id;
    borrowerName = name;
    borrowerAddress = address;
    borrowerStatus = status;
    borrowerNoBooks = noBooks;

    }//End of Borrower's Constructor

    import java.io.*;
    import java.util.*;
    class Test{
    static Book[]book = new Book[100];
    static Borrower[]borrower = new Borrower[100];
    public static void main( String[] args )throws IOException{



    Scanner in = new Scanner( new FileReader ( "db.txt" ) );
    Scanner me = new Scanner( new FileReader ("db.txt" ) );//Is there a more efficient way than using the Scanner twice?

    String v = in.next();
    int i = 0; //Iteratror for array

    while(!(v.equal s("END"))){
    book[i] = new Book(v, in.next(), in.next(), in.next(), in.nextInt(), in.next(), in.next(), in.next());
    i++;
    v = in.next();
    if(v.equals("EN D")){//Breaks out of the loop when the first end is encountered
    break;
    }
    }//End of while


    int count = 0;

    while(me.hasNex t()){//Not getting this part to read it is generating a mismatch error
    if(me.hasNextIn t()){
    borrower[count] = new Borrower(in.nex tInt());
    count++;
    }else{
    String l = me.next();
    if(l.equals("EN D")){
    break;
    }
    }
    }


    in.close();
    me.close();

    for(int j = 0; j < i; j++){
    System.out.prin tln(book[j].toString());
    }[/CODE]
    Last edited by r035198x; Oct 6 '08, 07:12 AM. Reason: added code tags
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    The tab character is usually '\t'.

    Comment

    • chetah
      New Member
      • Sep 2008
      • 10

      #3
      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

      Comment

      • chetah
        New Member
        • Sep 2008
        • 10

        #4
        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.
        [code=java]
        String v = input.next() //read first value in data
        int count = 0;
        while(!(v.equal s("END")))// check for end
        {
        book[count] = new Book (v, input.next() etc.)
        count++;
        v = input.next();
        if(v.equals("EN D"){
        break;
        }}

        int x = input.nextInt()
        while(x!=0){
        borrower[i] = new Borrower(etc.)
        i++;
        x = input.nextInt() ;
        String p = input.next()
        if(p.equals("EN D"){
        break;
        }}
        [/code] 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; Oct 7 '08, 02:04 PM. Reason: Added [CODE] tags

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          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.
          [code=java]
          String v = input.next() //read first value in data
          int count = 0;
          while(!(v.equal s("END")))// check for end
          {
          book[count] = new Book (v, input.next() etc.)
          count++;
          v = input.next();
          if(v.equals("EN D"){
          break;
          }}

          int x = input.nextInt()
          while(x!=0){
          borrower[i] = new Borrower(etc.)
          i++;
          x = input.nextInt() ;
          String p = input.next()
          if(p.equals("EN D"){
          break;
          }}
          [/code] 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

          Comment

          Working...