parse a text file

Posted on 2004-11-23
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

    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


    LVL 3

    Expert Comment

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

    Have a nice day pal.
    LVL 2

    Accepted Solution

    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Return ZipOutputStream using Servlet 2 27
    count8 challlenge 13 69
    array220 challenge 8 32
    Unable to start eclipse ? 17 50
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now