Java:Reading text file 
October 5th, 2008, 02:21 AM
| | Newbie | | Join Date: Sep 2008
Posts: 7
| | 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. - 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.equals("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("END")){//Breaks out of the loop when the first end is encountered
-
break;
-
}
-
}//End of while
-
-
-
int count = 0;
-
-
while(me.hasNext()){//Not getting this part to read it is generating a mismatch error
-
if(me.hasNextInt()){
-
borrower[count] = new Borrower(in.nextInt());
-
count++;
-
}else{
-
String l = me.next();
-
if(l.equals("END")){
-
break;
-
}
-
}
-
}
-
-
-
in.close();
-
me.close();
-
-
for(int j = 0; j < i; j++){
-
System.out.println(book[j].toString());
-
}
Last edited by r035198x; October 6th, 2008 at 07:12 AM.
Reason: added code tags
| 
October 5th, 2008, 04:17 AM
| | Expert | | Join Date: Sep 2007
Posts: 855
| |
The tab character is usually '\t'.
| 
October 6th, 2008, 10:53 AM
| | Newbie | | Join Date: Sep 2008
Posts: 7
| |
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
| 
October 6th, 2008, 11:05 AM
| | Newbie | | Join Date: Sep 2008
Posts: 7
| |
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. -
String v = input.next() //read first value in data
-
int count = 0;
-
while(!(v.equals("END")))// check for end
-
{
-
book[count] = new Book (v, input.next() etc.)
-
count++;
-
v = input.next();
-
if(v.equals("END"){
-
break;
-
}}
-
-
int x = input.nextInt()
-
while(x!=0){
-
borrower[i] = new Borrower(etc.)
-
i++;
-
x = input.nextInt();
-
String p = input.next()
-
if(p.equals("END"){
-
break;
-
}}
-
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 02:04 PM.
Reason: Added [CODE] tags
| 
October 7th, 2008, 02:11 PM
|  | Moderator | | Join Date: Aug 2007 Location: Germany Age: 22
Posts: 2,460
| | 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. -
String v = input.next() //read first value in data
-
int count = 0;
-
while(!(v.equals("END")))// check for end
-
{
-
book[count] = new Book (v, input.next() etc.)
-
count++;
-
v = input.next();
-
if(v.equals("END"){
-
break;
-
}}
-
-
int x = input.nextInt()
-
while(x!=0){
-
borrower[i] = new Borrower(etc.)
-
i++;
-
x = input.nextInt();
-
String p = input.next()
-
if(p.equals("END"){
-
break;
-
}}
-
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
|  | | Thread Tools | Search this Thread | | | | | | | 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 220,989 network members.
|