• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dsplaying the alphabet with a loop?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Raj Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raghu,

ur soln is good, and it works also thanks
[ March 21, 2005: Message edited by: Raj Young ]
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Raj Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Raj Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj:
Hi Raghu,

ur soln is good,

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

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic