Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

reading each number if there are irregular spaces among numbers

Posted on 2014-01-31
3
Medium Priority
?
213 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

636 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