• 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
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Adding space in printf format

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm very new to Java and am having some difficulty wrapping my head around when to do what and so forth.  My current assignment is to input a number and have it reversed.  I believe that I have the correct code but my out put does not want to place spaces between the entry and the result.  I've researched what may be the issue and I believe it has to do with the printf format and adding in ""+ between the text, entry and result but that does not seem to be working.  Hence, I'm probably not correct. I thought that "" would add in spaces so that my output would read correctly.  Here is my code and result.  Any help would be appreciated.


Result:
Enter the number to be reversed : 456
The reverse of456is654
 
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brian Halpin wrote:I thought that "" would add in spaces so that my output would read correctly.



How would a simple string concatenation know what you had in mind for spacing "correctly"? All it can do is to concatenate the strings it's given. Inserting spaces you didn't ask for would be a bad idea. (It would be even harder for the printf method to examine the string, intuit where you wanted spaces to appear, and insert those spaces.)

However it's very simple for you to put in the spaces yourself, because you know where you want them to go. Try this change:



You'll find it still doesn't insert all of the spaces you want, but you should be able to fix it now.
 
Ranch Hand
Posts: 37
Python Angular Framework Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer using String.format instead of concatenation using the + sign.

Here is what is should look like:

 
Marshal
Posts: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Please look here about how the code button works; since you are new I have corrected your tags and doesn't the ode look better

Here is what is should look like:

Sinve entry and result are ints, I would use %d, and I would add a %n at the end, otherwise you stay on the same line:-Beware: the technique using % can be unpredictable for negative numbers. I think that using 10 * will give you a correct output for negative numbers, but I am not certain.
 
Brian Halpin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help.  The String.format seemed to be the way to go.  To learn something so complicated it's frustrating to overlook and overthink these things sometimes.
 
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.out.printf(String.format()) incantation is redundant. All you need is System.out.printf():

The %n at the end causes a newline to be printed, as would be with System.out.println. Without %n, it would be like using System.out.print() to display something.

Basically, if you're using string concatenation with printf() then you're using it wrong.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try either of them:




Or try using the code as suggested by Junilu Lacar
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sherin Mathew wrote:Try either of them:


Like I said, using concatenation with printf() is NOT the way to do it. That's kind of like trying to cut wood with a power saw by swinging it back and forth without turning it on. Or using an electric toothbrush like you would a regular toothbrush. You're basically doing all the work the method was designed to do for you.
 
I think I'll just lie down here for a second. And ponder 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!