Solved

reading each number if there are irregular spaces among numbers

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 86
allStar challenge 1 43
parentbit challenge 3 52
topping3 challenge 14 52
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

746 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

10 Experts available now in Live!

Get 1:1 Help Now