Java Comparing char values with set char values in a while loop - java

I need to compare char values with set char values 'g' 'c' 'a' 't'(lower and upper case), for i want only those values to be entered. I can not seem to get certain cases of my input validation working.
f in the below strings can stand for any length of string that is not characters g,c,a,t.
The string "fffffff" keeps in the loop.
The string "fgf" keeps in the loop.
However, i want the strings, "fffffg" or "gfg" to exit the loop, and they are not doing so.
The actual purpose of the exercise, to take a user input of nucleotides like g,c,a,t like the one's in DNA, and convert them into the complementary string of RNA. G is complement to C and vice versa. A is complement to U(the T is replaced with U) and vice versa.
So if the string is "gcat", the response for RNA should be "cgua".
import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;
//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C A T, for DNA and give
//the complementary RNA strand, C G U A.
public class practiceSixty {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String input = null;
boolean loopControl = true;
char nucleotide;
while(loopControl == true)
{
input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(!(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' ))
{
loopControl = true;
}
else if(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' )
{
loopControl = false;
System.out.println(nucleotide);
}
}
}
JOptionPane.showMessageDialog(null, "the data you entered is " + input);
StringBuilder dna = new StringBuilder(input);
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(nucleotide == 'G' || nucleotide == 'g' )
{
dna.setCharAt(i, 'c');
}
else if( nucleotide == 'C' || nucleotide == 'c')
{
dna.setCharAt(i, 'g');
}
if(nucleotide == 'A' || nucleotide == 'a')
{
dna.setCharAt(i, 'u');
}
else if(nucleotide == 'T' || nucleotide == 't')
{
dna.setCharAt(i, 'a');
}
}
JOptionPane.showMessageDialog(null, "the DNA is , " + input + " the RNA is " + dna);
}
});
}
}

You could do your check with a single regular expression, and then just use a do/while loop to keep prompting for input until the user enters something valid.
do {
input = JOptionPane.showInputDialog(
null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
} while (!input.matches("[GCATgcat]+"));
The regular expression will match any input that consists of one or more letters of the 8 shown. When you don't get a match, the loop repeats.

Related

How do I print out the list of vowels, numerics etc?

This program takes in a statement and then print the number of vowels, numerics, etc. I wanted to know how I could print the list of numerics and vowels which have been given as input.
Can someone tell me how this could be done using an array?
package strings;
import java.util.Scanner;
public class Strings
{
public static void main(String [] abc)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a statement:");
String statement = sc.nextLine().toLowerCase();
System.out.println("------------------------------------------------");
System.out.println("Total characters: " + statement.length());
int vowels = 0,num = 0,spaces = 0,spl = 0,others = 0;
char alpha;
for (int i = 0;i < statement.length();i++)
{
alpha = statement.charAt(i);
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u')
{
vowels++;
}
else if (alpha == ' ')
{
spaces++;
}
else if (alpha == '0' || alpha == '1' || alpha == '2' || alpha == '3' || alpha == '4' || alpha == '5' || alpha == '6' || alpha == '7' || alpha == '8' || alpha == '9')
{
num++;
}
else if (alpha =='!' || alpha =='#' || alpha =='#' || alpha =='$' || alpha =='%' || alpha =='^' || alpha =='&' || alpha =='*' || alpha =='(' || alpha ==')')
{
spl++;
}
else
{
others++;
}
}
System.out.println("Total vowels: " + vowels);
System.out.println("Total spaces: " + spaces);
System.out.println("Total numerics: " + num);
System.out.println("Total special characters: " + spl);
System.out.println("Other characters: " + others);
System.out.println("");
for (int i = 0;i < statement.length();i++)
{
System.out.println("The vowels are as follows:");
System.out.println(alpha);
}
}
}
You can use java.util.ArrayList, for example, create an ArrayList for vowels:
List<Character> vowelList = new ArrayList<>();
...
for (int i = 0; i < statement.length(); i++) {
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u') {
vowels++;
vowelList.add(alpha);
} else {
...
}
System.out.println("The vowels are as follows:");
System.out.println(vowelList);
If in case you don't want to use any external packages.
....
Character v[] = new Character[statement.length()];
....
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u'){
v[vowels++] = alpha;
}
....
System.out.println("The vowels are as follows:");
for (Character c : v) {
if (c != null) {
System.out.println(c);
}
}

Trying to add a character before each vowel in a String

I want to insert an "ab" before every vowel in a word
Example, if the user enters the word: fire
it has to be changed to: fabirabe
but my code is only entering the ab before the word like: abfire. How can I fix that?
Here's my code so far:
import java.util.Scanner;
public class Foothill
{
// class variables shared by more than one method
String prompt;
static String strUserResponse;
// main method
public static void main (String[] args)
{
giveInstructions();
getUserInput();
convertToTurkeyIrish();
vowelCounter();
}
public static String convertToTurkeyIrish()
{
String turkeyIrish = strUserResponse;
String turkeyIrish2;
turkeyIrish2 = "ab" + strUserResponse.replaceAll("(aeiouAEIOU)", "$1ab");
System.out.println("Word In Turkey Irish: " + turkeyIrish2);
return turkeyIrish;
}
public static void vowelCounter()
{
int vowel = 0;
strUserResponse.length();
char vowels;
vowels = ' ';
for (int j = 0; j <= strUserResponse.length() - 1 ; j++)
{
vowels = strUserResponse.charAt(j);
if ((vowels == 'a') || (vowels == 'A') || (vowels == 'e') || (vowel == 'E') || (vowel == 'i')|| (vowels == 'I') || (vowels == 'o') (vowels == 'O') || (vowel == 'u') || (vowels == 'u'))
{
System.out.println("Vowels in " + strUserResponse + ": " + vowel++);
}
}
}
}
Your regex is wrong, and so is the replace string. Try this:
strUserResponse.replaceAll("([aeiouAEIOU])", "ab$1");
Change your regex to:
(?i)(a|e|i|o|u)
and the replacement to:
ab$1
Your current regex is aeiouAEIOU which matches a sequence of characters: "aeiouAEIOU".
Test it:
System.out.println("fire".replaceAll("(?i)(a|e|i|o|u)", "ab$1"));
// fabirabe
Or as suggested by #amit, you can simply use a character class and write [aeiou]. Note that I used the (?i) to indicate that the regex should be case insensitive.

Java: How do I return user to main menu if user input is not a single alphabetic lowercase letter?

I'm trying to run a program that will allow the user to input both a char keyCharacter and a String theString. Then, using these inputs, I will mask the keyCharacter if it occurs in theString with a "$", remove the keyCharacter from the theString, and finally, count the number of times the keyCharacter occurs in theString altogether.
Every method is working fine, except the method getKeyCharacter where the user has to input a char:
The user can only enter a single letter (e.g. q, or z).
If the user enters anything other than that single letter (which can be anything from a word, phrase, sentence, special character like # or $, blank space or tabs, or just pressing enter), then the program returns the user to the original question that asks for the keyCharacter from the user. This should continue looping back to that original question until the user enters a valid input.
Since I'm still a beginner to java and loops are my weakness so far, this part is causing me a lot of trouble. I know I should be using a while loop, it is the logic behind the nested loops that is really confusing me.
From searching for possible solutions, I know there are these things called regex and try-catch exception that could help with my issue, but since we haven't gone over that explicitly in class, I'd prefer not to deal with that for now. Thank you.
Here's a paste of my code:
import java.util.*;
public class Foothill {
// main method
public static void main (String[] args) {
char keyCharacter = getKeyCharacter();
String theString = getString();
maskCharacter(theString, keyCharacter);
countKey(theString, keyCharacter);
removeCharacter(theString, keyCharacter);
}
// get keyCharacter
public static char getKeyCharacter() {
Scanner inputStream = new Scanner(System.in);
boolean stop = false;
String firstPrompt, strKeyCharacter;
char keyCharacter = ' ';
while (stop != true) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();
while (strKeyCharacter.length() != 1) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();
}
keyCharacter = strKeyCharacter.charAt(0);
while (strKeyCharacter.length() == 1) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();
if (keyCharacter == 'a' || keyCharacter == 'b' || keyCharacter == 'c' || keyCharacter == 'd'
|| keyCharacter == 'e' || keyCharacter == 'f' || keyCharacter == 'g' || keyCharacter == 'h'
|| keyCharacter == 'i' || keyCharacter == 'j' || keyCharacter == 'k' || keyCharacter == 'l'
|| keyCharacter == 'm' || keyCharacter == 'n' || keyCharacter == 'o' || keyCharacter == 'p'
|| keyCharacter == 'q' || keyCharacter == 'r' || keyCharacter == 's' || keyCharacter == 't'
|| keyCharacter == 'u' || keyCharacter == 'v' || keyCharacter == 'w' || keyCharacter == 'x'
|| keyCharacter == 'y' || keyCharacter == 'z') {
System.out.println("You entered: " + keyCharacter + '\n');
stop = true;
} else {
break;
}
}
}
return keyCharacter;
}
// declare final = 4 to be constant
public static final int minimumLength = 4;
// get theString
public static String getString() {
Scanner inputStream = new Scanner(System.in);
String secondPrompt, theString;
do {
secondPrompt = "Please enter a phrase or sentence >= 4: ";
System.out.print(secondPrompt);
theString = inputStream.nextLine();
System.out.print('\n');
} while (theString.length() < minimumLength || theString == null || theString.length() == 0);
inputStream.close();
return theString;
}
// mask keyCharacter with $
public static String maskCharacter(String theString, char keyCharacter) {
theString = theString.replace(keyCharacter, '$');
System.out.println("String with " + " '" + keyCharacter + "' " + " masked.");
System.out.println(theString + '\n');
return theString;
}
// count number of times keyCharacter occurs in theString
public static void countKey(String theString, char keyCharacter) {
int countChar = 0;
for (int charTimes = 0; charTimes < theString.length(); charTimes++) {
if (theString.charAt(charTimes) == keyCharacter) {
countChar++;
}
}
System.out.println( "The key character occurs " + countChar + " times. \n");
return;
}
// remove keyCharacter from theString
public static void removeCharacter(String theString, char keyCharacter) {
theString = theString.replace(String.valueOf(keyCharacter), "");
System.out.println("String with " + "'" + keyCharacter + "' removed: ");
System.out.println(theString);
return;
}
}
And here's a paste of my run (as you can see, there is some serious debugging to be done in my program):
Please enter a SINGLE character to act as key: f
Please enter a SINGLE character to act as key: f
You entered: f
Please enter a SINGLE character to act as key: f
You entered: f
Please enter a SINGLE character to act as key: f
You entered: f
Please enter a SINGLE character to act as key: f
You entered: f
Please enter a SINGLE character to act as key:
// which then continues so on so forth...
public static char getKeyCharacter(){
Scanner inputStream = new Scanner(System.in);
boolean stop = false;
String firstPrompt, strKeyCharacter;
char keyCharacter = ' ';
while(!stop){
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.println(firstPrompt);
strKeyCharacter = inputStream.nextLine();
//check if the input contains only 1 character
boolean isSingleChar = (strKeyCharacter.length() == 1);
//check if the input character is within the ASCII code of 97 (a) to 122 (z)
boolean isValidChar =
strKeyCharacter.charAt(0) >= 97 &&
strKeyCharacter.charAt(0) <= 122;
if(isSingleChar && isValidChar){
keyCharacter = strKeyCharacter.charAt(0);
stop = true;
}
}
return keyCharacter;
}

How can I check how many consonants and vowels there are in a sentence in Java?

At the end of my loop, I am planning on displaying the number of consonants and vowels in the sentence. I was wondering if there was a more efficient way to check how many consonants and vowels are in a given sentence, rather than using an if statement and manually inputting every letter. (key refers to my Scanner which has already been initialized)
Edit: It needs to ignore digits and other special characters, so for example if I write Hello# how 1are you?. There should be 8 vowels and 6 consonants.
System.out.println("Please enter the sentence to analyze: ");
String words = key.nextLine(); //the sentence the user inputs
int c = 0; //# of consonants
int v = 0; //# of vowels
int length = words.length(); //length of sentence
int check; //goes over each letter in our sentence
for(check = 0; check < length; check++){
char a = words.charAt(check);
if(a == 'a' || a == 'A' || a == 'e' || a == 'E' || a == 'i' || a == 'I' || a == 'o'
|| a == 'O' || a == 'u' || a == 'U' || a == 'y' || a == 'Y')
v = v + 1;
else if(a == 'b' || a == 'B' || a == 'c' || a == 'C' || a == 'd' || a == 'D' || a == 'f'
|| a == 'F' || a == 'g' || a == 'G' || a == 'h' || a == 'H' || a == 'j' || a == 'J'
|| a == 'k' || a == 'K' || a == 'l' || a == 'L' || a == 'm' || a == 'M' || a == 'n'
|| a == 'N' || a == 'p' || a == 'P' || a == 'q' || a == 'Q' || a == 'r' || a == 'r'
|| a == 's' || a == 'S' || a == 't' || a == 'T' || a == 'v' || a == 'V' || a == 'w'
|| a == 'W' || a == 'x' || a == 'X' || a == 'z' || a == 'Z')
c = c + 1;
}
Use Character.isLetter(ch) to determine if the character is a vowel or a consonant, then check to see if the character in question is in the set of vowels.
One way to create the set of vowels:
Set<Character> vowels = new HashSet<Character>();
for (char ch : "aeiou".toCharArray()) {
vowels.add(ch);
}
And to increment v or c:
if (Character.isLetter(a)) {
if (vowels.contains(Character.toLowerCase(a))) {
v++;
} else {
c++;
}
}
Assuming you already have a letter (vowel or consonant, not a digit nor a symbol or anything else), then you can easily create a method to define if the letter is a vowel:
static final char[] vowels = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y' };
public static boolean isVowel(char c) {
for (char vowel : vowels) {
if (c == vowel) {
return true;
}
}
return false;
}
public static boolean isConsonant(char c) {
return !isVowel(c);
}
Note that I set Y and y as vowels since seems that they are in your language. In Spanish and English, Y is a consonant (AFAIK).
You can easily check if the char is a letter or not using Character#isLetter.
So, your code would become into:
for(check = 0; check < length; check++){
char a = words.charAt(check);
if (Character.isLetter(a)) {
if (isVowel(a)) {
v++;
} else {
c++;
}
}
}
How about something like
String vowels = "aeiouyAEIOUY"; // you can declare it somewhere before loop to
// to avoid redeclaring it each time in loop
//inside loop
if ((a>='a' && a<='z') || (a>='A' && a<='Z')){ //is letter
if (vowels.indexOf(a)!=-1) //is vowel
v++;
else //is consonant
c++;
}
I am sure this can be improved upon, but I'll throw it in the ring anyways.
Remove non-characters from the sentence, lowercase it, then convert to a char array and compare it to a char array of vowels that are all lowercase.
String myText = "This is a sentence.";
int v = 0;
char[] vowels = {'a','e','i','o','u'};
char[] sentence = myText.replaceAll("[^a-zA-Z]","").toLowerCase().toCharArray();
for (char letter : sentence) {
for (char vowel : vowels) {
if (letter == vowel) {
v++;
}
}
}
System.out.println("Vowels:"+ v);
System.out.println("Consonants:" + (sentence.length -v));
One easy way would be to create 2 lists:
one contains vowels (a, e, i, o, u)
the other contains consonants
Then you iterate over each character in the Java string.
See a sample below:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Counter {
public static void main(String[] args) {
String test = "the fox is in the woods";
test = test.toLowerCase();
List<Character> vowels = new ArrayList<Character>();
vowels.addAll(Arrays.asList(new Character[]{'a', 'e', 'i', 'o', 'u'}));
List<Character> consonants = new ArrayList<Character>();
consonants.addAll(Arrays.asList(new Character[]{'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'}));
int vcount = 0;
int ccount = 0;
for (int i = 0; i < test.length(); i++){
Character letter = test.charAt(i);
if (vowels.contains(letter)){
vcount ++;
} else if (consonants.contains(letter)){
ccount++;
}
}
System.out.println(vcount);
System.out.println(ccount);
}
}
You can do a range check to make sure it is a letter, then check if it one of the vowels:
if( ( a >= 'a' && a<= 'z' ) || ( a >= 'A' && a <= 'Z' ) )
{
// is letter
switch( a )
{
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'U': case 'u':
++v;
break;
default: // don't list the rest of the characters since we did the check in the if statement above.
++c;
}
}
Oh, there's certainly a much more readable way to do it. Not sure if that meets the "better" definition.
As a start, I'd suggest that you encapsulate what you have into methods that you can write once and call anywhere:
package misc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* ParseUtils get counts of vowels and consonants in sentence
* #author Michael
* #link https://stackoverflow.com/questions/24048907/how-can-i-check-how-many-consonants-and-vowels-there-are-in-a-sentence-in-java
* #since 6/4/2014 6:57 PM
*/
public class ParseUtils {
private static final String VOWEL_PATTERN_STR = "(?i)[aeiou]";
private static final Pattern VOWEL_PATTERN = Pattern.compile(VOWEL_PATTERN_STR);
private static final String CONSONANT_PATTERN_STR = "(?i)[b-df-hj-np-tv-z]";
private static final Pattern CONSONANT_PATTERN = Pattern.compile(CONSONANT_PATTERN_STR);
private ParseUtils() {}
public static void main(String[] args) {
for (String arg : args) {
System.out.println(String.format("sentence: '%s' # letters: %d # vowels: %d # consonants %d", arg, arg.length(), getNumVowels(arg), getNumConsonants(arg)));
}
}
public static int getNumVowels(String sentence) {
return getMatchCount(sentence, VOWEL_PATTERN);
}
public static int getNumConsonants(String sentence) {
return getMatchCount(sentence, CONSONANT_PATTERN);
}
private static int getMatchCount(String s, Pattern p) {
int numMatches = 0;
if ((p != null) && (s != null) && (s.trim().length() > 0)) {
Matcher m = p.matcher(s);
while (m.find()) {
++numMatches;
}
}
return numMatches;
}
}
Split the String by whitespaces and and Calculate only the number of Vowels. Then Number of consonants = Length of Sentence - No. of Vowels.
Detailed Code:
System.out.println("Please enter the sentence to analyze: ");
int v = 0;
int c = 0;
String string = key.nextLine(); //the sentence the user inputs
String[] stringArray = string.split(" ");
for(int i=0;i<stringArray.length;i++)
{
for(int j= 0; j<string.length(); j++)
{
char a = string.charAt(j);
if(a == 'a' || a == 'A' || a == 'e' || a == 'E' || a == 'i' || a == 'I' || a == 'o'
|| a == 'O' || a == 'u' || a == 'U' || a == 'y' || a == 'Y')
v = v + 1;
}
c= c+(stringArray.length)-v;
}
System.out.println("Vowels:"+v+" and Consonants:"+c);
One way to do it is to get rid of the non-letters, then vowels and consonants, and get the length of what is left:
public class CountChars {
public static final String CONSONANTS = "[BCDFGHJKLMNPQRSTVWXYZ]";
public static final String VOWELS = "[AEIOU]"; // considering Y a consonant here
public static final String NOT_LETTERS = "[\\W_0-9]";
public static void main(String[] args) {
String words = "How can I check how many consonants and vowels there are in a sentence in Java?";
String letters = words.toUpperCase().replaceAll(NOT_LETTERS, "");
System.out.println("Letters: " + letters.length());
String vowels = letters.replaceAll(CONSONANTS, "");
System.out.println("Vowels: " + vowels.length());
String consonants = letters.replaceAll(VOWELS, "");
System.out.println("Consonants: " + consonants.length());
}
}
Here is the best way of doing this:
public static void checkVowelsAndConsonants(String s){
System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
//Also eliminating spaces, if any for the consonant count
System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}

Convert Uppercase Letter Input to a specific Number

I'm super new to programming so I would love to keep this simple. The compiler accepts my code, but when I run the program and type in for example the letter A I just get a ton of errors. I tried earlier using String letter instead of int letter, but I just got compiler errors stating I couldn't convert Strings to characters or something. I'm really confused and could use a quick explanation and fix so I can get a number back. Here's my code:
import java.util.Scanner;
import java.lang.String;
public class PhoneAlgorithm {
public static void main(String[] args){
int digit = -1;
Scanner in;
in = new Scanner(System.in);
System.out.print("Enter an uppercase letter to find out the corresponding digit on a telephone: ");
int letter;
letter = Integer.parseInt(in.next());
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = 2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = 3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = 4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = 5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = 6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = 7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = 8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = 9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
if (digit != -1) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}
When you use parseInt(str), you will get an Exception if the parameter str cannot be converted to an integer.
You must use char, since you are comparing the input with single characters:
char letter;
letter = in.nextLine().charAt(0);
str.charAt(index) Returns the char value at the specified index.
I have modified your code, I guess this is what you are looking for..
import java.util.Scanner;
public class Try {
public static void main(String[] args) {
//declarations
char letter;
int digit=0;
// Asking the user to enterstring
System.out.println("Enter the string");
String enterString;
//creating a scanner object and reading the string
Scanner input = new Scanner(System.in);
enterString= input.next();
System.out.println("Entered string is "+enterString);
int temp=0;
for(int i=0;i<enterString.length();i++){
letter=(char)enterString.codePointAt(i);
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = digit*10+2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = digit*10+3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = digit*10+4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = digit*10+5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = digit*10+6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = digit*10+7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = digit*10+8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = digit*10+9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
/*if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}*/
}
if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}

Categories

Resources