Results 1 to 11 of 11
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Need help turning numbers into letters

    How to write a code that allows the computer to read a number from the keyboard, from 0-26 and print out that much of the alphabet
    *For example
    input = 3 output = ABC
    input = 7 output = ABCDEFG
    input = 0 output =

    I tried this but it only gives me one letter that corresponds to the number

    package pkg2911homework.pkg1;
    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    Scanner keys = new Scanner(System.in);

    System.out.print("Please enter a number: ");
    int n = keys.nextInt();
    char c = (char) n;
    System.out.println(c);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Need help turning numbers into letters

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    What are your ideas on how to print out the letters?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    Make a char array with letters a-z, then when you enter the number, iterate through indexes 0 - n of the char array and print out the char.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    You're just going to want to scan that number that they put in and save it as x or something. Then do

    Java Code:
    for(int i = 0; i < x; i++) { 
    //code here
    }
    Then, where the //code here part is, print out from an array with the letters using the indexes of i

  5. #5
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    i don't get this part of what you said, "print out from an array with the letters using the indexes of i".

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,336
    Rep Power
    5

    Default Re: Need help turning numbers into letters

    Thinks substring!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    Thanks

  8. #8
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    This source code works but I did not create them.
    Java Code:
    Scanner keys = new Scanner(System.in);
    int n = 1;
    
    System.out.print("Please enter a number: ");
    int input = keys.nextInt();
    
    while(n <= input) {
    System.out.println((char)(n + 64));
    n = n + 1;
    }
    MY QUESTION:
    I know that the variable n in the Boolean expression holds a value of 1 and it's asking, "is n less than or equal to input?" Let's say the user entered 3, the Boolean expression will now read, "(1 <= 3)," which is true therefore the program executes the print out statement below. The n being added to 64 holds no value because it wasn't initialized inside the while brackets so it's basically 0+64=64 and the (char) turns 64 into the letter A. Now n=n+1 adds 1 to n, which holds a value of 0? Therefore n is now equal to 1. SO MY QUESTION: What value will the variable n inside the Boolean expression hold? I'm letting you know my thinking process so correct me if I said anything wrong.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,336
    Rep Power
    5

    Default Re: Need help turning numbers into letters

    First, you initialized n to 1 not 0. The value of n inside the boolean expression will be the current value of n. So after n = n + 1, the loop starts over and n is checked again. Using your value 3 as input, n will be 0, 1, 2, 3, 4. When it becomes 4, the loop exits.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help turning numbers into letters

    Wouldn't that print out more than 3 Alphabets? So i'm right about n in (n + 64) holding a value of 0 when the loop first iterates?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,336
    Rep Power
    5

    Default Re: Need help turning numbers into letters

    The n being added to 64 holds no value because it wasn't initialized inside the while brackets so it's basically 0+64=64 and the (char) turns 64 into the letter A. Now n=n+1 adds 1 to n, which holds a value of 0? Therefore n is now equal to 1. SO MY QUESTION: What value will the variable n inside the Boolean expression hold? I'm letting you know my thinking process so correct me if I said anything wrong.
    Your code works correctly as written. I was simply correcting what you said above. You said 0 + 64 turns character into a A. That is not true, 0 + 64 is an @ sign. You also said (implied) that you initialized n to 0. You didn't. You initialized it to 1.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Turning numbers into words
    By Humphrey Bogart in forum New To Java
    Replies: 4
    Last Post: 03-23-2013, 09:18 AM
  2. turning string numbers into doubles
    By imorio in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 10:25 PM
  3. converting letters to numbers
    By fenhoff in forum New To Java
    Replies: 2
    Last Post: 01-24-2010, 04:45 AM
  4. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 07:38 AM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •