[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

reading each number if there are irregular spaces among numbers

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
dkim18
Asked:
dkim18
  • 2
1 Solution
 
CEHJCommented:
Why not just use String.split or a Scanner?
0
 
Mahesh BhutkarCommented:
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
 
CEHJCommented:
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now