Solved

reading each number if there are irregular spaces among numbers

Posted on 2014-01-31
3
207 Views
Last Modified: 2014-02-03
Hi Experts,

I am trying to read the following file:

http://www.who.int/childgrowth/standards/wfa_boys_0_5_percentiles.txt

InputStream      is = new StandardNormal().getClass().getResourceAsStream("resources/"+ dataTableNames[resourceDataType]);
                      
                      
 
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(is));
         		            
  for (String s1 = bufferedreader.readLine(); s1 != null; s1 = bufferedreader.readLine())
                        {
                                int i = Integer.parseInt(s1.substring(1, 8).trim());
                                float f = Float.parseFloat(s1.substring(9, 17).trim());
                                float f1 = Float.parseFloat(s1.substring(69, 86).trim());
                                float f2 = Float.parseFloat(s1.substring(87, 103).trim());
                                float f3 = Float.parseFloat(s1.substring(104, 122).trim());
...
...
...

Open in new window


unfortunately, each numbers in this file doesn't fall in exact location, so it messes up file reading.

how do I read each number if there are irregular spaces among numbers?

thx,
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39824281
Why not just use String.split or a Scanner?
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39828913
Your data seems to be symmetric. Scanner could be preferred option.

Entire 1st line would be string (Headings).
For nextline onwards 1st Int & then all Double.

Here is the logic for same,

Scanner scan1 = new Scanner("wfa_boys_0_5_percentiles.txt");
while (scan1.hasNextLine())
        {
            // Read an entire line for each record
            String line = scan1.nextLine();

            // Using Scanner to break up this line into parts
            Scanner lineBreaker = new Scanner(line);

            //If line is 1st then all strings, else 1st integer & then all doubles            
            System.out.println(lineBreaker.nextInt());
            System.out.println(lineBreaker.nextDouble());
..............
..............
..............
..............
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39829254
:)
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 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