473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java:Reading text file

10
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.    }
Oct 5 '08 #1
4 5566
Laharl
849 Expert 512MB
The tab character is usually '\t'.
Oct 5 '08 #2
chetah
10
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
Oct 6 '08 #3
chetah
10
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.
Oct 6 '08 #4
Nepomuk
3,112 Expert 2GB
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
Oct 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kre Rasmussen | last post by:
As fare as I know, the Linux/GNU compiler i able to compile a .java-file into a .exe-file which is able to run on a linux without the JRE installed. Is the same thing possible on windows and how is...
0
by: dashprasannajit | last post by:
package djvusearching; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.lucene.analysis.cjk.CJKAnalyzer; //import...
24
by: crazystone82 | last post by:
Hi all, please send me a source code to transfer a file to a server running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented...
3
by: Sushmita | last post by:
Hi All.. i need a small help...i will present the scenario in the best possible manner please help... We are asking the user to browse a file... and using that file path we are getting the java...
5
by: rahana | last post by:
Hello , Presently for compiling a simple java prog i am placing my .java file in C:---->j2sdk1.4---->bin----->rahana----->my .java progs. but my friend was suggesting not to keep...
3
by: Nick048 | last post by:
Hi to All, In order to create a Date Picker that I need to use in Oracle forms, I have modified some element in the source of original calendarPJC provided from Oracle. Following the...
2
by: jpr | last post by:
I have 2 classes saved as 2 different java files.(eg- class ABC.java & PQR.java) ->ABC.java classABC extends JTextField { ABC(int i) { super(i); ...
1
by: bsonline | last post by:
I have to modify a .java file in a project. But I dont have any editor to compile java. I use putty to connect server(Solaris). And using putty console I modify .java file and save it. Then I down...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.