Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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

loop and save letters in a string java

    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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: math.pow(x2-x1,2) 
Java :: join arrays in java 
Java :: recursion java fibonacci 
Java :: spring login response data 
Java :: method 
Java :: spring boot prerequisites 
Java :: java replaceall single character 
Java :: generics Interface in java 
Java :: Find Length of String using length() 
Java :: jre in java 
Java :: keycloak spring boot application.properties 
Java :: playwright java 
Java :: String to java.sql.Date 
Java :: java loop find index 
Java :: multiple inputs java 
Java :: search and delete class files from jars 
Java :: HOW TO MAKE ENUM START WITH ONE 
Java :: signo zodiaco jaa 
Java :: jbutton default color 
Java :: full screen in libgdx Lwjgl3 
Java :: layout focus from recycleview not from top in fragment inside nestedScrollview in android xml 
Java :: Java Catching base exception class only 
Java :: Person[] people = in java 
Java :: java equals on recursive apache commons 
Java :: java statements 
Java :: Java Standard Library Method 
Java :: sort a list according to location 
Java :: byte array to zip java 
Java :: Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "EXCEPTION": invalid identifier 
Java :: java to run colored lines 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =