• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

help please with errors

 
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have found a problem with this 3rd party java code

The problems arise when you try to evaluate a time difference which traverses one or two midnight points (i.e. change of day)

I have tried to insert a piece of code which will reduce the time differences to their correct values
BUT Java doesn't like my code

Here is the code:-



I get a syntax error on line 7 telling me to add a 'closing bracket' to complete the IF Statement?

Bob M
Dunedin
New Zealand
 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

syntax error on line 7  


Does that line number correspond to the posted code?  Line 7 is a comment.

The { on line 12 needs a closing }

Note: ALL statements in an if or a loop should be enclosed in {}s
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is now:-



How do I get a String for timeValue - last line

error says:- can not invoke toString() on the primitive type int
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you are trying to make a String from an int.
There are methods in the Integer class and the String class that will do that.

Take a look at the API doc for those classes:  https://docs.oracle.com/javase/8/docs/api/index.html
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried


but I get the same error as before
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the entire error message back here.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers

All sorted
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it, but what was the correct solution please?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did they get two days to be 282 and one day to be 138? Why are they storing those values as Strings?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Campbell

Those two values are what I have chosen to reduce the value of the variable to its correct amount

The problem arises when you have 1 or more days of no trading between the beginning of an event and the end of an event (e.g. a weekend)

Those two values I can't fully explain at the moment

Bob M
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why are they storing those values as Strings?



I have no idea.....they seem to have many Strings involving int or double variables

I can't see the logic at the moment

Bob M
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above is some 3rd party code......................

For example, when one has a 10 minute difference in end-beginning time timeDiff-1 ends up with a value of 600

However, once in a while, we get rogue values for timeDiff-1

For example: begin time 21:50 and end time is 22:00 (same day, same year, same month)
Yet timeDiff-1 comes out as 2999 ???

A second example: begin time 00:50 end time 1:10 (same year, month, day)
Yet TimeDiff-1 comes out as 2399

Can anybody see why the above code is not fallible ?

Bob M
New Zealand
 
Sheriff
Posts: 26777
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For example: begin time 21:50 and end time is 22:00 (same day, same year, same month)
Yet timeDiff-1 comes out as 2999 ???



If the begin time was 21:50 and the end time was 21:00, then the difference would be 50 minutes, which is 3000 seconds. You didn't say what the seconds were for each of those times so the difference could actually be 2999 seconds. So perhaps it's possible that you misread the second hours value by assuming that it would be greater than the first hours value. But actual debugging output would be useful.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The seconds are zero in each case

The end times are all either equal to the start times or are later
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . If the begin time was 21:50 and the end time was 21:00 . . .

That's 23 hours 10 min, or 83400 seconds. (I think.)
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No....every end time is equal to or greater than the beginning time
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That has got me even more confused, I am afraid.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any comments or documentation with this code that describes what it is trying to do?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To backtrack a little......................

weka requires attributes to be fed into it before it can make a prediction

one of these attributes is the number of 10 minute intervals that an event covers from start to finish

The code appears to be calculating the number of seconds that an event covers

However, I have found a flaw in the code.........

1) if the interval spans midnight, the number of seconds it calculates is wrong - way to big

2) this is also true if the interval spans two days or more

3) worse still, I then checked some of the what looks like 'normal' interval lengths such as 1, 2, ...5 10min intervals and found the code was incorrectly calculating again
but.... I can not see the reason why it deviates once in a while with intervals that happen on the same day, same month, same year

I guess I was hoping that somebody with a broad knowledge of java would spot where the code was letting me down

Bob M
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 somebody with a broad knowledge of java would spot where the code was letting me down


Can you write an complete, detailed description of what the code is supposed to do?
Given that we can help you write the code that does what is required.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code appears to me to be calculating in seconds two different times
One - the beginning of an event and the other the end of an event

By subtracting the former from the latter we should get the time interval of that event in seconds

This is the timeDiff which is printed out

If the event took place over one 10minute interval then timeDiff would be 10 minutes = 600 seconds

But like I say, if the interval spans midnight the calculation goes awry

And I am interested in why and how to correct the code that calculates the time interval in seconds
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the calculations on this site: https://stackoverflow.com/questions/25747499/java-8-difference-between-two-localdatetime-in-multiple-units
I found a set of code by Thomas that worked.  I didn't try them all,  There could be others that are better.


What Strings are in the startDate, startTime, endDate and endTime fields?
Can the parse methods be used to build Calendar objects instead of all the ugly regular expression code?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are as follows:-

endDate 21.06.2021
endTime 00:20:00

startDate 20.06.2021
startTime 23:50:00

Had a look at that site Norman............

and my head hurts !
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those date and time Strings can be handled by the SimpleDateFormat class's parse method to create Date objects.
The milliseconds of the two Dates can be subtracted and then divided to get the seconds between the times.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Norm

but the calculations will still go astray every so often as the code stands now
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

as the code stands now


It is usually better to write new, clean, working code than trying to fix poor, broken code.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
agreed
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest writing a small, simple program to work out the techniques for computing the time between two dates.
Create 4 Strings with the start and end date and times.  Then write code to concatenate them  so they can be parsed with the SimpleDateFormat's parse method.
The milli secs from the Date objects can be subtracted for the difference and the some divisions to get the desired values.

Post the code here if you have problems.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Norm
 
Paul Clapham
Sheriff
Posts: 26777
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . If the begin time was 21:50 and the end time was 21:00 . . .

That's 23 hours 10 min, or 83400 seconds. (I think.)



Notice the use of "Math.abs" which converts -50 minutes into +50 minutes.
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm said.........


I'd suggest writing a small, simple program to work out the techniques for computing the time between two dates.
Create 4 Strings with the start and end date and times.  Then write code to concatenate them  so they can be parsed with the SimpleDateFormat's parse method.
The milli secs from the Date objects can be subtracted for the difference and the some divisions to get the desired values.



so



Is it something like this ?

Bob M

New Zealand
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would write a separate small program with hardcoded start and end values for testing.
When that code worked, then copy it into your big program.


Does the posted code work?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my current code (remembering that I am trying to use what I have been given (at the moment)



I'll see what this produces
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using "testEvents[k]" ? Just use start_date and end_date (which should really be startDate and endDate).
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because I am looping through a series of events
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not if you're just writing a simple test program.

Even if you were, then, why pass in start and end dates?
 
Bob Matthews
Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my class...............



and I am calling it thus................


error on line 6:- the method findDifference(String, String) is undefined
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic