• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading file splitting data into individual readings

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added this if statement before int parse and I get a new exception


Array out of bound:1
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to add simple print statements by printing out the length of array after you split the line/record, so you'd see how many elements in it before you access element at particular index and you'll see where the things fall apart. Don't be scared to use print statements, these can tell you helpful things.

You can add also counter, so you'd know at which line the amount of elements differs.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Try to add simple print statements by printing out the length of array after you split the line/record, so you'd see how many elements in it before you access element at particular index and you'll see where the things fall apart. Don't be scared to use print statements, these can tell you helpful things.

You can add also counter, so you'd know at which line the amount of elements differs.


I tried changing the file and the only problems at the moment are the text file before the start of the number. If I delete them it works perfectly.
What regular expression matches only alphabet I might be able to put an if statement before my parse like

I'm not sure if I'm using the right regex cause it's not working. or maybe the problem is not because I have letters maybe it's because I have empty string but I also put a condition if(!fields[5].isEmpty) so in this case my code must not execute. at this point I get out of boundary exception
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you're desperately trying various things. Slow down

Make it simpler. Make for youself 9 records/lines file.

1. Task. You know that you don't need first 7 lines. Try to read file and print out only 8th and 9th lines.
2. Task. Split record on 8th and 9th lines. Make sure these are of length 7 afterwards.

3. Task. Try to iterate through those 2 splitted lines, and print element at index 5, only the one which contain 71.8 (by omitting ---).

Do by small bits, make sure you get what you think you get, don't try to crack out evertying in one go.

saeid jamali wrote:"[a-zA-Z]".equals(fields[0])

That first bit is not a regular expression, it is a string literal. Forget about that at the moment (i'm not sure you'll need that at all). Get first things done.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saeid jamali wrote:I'm not sure if I'm using the right regex cause it's not working....


I suspect you're going the wrong way about this. Basically, what you want to know is when a line DOES contain values, not when it doesn't.

So what you need is a regex that checks whether a "field" IS a valid 'value' or not - ie, a number or "---".
And a valid record is a line that contains at least 7 valid 'values'.

One possibility might be something like this:I've listed it out in detail to explain how it works, but you can just make it a single string if you want.

Have a look at what I posted earlier to see if it makes sense now.

HIH

Winston
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

saeid jamali wrote:I'm not sure if I'm using the right regex cause it's not working....


I suspect you're going the wrong way about this. Basically, what you want to know is when a line DOES contain values, not when it doesn't.

So what you need is a regex that checks whether a "field" IS a valid 'value' or not - ie, a number or "---".
And a valid record is a line that contains at least 7 valid 'values'.

One possibility might be something like this:I've listed it out in detail to explain how it works, but you can just make it a single string if you want.

Have a look at what I posted earlier to see if it makes sense now.

HIH

Winston


I just started reading the file from 7th line.


I know it's a bad idea but I don't want my code to get so complex and I got rid of "---" by your previous comment. I made if statement for very parse and now it's working perfectly.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saeid jamali wrote:I know it's a bad idea but I don't want my code to get so complex...


Nature of the beast I'm afraid. What you're hopefully starting to understand is that the rules for your file aren't as simple as you first thought.

And as Einstein said (nearly): Everything should be as simple as possible, but no simpler.

I made if statement for very parse and now it's working perfectly.


Great!

Winston
 
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi people.
I wrote the code for this kind of problem. The solution is way easier. Come to very basics of the programming.

Create an Array List (or whichever Data Structure you are comfortable) to store Data .
Suppose you start considering data from line 8
Since you have 7 fields of data of which rain is the 6th field.

//Code is as follows

//You have whole data in a Array List

// rainfall data is there is ArrayList
//Now assuming you have complete list of data in your file including those of leap year
Since your starting year is 1908, we have to check whether it is a leap year
//create function to print data of all years


Sorry if there are bugs in code(Beginner here myself).
That's how the code will be. You can do all the variation in the code, it's easy job now.
I just gave you way around this problem.
Honestly speaking, it took me a month to come over a problem like this.
Hope i helped. (IF you need help, feel free to ask)
-------------------------------

Hardwork beats talent when talent fails to work hard

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic