parse a text file

Posted on 2004-11-23
Medium Priority
Last Modified: 2010-03-31
I have a text file of the form:
33.581 14.994 92.452
33.461 14.976 90.967
32.861 13.656 90.629
33.463 12.623 90.924
34.845 15.053 90.276

These are x, y and z coordinates.  I need to read them into a java program and store them in arrays as floats.  What is the best way to do this?
Question by:vonster
LVL 92

Assisted Solution

objects earned 200 total points
ID: 12662117
BufferedReader in = new BufferedReader(new FileReader("a.txt"));
String line = null;
while (null!=(line=in.readLine()))
    String[] tokens = line.split(" ");
    float[] f = new float[tokens.length()];
    for (int i=0; i<f.length; i++)
       f[i] = Float.parseFloat(tokens[i]);

    // do what you need to with float array



Expert Comment

ID: 12662134
Hey objects you devil you beat me to it again. :)

Have a nice day pal.

Accepted Solution

SaMuEl earned 300 total points
ID: 12662172
import java.io.*;
class Reader{
      static float[] x=new float[100],y=new float[100],z=new float[100];
      static void read() throws IOException{
            BufferedReader in = new BufferedReader(new FileReader("text.txt"));
            String s;
                  for (int i=0;(s=in.readLine())!=null;i++){
                        x[i] = Float.parseFloat(s.split(" ")[0]);
                        y[i] = Float.parseFloat(s.split(" ")[1]);
                        z[i] = Float.parseFloat(s.split(" ")[2]);
            catch (Exception e){}

      public static void main(String[] args) throws IOException{
      for (int i=0; i<x.length;i++){
            System.out.println(x[i] + " " + y[i] + " " + z[i]);
This code has some obvious limitations.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question