esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I read a file that contains numbers?
The following code reads Numbers as well as String from a File.




import java.io.*;

public class readNumbers {

public static void main(String arg[]) {
BufferedReader bis=null;
StreamTokenizer st=null;

try {
bis = new BufferedReader(new FileReader
("numberFile.txt"));
st = new StreamTokenizer(bis);
}
catch(FileNotFoundException e) {
System.out.println("File Not Found");
}

try {
while (st.nextToken() != StreamTokenizer.TT_EOF) {
if (st.ttype == StreamTokenizer.TT_WORD) {
System.out.println("String: " + st.sval);
}
else if (st.ttype == StreamTokenizer.TT_NUMBER) {
System.out.println("Number: " + st.nval);
}
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}


Eg., File(numberFile.txt) contents:


20432 34343 Testing String
23432 3343


Output:


Number: 20432.0
Number: 34343.0
String: Testing
String: String
Number: 23432.0
Number: 3343.0




Further Information
Author of answer: Kishore Koya

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.