• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 64184
215
  • 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:
 
Rancher
Posts: 1170
18
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.
 
Bartender
Posts: 5776
56
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'.
 
Don't listen to Steve. Just read this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!