-4

I need to be able to save the loop results to a string to be able to manipulate the user output. No arrays

I've attempted to convert to string inside the loop which does not make much sense. I can't figure another way to save the results unless I make another method. I am not allowed create arrays.

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

            System.out.print("Enter phone number: ");
            String number = input.nextLine();
            String phone ="";

            for (int i = 0; i < number.length(); i++){
                if (Character.isLetter(number.charAt(i)))
                    phone = getNumber(Character.toUpperCase(number.charAt(i)));
                else
                    number.charAt(i);
            }
            System.out.println("Your number is " + phone);              
    }


public static int getNumber(char uppercaseLetter){
        if (uppercaseLetter >= 'W' && uppercaseLetter <= 'Z')
            return 9;
        else if (uppercaseLetter >= 'T' && uppercaseLetter < 'W')
            return 8;
        else if (uppercaseLetter >= 'P' && uppercaseLetter < 'T')
            return 7;
        else if (uppercaseLetter >= 'M' && uppercaseLetter < 'P')
            return 6;
        else if (uppercaseLetter >= 'J' && uppercaseLetter < 'M')
            return 5;
        else if (uppercaseLetter >= 'G' && uppercaseLetter < 'J')
            return 4;
        else if (uppercaseLetter >= 'D' && uppercaseLetter < 'G')
            return 3;
        else 
            return 2;
    }
}

should look like: ie. 352-hey-call = "Your number is 352-439-2255"

azro
  • 53,056
  • 7
  • 34
  • 70
Shelly
  • 25
  • 1
  • 5
  • You need to be adding to `phone`. I recommend using a `StringBuilder` and the append on the characters in the loop – GBlodgett Feb 09 '19 at 21:31

1 Answers1

1

Actually, for each char you're looking for the corresponding letter or number, but you don't use it, you need to append them together. Using += operator on String, but as it's in a loop for better performance it's recommended to use a StringBuilder

StringBuilder phone = new StringBuilder();
for (int i = 0; i < number.length(); i++){
    if (Character.isLetter(number.charAt(i))){
        phone.append(getNumber(Character.toUpperCase(number.charAt(i))));
    }else{
        phone.append(number.charAt(i));
    }
}

System.out.println("Your number is " + phone.toString());
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thank you for your feedback. I'm not sure if I'm allowed to use StringBuilder because it was not taught to us nor was in mentioned in the text. But I can attempt to find something from what you've written as it is a good solution it's just that I might not be advanced enough to be allowed that command yet in my assignments hence why I'm asking for help. I have used the concatenate operators before however, and I didn't even think about using that which is an oversight on my part. Thank you again! – Shelly Feb 09 '19 at 23:21
  • I just wanted to get back to you about the code. I concatenated the code to the string as suggested and it worked perfectly inside the loop! I didn't realize that I needed to do the same for the 'else' portion of the code as well. I had a few more test runs and it's golden! Thank you for the help! My program runs perfectly using += and without terms that I have yet to learn (I only started a month ago coding - so newbie here). – Shelly Feb 10 '19 at 02:51