hi..im new here and to java...im supposed to align the prices of a receipt in a system.out.println..and i dont kno how. i am supposed to align the prices on the right hand side, considering the length of the item name and length of price. Again, i dont use things such as print streamline ( i dont even know what that means) I was given a clue here and i'll put it down:

String someString = "Hello there, Kenny."
int numChars = someString.length()

so this basically counts the number of characters in the string. However, i dont know how to use this effectively because the items the user inputs are unlimited. (im not sure if im supposed to use a loop or statement)

IF ANYONE CAN HELP ME, IT'LL BE MOST APPRECIATED:-|

Your basic algorithm (at least, the one I would use for this) would be something like this:

1. Build the String.
2. Find out the length of the string.
3. Compare that length to some arbitrary maximum.
4. If the string in question is shorter than your arbitrary maximum, add spaces to the front of the string until it is the same length.
5. Call System.out.println

The size of the value you choose for your arbitrary maximum will affect how far to the right your text is aligned.

Your basic algorithm (at least, the one I would use for this) would be something like this:

1. Build the String.
2. Find out the length of the string.
3. Compare that length to some arbitrary maximum.
4. If the string in question is shorter than your arbitrary maximum, add spaces to the front of the string until it is the same length.
5. Call System.out.println

The size of the value you choose for your arbitrary maximum will affect how far to the right your text is aligned.

can someone define arbitratry maximum? and should i use an if statement?

+ how will i do this since the items inputted are unlimited until user inputs "done"

one more thing, say if i want 15 (char wise) but i get 20, how do i make the 20 into 15..

Let me make a few guesses, they may be off base.
You have the following:
System.out.println(strOutput)

But, strOutput is actually comprised of strItem & strSpacer & strPrice (The name of the item, blank spaces or dots, then the price of the item).

Also, you know how wide the receipt is. That is, you know how many characters will fit nicely on each line. That number is your arbitrary maximum. It is arbitrary because if you find that it isn't big enough, there isn't a problem making it larger. When I refer to "number of characters per line" this is the value which I mean.

Now. The item's name should be left-aligned, the price right aligned. Is that correct?
If so, you need to compare the length of the two strings combined to the number of characters per line.
If there is room to spare, you need find out how many more characters will fit, then add that many characters to strSpacer, insert strSpacer in between the two strings and check again. If the length of the combined strings is the same as the number of characters per line, the line can be printed.

You will have to use an If block somewhere in this code.

+ how will i do this since the items inputted are unlimited until user inputs "done"

You need to limit that. Don't leave it up to the user to input a string which isn't longer than the line will accept. Write your program in such a way that it tells the user if they try to input a string which is too long. You can do this by changing your If block to an If-Else block. The If section check to see if the input is too long, and if it is the program tells the user and asks for a different input. The Else section performs the width check and adds characters to strSpacer if needs be (as above).

one more thing, say if i want 15 (char wise) but i get 20, how do i make the 20 into 15..

You shouldn't be doing that. If someone enters a string which is longer than your program is expecting it should either give out an error message, or you should modify your program to accept longer strings. In this case, I'd go with the error message as the maximum length acceptable should already be as long as will fit on the receipt.

Have a go at the If block and post that if you can't get it to work. If you do, make sure to tell us what the type and expected values for each variable are (unless it is obvious from the code). You'll also need to tell us how many characters you want the receipt to accept.

It doesn't matter. From what I read on his first post, he has to use println(). Besides, knowing how to do it is useful in its own right.

I wasnt implying that he should use it, all i said was thank god printf is now included in java 5. Learning to format IS a good thing, but once you understand it already shortcuts are always a plus. Printf is also helpful in easing the (already fairly smooth) transition from Java to C or vice versa.

Have you tried searching the forum for similar questions? At the bottom of this thread I clicked on the first "similar" thread mentioned which brings up a thread I replied to before with a solution. My method pads a string to make the length match up to whatever you need, left or right aligned.

This article has been dead for over six months. Start a new discussion instead.