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.
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
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
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!
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.
- X 2
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.
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! ;)
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.
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
It would replace the %,20.0f from what Im working with now
Thanks a lot for all the help ! xD
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 ^^
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.
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.)
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 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
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
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 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.
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
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.