Connecting Tech Pros Worldwide Forums | Help | Site Map

how to align things

Newbie
 
Join Date: Oct 2006
Posts: 24
#1: Oct 24 '06
hi im new here and to java..im supposed to output a receipt that aligns the prices to the right. Since im new, i only know the basics. I was given a clue on how to do this:

Strin someString = "Hello there, kenny.";
int numChars = someString.length();

so basically i need to find a way to align prices to the right, keeping in mind the length of the item and and length of the price..any ideas will be most appreciated

Newbie
 
Join Date: Oct 2006
Posts: 24
#2: Oct 24 '06

re: how to align things


oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)

Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

Suppose you want the prices to appear at 60 spaces from the left of the console

For each item, you get the length of the item's name using the method you were give above.

int l = item.length();
Then you get how many spaces you need to add using
int spaces = 60 - l;

So when printing item item1 and its price, You print the item name, then you print spaces spaces, then you print the item's price and go to the next line. If you do this for all the items, you will always get the prices aligned 60 spaces form the left of the screen assuming the names of the items are not more than 60 characters long
Newbie
 
Join Date: Oct 2006
Posts: 24
#4: Oct 24 '06

re: how to align things


[quote=r035198x]Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

so for the output, wat do i put exactly..s.o.pln (item + space)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Oct 24 '06

re: how to align things


[quote=kenvin100]
Quote:

Originally Posted by r035198x

Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

so for the output, wat do i put exactly..s.o.pln (item + space)

Expand|Select|Wrap|Line Numbers
  1. String space = "";
  2. for(int i = 0 ;i < spaces; i++) {
  3.      space = space + " ";
  4. }
  5. System.out.println(item + space + price);
Newbie
 
Join Date: Oct 2006
Posts: 24
#6: Oct 24 '06

re: how to align things


k thx for your help..gonna try it now
Newbie
 
Join Date: Oct 2006
Posts: 24
#7: Oct 24 '06

re: how to align things


u kno where u put space as the output, am i supposed to put a number there or space..cause i declared space and it doesn't work
Newbie
 
Join Date: Oct 2006
Posts: 24
#8: Oct 24 '06

re: how to align things


my code doesnt seem to work...let me show u..


String out = "";
int space = 17-1;
for(int i = 0 ;i < 17; i++) {
out = " ";
}

is this rite?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

u kno where u put space as the output, am i supposed to put a number there or space..cause i declared space and it doesn't work

what does the error say
Newbie
 
Join Date: Oct 2006
Posts: 24
#10: Oct 24 '06

re: how to align things


there's no error it's just that its not spacing..
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: Oct 24 '06

re: how to align things


Expand|Select|Wrap|Line Numbers
  1. String item = "something";
  2. double price = 20.0;
  3. int spaces = 50 - item.length();
  4.  
  5. String space = ""; //declare the variable space
  6. for(int i = 0 ;i < spaces; i++) {
  7.      space = space + " "; //
  8. }
  9. System.out.println(item + space + price);
Newbie
 
Join Date: Oct 2006
Posts: 24
#12: Oct 24 '06

re: how to align things


+ if i declare a space as 17-1, the output shows 17, i kno its an int but there should be a way to solve this..
Newbie
 
Join Date: Oct 2006
Posts: 24
#13: Oct 24 '06

re: how to align things


and "space" and "spaces" are two different things..
Newbie
 
Join Date: Oct 2006
Posts: 24
#14: Oct 24 '06

re: how to align things


it works a little but there's still some problem..apparantly items with larger names arent jusitifed..subtraction sign is space

item---------------------------- quantity@price...
item------------------------------------------------------------quantity@price...

see the problem?
Newbie
 
Join Date: Oct 2006
Posts: 24
#15: Oct 24 '06

re: how to align things


of course the second name is longer..
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#16: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

it works a little but there's still some problem..apparantly items with larger names arent jusitifed..subtraction sign is space

item---------------------------- quantity@price...
item------------------------------------------------------------quantity@price...

see the problem?

May you please post the code that produced the above output
Newbie
 
Join Date: Oct 2006
Posts: 24
#17: Oct 24 '06

re: how to align things


i think u misunderstood wat im supposed to do...im supposed to align the prices to the right regardless the length of the item
Newbie
 
Join Date: Oct 2006
Posts: 24
#18: Oct 24 '06

re: how to align things


no problem...i used string out instead..

String out = "";
int space = 0;
int spaces = 50 - item.length();

out = ""; //declare the variable space
for(int i = 0 ;i < 27; i++) {
out += " "; //
}
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#19: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

i think u misunderstood wat im supposed to do...im supposed to align the prices to the right regardless the length of the item

That's what I thought you wanted to do
Newbie
 
Join Date: Oct 2006
Posts: 24
#20: Oct 24 '06

re: how to align things


maybe i need an if statement
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#21: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

no problem...i used string out instead..

String out = "";
int space = 0;
int spaces = 50 - item.length();

out = ""; //declare the variable space
for(int i = 0 ;i < 27; i++) {
out += " "; //
}

What is the 27 doing there. You are supposed to put spaces as in
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0 ;i < spaces; i++) 
Newbie
 
Join Date: Oct 2006
Posts: 24
#22: Oct 24 '06

re: how to align things


wat does 50-item.length(); mean
Newbie
 
Join Date: Oct 2006
Posts: 24
#23: Oct 24 '06

re: how to align things


k im almost there..its almost aligned
output:

apple 1@1.0
fresh baked bread1@1.0

i just need to solve this problem (dont ask why i put fresh baked bread..laughs)
Newbie
 
Join Date: Oct 2006
Posts: 24
#24: Oct 24 '06

re: how to align things


Yes I Got It!! Thanks Man!!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#25: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

wat does 50-item.length(); mean

item1------------------------------------------20
myItem2--------------------------------------30
mylongerItem-------------------------------40

The area marked --------------------- is what we are trying to construct and store it as space(variable);
First we have to know how many dashs (spaces).
Suppose that where we have the 20, 30, 40 is exactly 50 spaces from the left of the screen. Then spaces = 50 - item1.length();
Newbie
 
Join Date: Oct 2006
Posts: 24
#26: Oct 24 '06

re: how to align things


k...there's only one problem..let me show u..

item------ quantity@1.0
item-------quantity@2.06

the problem here is that if the price has two decimals, (eg 12.46), the alignment is off..how do i solve this
Newbie
 
Join Date: Oct 2006
Posts: 24
#27: Oct 24 '06

re: how to align things


since there's supposed to be aligned at the right side so im trying adding spaces but it doesnt work
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#28: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

since there's supposed to be aligned at the right side so im trying adding spaces but it doesnt work

Restrict the output to 2 decimal places only. 1.0 becomes 1.00
Newbie
 
Join Date: Oct 2006
Posts: 24
#29: Oct 24 '06

re: how to align things


um all i kno how to do is to make numbers with two decimal places..im usin that in this code like if the person puts 1.0695, it outputs as 1.07
Newbie
 
Join Date: Oct 2006
Posts: 24
#30: Oct 24 '06

re: how to align things


its just that if i put something like 12.46, its not aligned

apple 1@1.06
fresh baked bread 1@12.46


see the problem?
Newbie
 
Join Date: Oct 2006
Posts: 24
#31: Oct 24 '06

re: how to align things


i forgot how stubborn the forums are wen it comes to spacing

apple-------------------------1@1.06
fresh baked bread...... 1@12.06.
any way to solve this?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#32: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

i forgot how stubborn the forums are wen it comes to spacing

apple-------------------------1@1.06
fresh baked bread...... 1@12.06.
any way to solve this?

Have a read at this
Newbie
 
Join Date: Oct 2006
Posts: 24
#33: Oct 24 '06

re: how to align things


oh..okay..never learned this before but watever..thx
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#34: Oct 24 '06

re: how to align things


Quote:

Originally Posted by kenvin100

oh..okay..never learned this before but watever..thx

you'll be looking out for where it says pad with zeros
Reply