loop and save letters in a string java

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());

3.7
10
Dominika 85 points

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

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

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


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

3.7 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
to measurements in a string for loop java while loop to loop through string length of string whith loop inv java string method to loop and and find a word java loop a string in java loop as you get data between characters java string name loop java loop string names java while loop that prints the word depending on its length java while loop using a string and int java while loop using a string and in for loop variation for string loop through characters of string java java for loop string java loop through characters in string different string for loop how to find first character in for loop variable how do take the first character of foor loop variable for loop string java string loop how to run a for loop long as string for loop for string java java get a string out of a for loop for loop for string in java how to print string using for loop in java how to add a str to a str valuye after every loop java print string letter by letter for loop java print string value in for loop for loop that searches through a string java loop for string java loop in java for sting using different string in for loop java how to make for loop for string type for loop in string java for loop in java string hot to create by looping the same string with increments the numbers java how to create a string using for loop in java how to add loop output in between a string java contains inside loop but other numbers for loop for string using uppercase characters over 10 lines in java how to make a loop go for the length of a variable java how to loopo a string why is my i in the for loop a string can you use i loops for strings string letter loop int to string loop java for loop from string java java loop string how to store a series of charecters with a for loop java loop string java java loop in a integer character by character loops using String java string value in a while condition java how to apply for loop on String in java print the string character in loop java java name string after for loop index string loop in java how to end while loop string how to make a while statement with an int and string how to make a for loop start at the end of a string java how to make a loop that adds a number after everycharacter java looping character postition in string java how to make afor loop to check if a variable equals a string in a list java s.length in for loop java how to make loop that adds letters loops and strings java method to remove all strings less then 5 characters in legnth java with a while loop how to loop string in java How to grab a string using a for loop java for loop with contains string loop through a string until it reaches a whitespace java how to create a loop that iterates through each string in the array and adds the user's text input to the end of each strin for loop character start loop at end of string java how can you get 1 more character on each line in a for loop in java make a while loop that takes the rightmost character and makes it another one for loop string java get a string of the number loops in java for loop in java to loop a letter 10 times how to loop a single letter in java 10 times java char for loops for loop string length count java how to make string a loop how i can increment for loop for string how to create a loop that iterates through letters for loop to run through string for loop within string indef of char in for of loop iterste a string in java how to take multiple string input and save in a dictionary in python split string and store in dictionary python how to split and put elements in dictionary python how read faster int inputs in java php make all the strings in an array uppercase java add all char in a string and print letters in a string for python take a string input in java how to take input of a 2d array in java using scaner class take textbox values and put them in string array and display them in listbox c# java string finish with LF (\n) how to slice a string using a loop in python print all substrings of a string using recursion forlopp text in java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source