Solved

reading each number if there are irregular spaces among numbers

Posted on 2014-01-31
3
200 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

911 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

21 Experts available now in Live!

Get 1:1 Help Now