• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using java String.format()  RSS feed

 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm trying to format my output in a program I'm writing but String.format() messes out the output. I've looked at a few tutorials on how to use it as well as how to use the syntax but none of them work out for me.

This is my code for the string:


This is my current output and my format string is: String format = "%-4s %-20s %04d"; (I used the 0's to keep track of where the itemName Ends)



Based on this tutorial, -4s would mean that my itemNumber takes up 4 spaces and will be left justified, then my itemName will take up 20 spaces and would be left justified, then 04d would right justify my itemValue and pad it with 0's to the left if the value does not take up 4 character spaces. my question is why does it look like Can and Plastic bag are consuming a different number of spaces when I'm defined that they should take 20 ?

Plastic bag is 10 characters (11 with the space) so nothing is overflowing, how can I get output like this:


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps you can provide an SSCCE so we can reproduce your output?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was so intrigued with this that I expanded the appExactly as expected. Please supply more details of your app; I cannot reproduce the problem.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see I should have missed out Integer.parseInt() around args[0]. removing it makes no difference to the output.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the itemName variable doesn't include any trailing spaces ? If it was(that's 11 trailing spaces) it would push the value position out.
Try using

to see if that helps.
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I was so intrigued with this that I expanded the appExactly as expected. Please supply more details of your app; I cannot reproduce the problem.


I tried taking your format and the result was still jumbled up. I have about 13 classes, if you want I could PM you a rar file with my code.

The app is basically a recycling machine that takes in items like can, plastic bag and others; the output I'm trying to format is for the receipt.

Stuart A. Burkett wrote:Are you sure the itemName variable doesn't include any trailing spaces ? If it was(that's 11 trailing spaces) it would push the value position out.
Try using

to see if that helps.


This doesn't change anything, the output is still messed up


This is the method that takes care of all this (myItems is a vector)
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bad news is, I can see three design errors in that little class. You have classes with non‑private fields. You are using == on a reference type. You are using the + operator on Strings in multiple statements (least serious of the errors).

The worse news is, none of those things explains your formatting problem. I still think it has to do with additional whitespace somewhere. As I said. I could not reproduce your errors.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use \n or \r in format Strings.
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll just play around with this even more and see if I can resolve the problem. Thanks for the tips and the attempt to help.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't play around with it.Investigate the error properly, by putting proper debugging code in.

Another design error I have seen:- What will happen when your recycling stuff is a "glass bottle"? You have some bad design with those if‑elses. They are using implementation details of the recycled item class, viz that it might be a can or plastic bag. Adding bottles or foil containers means you have to change the code twice. you should only change the code once.
Maybe, move the counting into the item class. Maybe, you don't yet know how to do this.
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, don't play around with it.Investigate the error properly, by putting proper debugging code in.

Another design error I have seen:- What will happen when your recycling stuff is a "glass bottle"? You have some bad design with those if‑elses. They are using implementation details of the recycled item class, viz that it might be a can or plastic bag. Adding bottles or foil containers means you have to change the code twice. You should only change the code once.
Maybe, move the counting into the item class. Maybe, you don't yet know how to do this.


Most of this code was pre written, my job was just to enhance it.

I have tried several ways of debugging this problem. if I specify that the Max width of string should be 11, it does this for me.

Can | <-- This is 11 characters so it ends there but for plastic bag which evidently is 11 characters on its own this happens (it's 11 without some of the white space cleated out after posting)
Plastic Bag| <-- They end at different points and so whatever needs to be formatted next (the values) come out jumbled up.

I've also trimmed every string I can but nothing hcanges
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through each of those Strings and print its length on screen. Also print the number values of the individual chars, remembering space = 0x0020, A‑Z = 0x0041‑0x005a, a‑z = 0x0061‑0x007a. Find any chars which are not in those ranges.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is totalCans? Is it a field? Why? If you run that method twice, are you going to get an incorrect number of cans?
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is totalCans? Is it a field? Why? If you run that method twice, are you going to get an incorrect number of cans?


Yes it is a field, it's meant to count the total amount of cans inserted into the machine; I won't get an incorrect number of cans because after every "Transaction" everything is reset
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also discovered something new, if I print my output on console it works out but on a JTextArea it doesn't print how I want it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Sigauke wrote:I have also discovered something new, if I print my output on console it works out but on a JTextArea it doesn't print how I want it.

In that case your JTextArea is probably using a proportional font rather than a mono-spaced font. Try specifying a mono-spaced font.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Sigauke wrote: . . . but on a JTextArea it doesn't print how I want it.
You never said anything about text areas. Please read this.
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Lee Sigauke wrote: . . . but on a JTextArea it doesn't print how I want it.
You never said anything about text areas. Please read this.


Yeah, I realised that, I'm sorry for all the trouble I'll try and be clearer next time. Thank you for all your help.

Tony Docherty wrote:
Lee Sigauke wrote:I have also discovered something new, if I print my output on console it works out but on a JTextArea it doesn't print how I want it.

In that case your JTextArea is probably using a proportional font rather than a mono-spaced font. Try specifying a mono-spaced font.


This worked, thank you
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted.
 
Lee Sigauke
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Apology accepted.


I had also assumed that the JTextArea and console had the same formatting so didn't see it necessary to mention it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Sigauke wrote:I had also assumed that...

Hence the saying "Assumptions are the root of all evil"
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't actually say anything about Fonts in the JTextArea documentation.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it does mention fonts, but doesn't say anything about a default font.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!