This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Java program with arrays  RSS feed

 
Greenhorn
Posts: 29
  • 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: 3353
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: 29
  • 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: 3353
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: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, would I do that using a if..else if?
 
Marshal
Posts: 62803
203
  • 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: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the user's input directly as the index to the array:
 
Master Rancher
Posts: 1043
17
Firefox Browser 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: 5460
55
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'.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!