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]
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]
Comment