Difference between revisions of "Cellphone Texting"
Line 127: | Line 127: | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | String | + | public static String codeEncription(String input) |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | + | String output = ""; //Store each number that is translated into letter | |
+ | int counter = 1; //count the number of same consecutive number | ||
+ | char last = input.charAt(0); //Initialize the last to the first character of the string | ||
+ | char cur = input.charAt(0); //Initialize the cur to the first character of the string | ||
+ | for(int i = 1 ; i <= input.length();i++) | ||
{ | { | ||
− | cur = input.charAt(i); //store the current character to cur | + | if(i < input.length()) |
− | + | { | |
− | + | cur = input.charAt(i); //store the current character to cur | |
− | + | } | |
− | + | else | |
− | + | { | |
+ | cur = '.'; //this else statement is necessary to complete the last letter of the input | ||
+ | } | ||
− | + | if(cur == last) | |
− | + | { | |
− | + | counter++; //number of same consecutive number | |
− | + | } | |
− | + | else | |
− | + | { | |
− | + | output += converter(last,counter) +""; //this will call converter() method that will change the number into letter | |
− | + | last = cur; //to set the last into the current character | |
− | + | counter = 1; | |
+ | } | ||
} | } | ||
+ | return output; // return the translated code | ||
} | } | ||
− | |||
− | |||
}} | }} | ||
− | |||
− | |||
− | + | Last step is to make a method that will convert the number into letter using String array and Modulus.<br> | |
+ | In this method, there are two parameter ,char and int, the char represent the number and the in represent how many consecutive number was found in the current search from the other method. This will return the equivalent letter or if its space it will return empty String. | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | + | public static String converter(char find,int count) | |
− | + | { | |
− | }} | + | //String array are manually entered. The arrangement of letters is necessary in order to get the right letter |
− | {{ | + | String[] two = {"C","A","B"}; |
− | + | String[] three = {"F","E","D"}; | |
− | + | String[] four = {"I","G","H"}; | |
− | + | String[] five = {"L","J","K"}; | |
− | + | String[] six = {"O","M","N"}; | |
+ | String[] seven = {"S","P","Q","R"}; | ||
+ | String[] eight = {"V","T","U"}; | ||
+ | String[] nine = {"Z","W","X","Y"}; | ||
+ | String convert = ""; | ||
+ | |||
+ | //doing a modulus will return either 1,2 or 0 if its mod 3 and 1,2,3 or 0 if its mod 4 | ||
+ | if(find == '2'){ | ||
+ | convert = two[(count%3)]; | ||
+ | }else if(find == '3') | ||
+ | { | ||
+ | convert = three[(count%3)]; | ||
+ | }else if(find == '4') | ||
+ | { | ||
+ | convert = four[(count%3)]; | ||
+ | }else if(find == '5') | ||
+ | { | ||
+ | convert = five[(count%3)]; | ||
+ | }else if(find == '6') | ||
+ | { | ||
+ | convert = six[(count%3)]; | ||
+ | }else if(find == '7') | ||
+ | { | ||
+ | convert = seven[(count%4)]; | ||
+ | }else if(find == '8') | ||
+ | { | ||
+ | convert = eight[(count%3)]; | ||
+ | }else if(find == '9') | ||
+ | { | ||
+ | convert = nine[(count%4)]; | ||
+ | } | ||
+ | |||
+ | return convert; | ||
+ | } | ||
}} | }} | ||
Revision as of 05:01, 5 December 2011
Back to the Program-A-Day homepage
ProblemIn non-smart cell phones, text input can be handled by a
technique called multi-tap(example picture: link here). With multi-tap, you use the
letters on each key, and press a key multiple times to
access different letters. For instance, to get the letter 'a',
you press the 2 key once, but to get 'n', you press the 6
key twice (the first press gives you 'm', the second
switches 'm' to 'n'). A slight pause by the user will indicate
the end of inputting one character and a beginning of the
input of the next character.
The letters 'q' and 'z', not originally part of the letters on a
phone, force the keys 7 and 9 to have four characters each. All other keys have 3
characters.
Enter code: 222666 67777222444 Translated Code: COMSCI Enter code: 7 2 777 2 7777 444 8 444 222 Translated Code: PARASITIC
|
By Students...COMP 1010 is where I first learned how to make a program. I was only taking that course for prerequisite and do not have interest on it, but as time goes by, I notice that I was enjoying myself making a program.
| |
---|---|---|
SolutionStart by importing the swing java package. import javax.swing.*; import java.util.Scanner;
Scanner scan = new Scanner(System.in); // To enter the input String input = " "; // Store the input String convert = ""; // Store the translated input System.out.print("Enter code: "); while(!input.equals("")) { input = scan.nextLine(); // scan will get the user input and store in in the String input if(checkInput(input)) // calls the method checkInput to check for validation { convert = codeEncription(input); //calls the method codeEcription to translate the input into letters System.out.print("Translated Code: "+convert); //output message System.out.print("\nEnter code: "); } else { System.out.println("Invalid Input"); //error message System.out.print("Enter code: "); } } Enter code: 23a43 Invalid Input Enter code: 77 88 444 9999 9999 444 222 2 555 Translated Code: QUIZZICAL
public static boolean checkInput(String input) { boolean check = true; int counter = 0; while( check && counter < input.length()) { if(input.charAt(counter) == ' ') //if space is found in the input its still valid counter++; else if(input.charAt(counter) < '0' || input.charAt(counter) > '9') //check if its a number or not check = false; else counter++; } return check; //returns true if valid or false if not valid }
public static String codeEncription(String input) { String output = ""; //Store each number that is translated into letter int counter = 1; //count the number of same consecutive number char last = input.charAt(0); //Initialize the last to the first character of the string char cur = input.charAt(0); //Initialize the cur to the first character of the string for(int i = 1 ; i <= input.length();i++) { if(i < input.length()) { cur = input.charAt(i); //store the current character to cur } else { cur = '.'; //this else statement is necessary to complete the last letter of the input } if(cur == last) { counter++; //number of same consecutive number } else { output += converter(last,counter) +""; //this will call converter() method that will change the number into letter last = cur; //to set the last into the current character counter = 1; } } return output; // return the translated code }
public static String converter(char find,int count) { //String array are manually entered. The arrangement of letters is necessary in order to get the right letter String[] two = {"C","A","B"}; String[] three = {"F","E","D"}; String[] four = {"I","G","H"}; String[] five = {"L","J","K"}; String[] six = {"O","M","N"}; String[] seven = {"S","P","Q","R"}; String[] eight = {"V","T","U"}; String[] nine = {"Z","W","X","Y"}; String convert = ""; //doing a modulus will return either 1,2 or 0 if its mod 3 and 1,2,3 or 0 if its mod 4 if(find == '2'){ convert = two[(count%3)]; }else if(find == '3') { convert = three[(count%3)]; }else if(find == '4') { convert = four[(count%3)]; }else if(find == '5') { convert = five[(count%3)]; }else if(find == '6') { convert = six[(count%3)]; }else if(find == '7') { convert = seven[(count%4)]; }else if(find == '8') { convert = eight[(count%3)]; }else if(find == '9') { convert = nine[(count%4)]; } return convert; }
else if (monthNum <= 0) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: -1 Please enter the Year: 2009 Please try again, there was an error with the month you have entered
else if (year <= 999) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: 12 Please enter the Year: 987 Please try again, there was an error with the month you have entered
The last case, which is most important since it is processing the valid input will be the next step. In this case there is a set of nested cases. Again, you will have to separate the conditions into statements. In this problem you separated the months by 31 days, 30 days, and February as a case of its own for calculating leap years. Months with 31 days, since you are assuming Jan = 0, the condition will "or" all integer representations of the months with 31 days. Remember July and August are back to back months with 31 days! The first condition checks for the appropriate monthNum and the nested conditions will assign the appropriate monthName depending on which integer was chosen. There may be alternate solutions and methods to solve this problem but for the sake of example and topics to cover you have chosen this method. Lastly, System.out.println prints an meaningful output message. //JAN //MAR //MAY //JUL //AUG //OCT //DEC if (monthNum == 1 || monthNum == 3 || monthNum == 5 || monthNum == 7 || monthNum == 8 || monthNum == 10 || monthNum == 12) { if (monthNum == 1) monthName = "January"; else if (monthNum == 3) monthName = "March"; else if (monthNum == 5) monthName = "May"; else if (monthNum == 7) monthName = "July"; else if (monthNum == 8) monthName = "August"; else if (monthNum == 10) monthName = "October"; else if (monthNum == 12) monthName = "December"; System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 3 Please enter the Year: 2009 March 2009 has 31 days.
//FEB else if (monthNum == 2) { monthName = "February"; //check for leap if (year % 4 == 0) System.out.println(monthName + " " + year + " has " + (FEB + 1) + " days."); else System.out.println(monthName + " " + year + " has " + FEB + " days."); } Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2008 February 2009 has 29 days. Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2009 February 2009 has 28 days.
//APR //JUN //SEP //NOV else if (monthNum == 4|| monthNum == 6 || monthNum == 9 || monthNum == 11) { if (monthNum == 4) monthName = "April"; else if (monthNum == 6) monthName = "June"; else if (monthNum == 9) monthName = "September"; else if (monthNum == 11) monthName = "November"; System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 11 Please enter the Year: 2009 November 2009 has 30 days.
Lastly, for additional robustness, you can check if the user enters a monthNum that is too high. Remember, initially you checked for negative monthNum, but this time is now the opposite end of the spectrum where the monthNum is too beyond the scope. If the monthNum is too high output a meaningful error message. //number is larger than 11 else System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); Please enter the month [Jan(1) to Dec(12)]: 13 Please enter the Year: 2009 Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)
| ||
Code |