Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Java program with arrays  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a program that asks the user to enter a number. The program should print the alphabet letter corresponding with the number (1-26):
This is the code i have so far: (not sure if I have to user an if.. else loop but I feel as though that would be inefficient )

 
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any specific java programming questions about the program?

There is no need for a loop. Use the user's input value to access the array since the array's contents are in sorted order: first is a and last is z.
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes how would I do the actual number corresponding to the letter of the alphabet?
 
Norm Radder
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the user's input value to access the array since the array's contents are in sorted order: first is A and last is Z.
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, would I do that using a if..else if?
 
Marshal
Posts: 61727
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need an array; you can do it all with arithmetic from the number entered. You don't need an if‑else either. But you might need a loop of you want more than one letter back.
 
Norm Radder
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the user's input directly as the index to the array:
 
Rancher
Posts: 515
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go like Ritchie said and increment the charachter by the value.
Start off with setting a charachter to 'a'. Ask the user for which letter he wants and use that number as boundery for a for-loop , increment the charachter each run, after the loop you print the char.
 
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that in Java array indexes start with zero but your user will probably expect 'A' if they enter '1'.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!