This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

.get() Method and String formatting

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I'm trying to modify a program per the Lesson requirements and I've determined that I just have to change the operator but I'm trying to gain a deeper understanding.

I've searched and searched the Internet (event tried the Oracle site).  I found some information regarding "%s" but no information that details what %s actually does?

Also, the following line has me in the dark: " newCounter = counter.get() * counter.incrementAndGet();"  How does .get() work; what does it specifically do?




Thanks in advance for any assistance!



 
Saloon Keeper
Posts: 9720
80
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
Is this Android? What is 'template'? What type is 'counter'?
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an example link that shows the %s although I can't tell exactly what it does:

https://compiler.javatpoint.com/opr/test.jsp?filename=FormatExample
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the entire file:




Sorry, no, it isn't Android and I'm using STS...
Staff note (Ron McLeod) :

STS = Spring Tool Suite

 
Carey Brown
Saloon Keeper
Posts: 9720
80
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
https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html Oracle's javadoc on the syntax of a formatter string.

A formatter String is a String that usually contains one or more substitution macros that begin with '%' and end with a letter. For instance, '%s' would be replaced by a String from the argument list that follows the format String.

The two most common methods that know how to parse and process a format String and the following arguments are:
String.format(...)
System.out.printf(...)

Example:
System.out.printf("Hi there, %s",name);
The '%s' is replaced by the contents of the variable 'name'.

Format macros can have a variety of modifiers between the '%' and the closing letter 's'.
Example:
System.out.printf("Hi there, %-10s (good to see you again)",name);
The '%-10s' is replaced by the contents of the variable 'name'. The '10' tells the formatter to allocate a minimum of 10 characters for the name and if there are less to pad them with spaces. The '-' tells the formatter to left justify the name within the 10 characters.
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey -

Thank you so much for your post, examples, and the link!
 
Sheriff
Posts: 17305
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Is this Android? What is 'template'? What type is 'counter'?



since newCounter is declared as a long, I would guess counter is an AtomicLong object.

That would also explain the counter.get() and counter.incrementAndGet() method calls.

The incrementAndGet() method is the equivalent of the ++n (pre-increment operation)

The getAndIncrement() method is the equivalent of the n++ (post-increment operation)

Search for examples of AtomicLong.incrementAndGet()
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu -

Thanks for taking the time to respond.

I'm just attempting to understand what .get() does?

I understand what incrementAndGet does but I'm not understanding what 'value' is residing in counter.get()?



Hope this clarifies.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
counter.get() just returns the current value of counter.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicLong.html#get()
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Knute -

Thank you, thank you, thank you!
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic