aspose file tools*
The moose likes Beginning Java and the fly likes Dsplaying the alphabet with a loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dsplaying the alphabet with a loop?" Watch "Dsplaying the alphabet with a loop?" New topic
Author

Dsplaying the alphabet with a loop?

Tricky Phillips
Greenhorn

Joined: Mar 21, 2005
Posts: 4
I recieved an assignment from my Java teacher, and I'm not sure how this could be done. He wrote;

"Write a Java program that will produce the following output. The program should print the heading line and then produce the next list using a loop to step through the alpgabet in ascending and descending sequence at the same time. Hint: perform the loop 26 times.

ALPHABET FROM A TO Z alphabet from z to a
Az By Cx Ev Fu Gt Hs Ir Jq Kp Lo Mn Nm Ol Pk Qj Ri Sh Tg Uf Ve Wd Xc Yb Za"

What I'm looking for is some way to make my "currentletter" variable +1 or -1.

Thanks.
Tricky Phillips
Greenhorn

Joined: Mar 21, 2005
Posts: 4
Alright. I've realized that I have to use ASCII along with "int++" and "int--" so now I'm trying to figure out how to display characters by using their ASCII value. Thanks.
Raj Young
Greenhorn

Joined: Mar 16, 2005
Posts: 9
char[] ch = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

int j = ch.length;
System.out.println("ALPHABET FROM A TO Z alphabet from z to a");

for (int i = 0; i < ch.length; i++)
{
System.out.print(Character.toString(Character.toUpperCase(ch[i])) + Character.toString(ch[j - 1]) + " ");
--j;
}
System.out.println();




I think the above code sample helps
Tricky Phillips
Greenhorn

Joined: Mar 21, 2005
Posts: 4
Here's what I've got so far, but I'm having a problem on line 21 and 23, since "ASCII" isn't a real method (I'm just using it as an example for what I want).


What method can I use to turn that int into an ASCII character?

Thanks!
[ March 21, 2005: Message edited by: Tricky Phillips ]
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
A variabel of type char can be used both as a number an as a character, which means that it is possible to do calculations with it, like incrementing it with the ++ operator. The following code shows an example where this is done.



The output will be:
A
B
Tricky Phillips
Greenhorn

Joined: Mar 21, 2005
Posts: 4
Originally posted by J Isberg:
A variabel of type char can be used both as a number an as a character, which means that it is possible to do calculations with it


Wow. Thanks a lot. Looks like I was taking the extra step for nothing. I'm too tired to test it out right now, but I'll take a look first thing tomorrow morning.

Thanks for the help.
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
Hopes this will help u.

public class Alphabets1
{
public static void main(String args[])
{
char ascending='A';
char descending='z';
while (ascending<='Z')
{
System.out.println (ascending + " " + descending);
ascending++;
descending--;
}
}
}

Raghu
Cheers!!!


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Raj Young
Greenhorn

Joined: Mar 16, 2005
Posts: 9
Hi Raghu,

ur soln is good, and it works also thanks
[ March 21, 2005: Message edited by: Raj Young ]
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Originally posted by Raj:
Hi Raghu,

ur soln is good, only thing is that u need to do a Character.toString(ascending) + Character.toString(descending) while printing the character, otherwise it will print a integer equivalent of char


No you don't.

System.out.print() and ...println() can take a char.

This is my solution...


Rene


Regards, Rene Larsen
Dropbox Invite
Raj Young
Greenhorn

Joined: Mar 16, 2005
Posts: 9
yes u r right, no need to do Char.toString(), I directly concatenated the two values in the print method, that's why I didn't get previously.

System.out.println(ascending + descending);

that's why I have got the number it seems.

thanks for ur reply
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Several points:

1). It looks like Tricky came up with a solution on his own, but I would like to take the moment to remind everybody that JavaRanch is not here to do other people's homework for them. Please do not post whole solutions to homework problems; that is not a good way to teach people! (J Isberg's post was the proper approach to take -- just help on the places where the original poster is stuck...)

2). Raj: Welcome to Java Ranch! Please read our Naming Policy and then Change your display name to comply (We are looking for a first and last name, preferably your real one, but one that is not obviously fake...)

3). Raj: Actually, shree's code will work just fine.

4). Please remember to use [CODE] and [/CODE] tags to protect the formatting of your code. (It makes it so much easier to read.)
[ March 21, 2005: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Raj Young
Greenhorn

Joined: Mar 16, 2005
Posts: 9
Originally posted by Raj:
Hi Raghu,

ur soln is good,
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What method can I use to turn that int into an ASCII character?

When you assign an int variable to a char variable, Java does this automagically for you. So, in the case of your code above, you can just do this:

As you can see above, there are ways to solve this problem with less code than what you did. However, I thought I'd still answer your question because it is a nice tidbit to know when the occassion calls for it.

BTW, congratulations on getting your solution to work! Feel free to come back with more questions.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dsplaying the alphabet with a loop?