loop and save letters in a string java
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;
}
}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());
Also in Java:
- Title
- java list as parameter
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- dicom read with java
- Category
- Java
- Title
- java integer to binary string
- Category
- Java
- Title
- what is graphics default color java
- Category
- Java
- Title
- remove duplicates from singly linked list
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- get file path java
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- 2d array java
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- how to create an abstract class in java
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- processing sound library
- Category
- Java
- Title
- import
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- store string elements in character array in java
- Category
- Java
- Title
- The specified child already has a parent. You must call removeView() on the child's parent first
- Category
- Java
- Title
- rgb to hex java
- Category
- Java
- Title
- get method of a class which I only have string to
- Category
- Java
- Title
- java hashmap set value
- Category
- Java
- Title
- how to break two loop in java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- cordova cannot find symbol import com.google.firebase.iid.FirebaseMessagingService;
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- cosinus-1 java
- Category
- Java
- Title
- getordefault java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- android studio keep device awake
- Category
- Java
- Title
- add in list java
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- java public private key encryption
- Category
- Java
- Title
- executors java
- Category
- Java
- Title
- string reverse in java
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- java singleton implementation
- Category
- Java
- Title
- java insert array
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- java program to find prime number between 1 and 100
- Category
- Java
- Title
- java file path linux
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- validate request body java
- Category
- Java
- Title
- delete object java
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- équivalent setTimeInterval java
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- how to print to console in java
- Category
- Java
- Title
- integer to string java
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- log10 in java
- Category
- Java
- Title
- java connect to mysql
- Category
- Java
- Title
- how to count number of words in a string
- Category
- Java
- Title
- how to compare strings java
- Category
- Java
- Title
- how to find powers in java
- Category
- Java
- Title
- taking string input in java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- how to change maven java version in po,
- Category
- Java
- Title
- system.in example in java
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java
- Title
- java print syntax
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- iterate through hashmap java
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- contains example in java
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- Java copy file
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- check leap year in java
- Category
- Java