• 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
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Problem with printf output

 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello there!
I was wondering if printf could do the following when it comes to numbers formatting :
1 345 567.0 instead of 1345567.0
I can get it to output with commas (ie: 1,345,567.0) but to add readability to my column of huge numbers I need it to put spaces instead of comma's.
If you refer me to a certain class, please include the minimum code needed to implement it.
Thanks !
"Jake" ^^
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the DecimalFormat and DecimalFormatSymbols classes. I haven't tried what you want to do but the docs suggest it could be possible.
 
Sheriff
Posts: 22810
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DecimalFormatSymbols and its setGroupingSeparator method should definitely be able to help you out.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See setGroupingSeperator().

Edit: Grrrr...
 
Marshal
Posts: 79808
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So there is still a use for DecimalFormat!
 
Rob Spoor
Sheriff
Posts: 22810
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most definitely.
 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ! thanks a lot for the help =) But I have a problem when compiling the given code (implemented within a main method, of course). It tells me :

cannot find symbol - class NumberFormat

pointing to NumberFormat, first word on line 3 in the example above

I quickly checked some basic info on NumberFormat and according to the little I understood it seems to be correct so I no understand ..
Thanks again for the help and for more to come xD
Regards,
Jake
 
Rob Spoor
Sheriff
Posts: 22810
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you imported java.text.NumberFormat?
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I hadnt :p - remember you're in beginners forum and thats exactly what I am. Its more or less pointless to refer me to the API as its still too complex for me to read in any efficient way, they're not for green noobies to read but for at least medium level programmers - I know this doesn't apply to previous answer, just generally speaking ^^

Now the compiler is telling me it doesnt recognize 'locale' (!) :s did I have to import that as well?
it had a problem with NumberFormat.getInstance(Locale.

pleee-ase help xD
Jake



or, please give me a fuller code source, its was my fault to ask for minimum code source as opposed to a 'complete' one, my bad!
 
Rob Spoor
Sheriff
Posts: 22810
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the compiler is complaining about a missing class, the first place to go to is the official Javadoc page (http://download.oracle.com/javase/7/docs/api/). Go to the bottom-left frame, scroll until you find your class (it's all alphabetical) and click on it. That will tell you the full class name, including the package. (Be warned, there are a few occurrences where classes in different packages have the same name. java.awt.List and java.util.List are such examples.) In this case, you need to import java.util.Locale.

Your compiler is complaining about locale, with a lowercase l. That leaves two options:
1) You have made a small typo and meant Locale instead of locale. Remember, Java is case sensitive.
2) You tried to use a variable that you forgot to declare.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Mauve wrote:No I hadnt :p - remember you're in beginners forum and thats exactly what I am. Its more or less pointless to refer me to the API as its still too complex for me to read in any efficient way, they're not for green noobies to read but for at least medium level programmers



Yeah, um, I'm gonna have to go ahead and disagree with you there. For a first-day, never-wrote-a-line-of-code-in-his-life newbie, yeah, the API docs can be pretty intimidating and over-the-head unhelpful. However, once you've decided to start working with Java, becoming familiar with those docs should be one of your very first tasks. It's an ongoing process, to be sure, but you should not go beyond "Hello World" without at least reading and making some effort to understand everything mentioned in that program that's in the docs. It may be a three-hour tour, but it's time well spent. After that, every single class and method you use, you must look up, read thoroughly, and do your best to understand. If not, you are absolutely wasting your time.

</rant>
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw I gave a point to Rob Spoor (same to Harsha Smith) because he was extra helpful =) I did find all I needed with your quick handy info.
Just to repeat myself, I am an uber noob and still have to be told everything. I understand how this can be frustrating for a lot, but everyone went through it. I really am trying my best to learn on my own, I've got a array (pun intended :p) of books and tutorials, but still, every once in a while I get a very specific problem with a small thing where the literature I base myself on doesn't help because they didn't account for having to explain it, and thats when I come here xD So its not that Im a 'lazy' f0cker just waiting for answers xD

About the case sensitive thing, I was aware of it, which is why I clicked on the very handy 'copy to clipboard' button over the sourcecode above to make sure I didnt do any mistakes ^^ (just wanted to specify, my case isnt that hopeless, I hope :p) I had written the first instance of "locale" with a small "eL" in my forum post, not the sourcecode ))

It took me a while to realise I had to import all the different DecimalThis and DecimalThat, and finally found on the API thanks to the extra push from Rob where I could find the 'import' line. I still had to guess and try to make sure because it only mentioned "java.text.DecimalFormatSymbols" for example, without the word import (I did a ctrl-F to see if the actual word import was anywhere on the DecimalFormatSymbol site on the API, and of course it wasn't) and so the technical details right before it didnt make it clear to me that "ahh, I need to specifically import this thing, and ahh, there's the line of java.this.that; for that specific purpose" - to me that "java.text.DecimalFormatSymbols" could have been used in any other way, whereas to you guys it only means one (clear) thing.

As usual, I like to recap, since the aim of this thread is that it serve for people with the same problem at a later date:
This code worked for me without any problems


Its the first time I use the code syntax so I hope my code came out ok (the Preview button wouldn't respond)

Thanks again for all the help! xD
Got another question !! --> next answer to thread because still same context

PS : thanks and point to Jeff too ! I totally agree, and I will do so (the teacher I had last semester was uber bad, he just mentioned once that we could always refer to the API for help, but after first try I obviously gave up on the API :p only today did it become obvious that I indeed had to force myself to learn how to read it properly and just out of curiosity's sake, check out new methods and classes as start. So, again, thanks! xD
I'm making progress thanks to you guys! ;)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Mauve wrote:I am an uber noob and still have to be told everything. I understand how this can be frustrating for a lot, but everyone went through it. I really am trying my best to learn on my own, I've got a array (pun intended :p) of books and tutorials



And that is the double-edged sword of the combination of the internet in general and Java's philosophy and popularity in particular: There's a ton of information out there literally at your fingertips. Durn near anything you want you can find, a thousand times over. The downside is, for a beginner, how does one sort through all that to find something that is correct, understandable, and useful.

So, if I can couch my earlier comments in somewhat gentler terms: Despite how overwhelming it may seem at first, for a very large portion of the questions you will encounter, the core API docs do provide a very useful resource. There is a learning curve there, but it's worth investing the time to climb it.

Good luck! And post again when you get stuck.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is, I hope, a quickie.

When using printf, you have to use code to refer to numbers you 'write' at the end of brackets, like so:



(I am aware that System.out.printf("%d",5); and System.out.printf("5"); give the same result as previous)

My question is, what letter or code do I have to use to refer to something like in the case that isn't 'enough'.
Heres the line I'll be using it in:


so otherwise stated, my question is

System.out.printf("%.?. ,df.format(x));

It would replace the %,20.0f from what Im working with now

Thanks a lot for all the help ! xD
Jake

EDIT FOLLOWING JEFF's ANSWER X) :
its much more simple. in the context of printf, to 'refer' to a decimal you use %d, to 'refer' to a floating pt you use %f and so on, like the following :


what letter do you use to refer to something like df.format(x)? would it be for example %o for object ? (as in, o for object, thats why I thought of that possibility)
and the resulting line would then look something like (!): in the case that %o is the right answer of course, but I dont know, thats my problemo ^^
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not really clear what you're asking, but let me take a guess...

Given :


You're wondering how to know how many args you need after the initial "some string", or perhaps when it's okay vs. not okay to have no args?

If this is not your question, just skip the rest of this reply and try to clarify what you're asking. However, if this IS what you're asking...

The approximate answer is that, in "some string", you will have one or more occurrences of "%something". For each "%something", there must be a corresponding element in the 'zero, or, more, args' part. There are subtleties that make it not quite that straightforward, but the basic idea is that each %something gets replaced by the value of the corresponding following arg, subject to formatting rules.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


System.out.printf("%.?. ,df.format(x));

It would replace the %,20.0f from what Im working with now



Um... one doesn't generally use printf and DecimalFormat together. There's some overlap in their functionality, so we typically use either one or the other. I've never in my life used them both together, and I can't imagine a situation where one would. (Though I supposed there's some corner case where it makes sense.)
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, just my luck .. its because i get big numbers in a column type output, and to make it easier to read them (in the order of 9-15 digits) I thought of putting that space instead of the comma you get from
System.out.printf("%,f",....);

You want to see the small program Im having fun typing up? :p



Please let me know if the small intro explanation of the program is understandable enough, cause Im obviously aware of what it does but not everyone who reads it for the first time gets it.
and there is a reason for which my stopping conditions are the way they are, I am referring to all those 9999's ^^ its because the 'value' of Pi according to the formula keeps jumping over and under the actual value of Pi until it finally narrows it down, gradually.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heres the result :



there are still three steps after this for which I've never had the patience yet :p especially considering the last one takes almost 5 hours, and thats from where it was at last step! so 5hrs to process 560,000,000,000 terms xD
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Mauve wrote:
You want to see the small program Im having fun typing up? :p



While that's small in the scope of an application, it is, for me at least, extremely large for a forum post. Given that your problem is with formatting, I would expect to see no more than 10-15 lines. Just a handful of cases of what you're trying to format and hardcoded values that you want to output. In short, an SSCCE: http://sscce.org
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fair enough ;)
I love this part : This depends on the group or forum. For a public Usenet forum, most readers will stop reading by 20Kb, and start complaining at 30Kb

I felt I needed to show the massiveness of my numbers and the special need for formatting. it was to avoid other people giving me more 'simple' ways of doing something they perceived as me doing the hardway

aand, to respond to your reply : thats why they came out with scrollbars and middle mice button scrollers :p but point taken, I will keep it short for next times. Like you can see Im barely at my 15th post
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your choice of course.

I'm just saying that I personally won't even consider reading that much code in this context, and I have not seen anything so far to suggest that more than maybe 10% of that is necessary to convey your problem.

JMHO
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just scanned your code and one thing which is very obvious to me is that your code contains a lot of duplicate code. Why don't you extract a method and call it with different arguments?
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for taking a look! it was a program i made from a small exercise which only required the 3 very first loops, then had fun making a full program out of it. And it was back in the start where I had fun starting with conditions and loops before everything else. Objects handling Im still getting my head around, plus we're supposed to be waaay ahead in our course. I had fun building a bit on it getting the timer then a better formatted output, but its true that this is probably one of the most basics ways of doing this thing. Could you show me how to reduce it?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This part gets repeated many times, make a method out of it.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the loop's stopping condition is different for every step. in that its different each time by one digit. and there are other small details that change every line, like the second %part in the printf, because there being an extra decimal digit, I need to ajust to make sure it stays aligned with the rest. the only thing which is constant throughout the whole code is the startTime/endTime before and after each loop.
I guess I could try make a method for the Times output variable. Put in one block all my conditions to check if its in secs, mins or hrs and divide accordingly (although that still sounds complicated for me). but that still wouldnt work cause I'd need it to change the sec/min/hr notation in my printf statements.
I tried once putting it all into just one for loop, have the formula going non stop and have the thing print out at every step on the way, with a simple if, using the same conditions as the individual whiles and assign all the different outputs. It would reduce the code a bit, especially since then ill have one if condition and system.out.printf statement on the same line; but, it didnt work so I just reverted back to original while loops.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as in



per while block, 1 line instead of 4.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Mauve wrote:the loop's stopping condition is different for every step. in that its different each time by one digit. and there are other small details that change every line, like the second %part in the printf, because there being an extra decimal digit, I need to ajust to make sure it stays aligned with the rest.



Then perhaps those could be parameters that are passed to the method. Or perhaps that method could be further broken down, and it could in turn call another method with the different parameters you need. I haven't looked at your code closely enough to see if this would make sense, but any time you have a lot if identical or even similar code, there's a good chance it can be simplified by creating one or more methods.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true ! I'll keep that for when I get to next chapters in the book ^^ Im still too much of a beginner.
if you want to have a look at the code, I'd suggest one of them helpful Java writing programs that displays blocks in the code in different color blocks, like with blueJ, otherwise it's a real pain to read through, especially because so seemingly repetitive. But I'll try what I said, get some loop going for the formula, and one IF condition per 'printing' statement, should at least reduce it somewhat. I would end up with at most two lines per if condition since I to stop the timer per case. as in



But do you reckon it would still be worth doing? Cause I would still be staying within the realm of 'easy code', and it wouldnt even make the calculations go faster. I'd still wait five hours for one of those last cases :p
I'll ask one of my classmates tomorrow if he can help me reduce things somewhat xD
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Mauve wrote:I felt I needed to show the massiveness of my numbers and the special need for formatting.


If you really need massive numbers, another thing to look at would be BigDecimal and MathContext. The toPlainString() method will almost certainly produce exactly what you want and you're not constrained by any limits (except those you want to impose yourself). I managed to get pi to about 20,000 dps using the Chudnovsky formula before I got bored.

Winston
 
Arthur, where are your pants? Check under this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic