Results 1 to 6 of 6
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Assignment of char and int question

    What I am trying to do is have the scanner read 5 separate numbers (five times) and then output characters associated with the input number without using any kind of loop. For example, if the number 1 is input, display b, number 2 display c, number 3 display 4, etc. My best shot produces long long code. Then I had an idea (that didn't work) to do assignments of an integer = number = (char). I have tried:

    int a = 1 = (char) a; and get an error mark under the number 1 thinking that if the user input a 1, it would be assigned the b character to a new variable to be printed out later. Note: I will only be using the numbers 0-9.

    The result is that words would be output at the end of the program based on the numerical input/conversion to char process.

    Any ideas would be greatly appreciated. Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You could try using a switch, I think this would fit what you want.
    Java Code:
    switch(i){
      case 1:
        assign b;
        break;
      case 2:
    ...
    Last edited by sunde887; 01-21-2011 at 07:43 AM.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    You can use array.
    Java Code:
     char[] selectedChar = {'a', 'b', 'c'};
    then use the user input as an index of the char[].
    Java Code:
    System.out.println("Enter number: ");
    int input = Integer.ParseInt(System.in.read());
    System.out.println(selectedChar[input]);

  4. #4
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default

    Thanks Sunde887. I am able to do this with a case statement or if statements after every input but was hoping to find a shorter way. mine0926 - thanks. An array will work but can't use an array for this problem. Is there an unusual enumeration that would work?

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Why you cant use array? Can you show your code?

  6. #6
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Can't use array because

    A friend taking a Java class had this project in her text. She couldn't figure it out so asked me. Up to this point in the text they have only dealt with if, else if and switch statements and loops are taught in the next lesson. The reason the question was asked about the unusual enum was that was the title of the project she is working on. So, I have assumed that there must be something out of the ordinary enumeration that would eliminate the switch statement after each number is entered.

    Thanks for your help

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 10:24 PM
  2. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-25-2010, 12:44 AM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  4. i have a question on my assignment??(java)
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 12:15 AM
  5. Question about school assignment
    By wata in forum New To Java
    Replies: 7
    Last Post: 08-18-2009, 03:00 PM

Tags for this Thread

Posting Permissions

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