Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Print a letter of the alphabet based on a number that is input

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print a letter of the alphabet based on a number that is input

    I am trying to print a letter based on the user's input. So far I have:

            System.out.println("Enter an integer from 1 to 26:");
            Scanner alpha = new Scanner(System.in);
            int integer = (char)alpha.nextInt();
     
            char character =  (char) integer;
            System.out.println(character);

    For example, if the number 7 was input, the output would be the letter G.
    But all this does is print a blank line, can anyone help me with this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Print a letter of the alphabet based on a number that is input

    To convert the int value 7 to the character 'g' you need to understand the ASCII values. Do a google on the ASCII values and find the int value for the char 'g'.
    You can do arithmetic with chars. For example: 'a' + 2 = 'c' or 'g' - 'a' = 6
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  2. Switching letter-cases from user input; why is not working?
    By Kimiko Sakaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 27th, 2011, 07:48 AM
  3. Parsing input based on grammar rules
    By Winterfresh in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2010, 07:54 PM
  4. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM