This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Having trouble calling a method to truncate a string that is read in from a file.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This is what I have right now, I'm getting a "cannot be applied to given type" error.
 
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please TellTheDetails (that's a link to a wiki page, click on it). Which particular line does that error message refer to? Without that information people will have a much harder time helping you.
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem now. The truncate method defines several parameters but you are trying to call it with only one argument. Any call you make to truncate must have values passed to each of the declared parameters. Your design of that method is wrong. It's also unnecessarily specific which leads to code duplication.
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I see the problem now. The truncate method defines several parameters but you are trying to call it with only one argument. Any call you make to truncate must have values passed to each of the declared parameters. Your design of that method is wrong. It's also unnecessarily specific which leads to code duplication.




With that being said what would be the appropriate way to handle each existing string then?
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's talk about the code you wrote for truncate()

I don't know if you can see it but there are three duplicate sections of code there. Why the duplication? Why are the parameters named that way? Try to explain this logic in plain English without using the words "artist name," "title," or "release name." If I were to change the names a little bit, would that explanation make sense?

Code in a method like this should be oblivious to what the values it is being passed mean outside of the method. It only knows that it is being passed a String that it needs to shorten according to some rules. This is supposed to be a general utility method and as such should be able to truncate any string, regardless of what that string means in the rest of the program. With the changes I made above, is the duplication of code more clear to you now? How can you generalize this code and remove the duplication?
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm honestly not too sure on how to generalize the code.
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Remove 2/3rds of it.




Alright so I suppose I would need to call individual methods for the other two strings now or is there an easier way?
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Why not call the same method three times?




What would that look like?
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Start simple. How would you call it just once for artistName?







 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

What would this look like if you removed 2/3rds of it?
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Junilu Lacar wrote:

What would this look like if you removed 2/3rds of it?




Well I would assume exactly like the segment I posted above. Or is that wrong?
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Bauer wrote:

Carey Brown wrote:Start simple. How would you call it just once for artistName?








Excellent !!

NOW, in the method, change 'artistName' to 'str'.

The method shouldn't care what the String is, only that it is a String.
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good stuff. Now I just need to figure out how to change the ugly output into separate columns with their respective type.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm late to this game, can you show me what you've got and the output you're getting? I suspect you'll want to use System.out.printf(...).
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm late to this game, can you show me what you've got and the output you're getting? I suspect you'll want to use System.out.printf(...).






MusicFileOutput.jpg
[Thumbnail for MusicFileOutput.jpg]
Output from Music.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start out simple try to print just this...

Hopefully this will give you a hint of what printf() can do. Printf() does require careful study of the javadocs though because it has all sorts of cryptic formatting specifications for various data types, %s is the one for Strings.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... of course you'll still need to read in the other values, just don't try to print them just yet.
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Bauer wrote:I'm honestly not too sure on how to generalize the code.



Seems like you caught on by now but here's what I was hoping you'd do.

Explain your logic in plain English without using any of the field names: If the string is more than 20 characters long, then shorten it to 17 characters and add "..." on the end of that. Otherwise, there's no need to truncate the string.

As you know, the conditional part is translated to Java as

"shorten it to 17 characters" is written as

"add ..." to the end of it is

and the "Otherwise" part you already know. And that's how you get:

There is still some duplication in this code. Notice how you wrote "20" two times. If your rule changes and the cutoff length needs to be modified, you'd have to change your code in two places. That's not too much of a bother but ideally, you'd only change one thing or not have to change anything at all. The value 20 represents something. The "formula" 20-3 also represents something.  Here's a more general version of your logic:

And for convenience, you could create another method like so:

 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm struggling now to determine how I would go about calculating the duration of each song and formatting the output it produces as indicated in the attached screenshot.


Right now as far as the code goes this is what I have.


EndGoalDurationOutput.JPG
[Thumbnail for EndGoalDurationOutput.JPG]
What the output SHOULD look like.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What units is duration endOfFadeIn in?
What is 001:02:03:04? days, hours, mins, secs?



Yes it starts with days then hours, minutes, and seconds. Well actually endOfFadeIn is in seconds.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Andrew Bauer wrote:

Carey Brown wrote:What units is duration endOfFadeIn in?



endOfFadeIn is in seconds.

 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait, do you mean a different unit of measurement?
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seconds makes sense, though it looks like it's accurate to the millisecond. Your duration doesn't take milliseconds into account (which is ok).
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Seconds makes sense, though it looks like it's accurate to the millisecond. Your duration doesn't take milliseconds into account (which is ok).




How do you deal with two doubles though to produce a third double(duration)?
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Bauer wrote:How do you deal with two doubles though to produce a third double(duration)?


It's not complicated, really. In your case you tried the expression (start - end) but were not getting the results you wanted or expected. Think about it though. If you start at 1 and end at 15, should the duration be calculated as 1 - 15 or 15 - 1?

Also, you are obsessing on primitives (the "formal" code smell name). The duration between two points in time is not just a simple double value. There are seconds, milliseconds, minutes, etc. involved. That logic is already encapsulated in classes in the java.time package. I suggest you check those out and use the appropriate class(es)
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, you are obsessing on primitives (the "formal" code smell name). The duration between two points in time is not just a simple double value. There are seconds, milliseconds, minutes, etc. involved. That logic is already encapsulated in classes in the java.time package. I suggest you check those out and use the appropriate class(es)

"Duration" would be nice but it doesn't handle days. You'd have to handle those separately.
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"Duration" would be nice but it doesn't handle days. You'd have to handle those separately.


So what would the toDays() method be for then, if not to handle days?
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duration.toString()

public String toString()
A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.

The format of the returned string will be PTnHnMnS, where n is the relevant hours, minutes or seconds part of the duration. Any fractional seconds are placed after a decimal point in the seconds section. If a section has a zero value, it is omitted. The hours, minutes and seconds will all have the same sign.

Examples:

   "20.345 seconds"                 -- "PT20.345S
   "15 minutes" (15 * 60 seconds)   -- "PT15M"
   "10 hours" (10 * 3600 seconds)   -- "PT10H"
   "2 days" (2 * 86400 seconds)     -- "PT48H"


Note that multiples of 24 hours are not output as days to avoid confusion with Period.
 
Carey Brown
Saloon Keeper
Posts: 6424
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, toString() doesn't format like I thought. Too bad.

 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Am I at least moving in the right direction now?
 
Sheriff
Posts: 6381
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so.  That code won't compile.  You're not doing anything to set days, hours, minutes, and seconds from duration.
 
Attractive, successful people love this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!