How to convert characters from a string into numbers (phone number converted)? - java

I'm a bit stuck on how I should replace each element in char array to a number.
Here's the code I have so far:
public static void main(String []args){
String[] dialTwo = {"a", "b", "c"};
String[] dialThree = {"d", "e", "f"};
String[] dialFour = {"g", "h", "i"};
String[] dialFive = {"j", "k", "l"};
String[] dialSix = {"m", "n", "o"};
String[] dialSeven = {"p", "q", "r", "s"};
String[] dialEight = {"t", "u", "v"};
String[] dialNine = {"w", "x", "y", "z"};
Scanner in = new Scanner(System.in);
System.out.print("Enter a phone number: ");
String phoneInput = in.next();
char[] inputToArray = phoneInput.toCharArray();
while (!phoneInput.matches("^[a-pA-P0-9]*$")) {
System.out.println("Not a valid number. Try agian.");
phoneInput = in.next();
}
I was able to successfully verify the string in case someone wanted to enter ;;';';.
Thank you for your help guys.
My teacher also wants me to use method classes, but I'm slightly confused on it so I'm doing it a bit differently.
So the output I want, if someone were to input "CFG" it would print 123.

My solution would be a bit simpler.
First, I would not use those arrays but one 2D array like :
static char[][] keyboard = {
{'a','b','c'}, //2
{'d','e','f'}, //3
{'g','h','i'}, //4
{'j','k','l'}, //5
{'m','n','o'}, //6
{'p','q','r','s'}, //7
{'t','u','v'}, //8
{'w','x','y','z'} //9
};
Then, from this, I would loop on every character of the input you have. For each character, I would search on which array it is. The value you need is the index + 2. So using a simple for-loop on keyboard, you can find where is the character and print the value you want. There is exception for numeric, space and symbol of course.
for each character in input
if character is numeric
output ( toNumeric ( character ) )
else
index = 0
while character not found
if character in array[index]
output ( index + 2 )
index++
For more code, well, you need to give more information because you need to work a bit too ;)

You can use Collections instead of String[]. Probably map would be good. But, since you are using String[] following code should help:
for (int i = 0; i < inputToArray.length; i++) {
if (Arrays.asList(dialTwo).contains(inputToArray[i]))
inputToArray[i]=2;
...
}
You need to fill the ... part with other if else conditions where you check for inputToArray[i] with other arrays and replace accordingly.

A simple way to do that is to map a function to use map
inputToArray.stream().map(/*some function*/).toArray();
private void int /*your function*/(char c){...}
A little rusty on Java so can't claim the syntax is correct but basically map a function that takes a char and returns your desired int to your array. After that all you have to do is write the actual function you are mapping.
There's also a number of ways you could just parse the string returned by next as there doesn't seem to be any particular reason in your code for the conversion to a char array
It should also be mentioned that it is rather inefficient to have arrays of 1 length strings for no specific reason. You could easily use strings instead

public static void main(String[] args) {
String[] dialTwo = { "a", "b", "c" };
String[] dialThree = { "d", "e", "f" };
String[] dialFour = { "g", "h", "i" };
String[] dialFive = { "j", "k", "l" };
String[] dialSix = { "m", "n", "o" };
String[] dialSeven = { "p", "q", "r", "s" };
String[] dialEight = { "t", "u", "v" };
String[] dialNine = { "w", "x", "y", "z" };
Scanner in = new Scanner(System.in);
System.out.print("Enter a phone number: ");
String phoneInput = in.next();
char[] inputToArray = phoneInput.toCharArray();
int i = 0;
while (!phoneInput.matches("^[a-zA-Z0-9]*$")) { // Used to check if any
// special character is
// enter in phone number
System.out.println("Not a valid number. Try agian.");
phoneInput = in.next();
}
List<String> one = (List) Arrays.asList(dialTwo);
// for converting array into list so that we can use contains method
// which is not //available in array
List<String> two = (List) Arrays.asList(dialThree);
List<String> three = (List) Arrays.asList(dialFour);
List<String> four = (List) Arrays.asList(dialFive);
List<String> five = (List) Arrays.asList(dialSix);
List<String> six = (List) Arrays.asList(dialSeven);
List<String> seven = (List) Arrays.asList(dialEight);
List<String> eight = (List) Arrays.asList(dialNine);
while (i < inputToArray.length) {
if (inputToArray[i] >= 48 && inputToArray[i] <= 57) {
// for numeric characters
System.out.print(inputToArray[i]);
} else if (one.contains(String.valueOf(inputToArray[i]).toLowerCase()))
/*
* searches given character by converting it into lower case in case
* of capital letters
*/
{
System.out.print(1);
} else if (two.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(2);
} else if (three.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(3);
} else if (four.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(4);
} else if (five.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(5);
} else if (six.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(6);
} else if (seven.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(7);
} else if (eight.contains(String.valueOf(inputToArray[i]).toLowerCase())) {
System.out.print(8);
}
i++;// counter variable for counting number of chars entered
}
}

We can use Regular Expression(regex) here to find alphabets in the input and replace each with corresponding integer till entire value contains integers.
Add the following code:
/*Search and replace all alphabets till only numbers are left in the string*/
while(phoneInput.matches("^[a-zA-Z0-9]*$") && !phoneInput.matches("^[0-9]*$")){
/*
* Scenario 1:
* The regex used will search for one occurrence of alphabets a, b & c(both upper and lower case)
* and replace with "1".
* Same goes down for other values as well.
*/
phoneInput = phoneInput.replaceFirst("[a-cA-C]", "2");
phoneInput = phoneInput.replaceFirst("[d-fD-F]", "3");
phoneInput = phoneInput.replaceFirst("[g-iG-I]", "4");
phoneInput = phoneInput.replaceFirst("[j-lJ-L]", "5");
phoneInput = phoneInput.replaceFirst("[m-oM-O]", "6");
phoneInput = phoneInput.replaceFirst("[p-sP-S]", "7");
phoneInput = phoneInput.replaceFirst("[t-vT-V]", "8");
phoneInput = phoneInput.replaceFirst("[w-zW-Z]", "9");
}
System.out.println("The formatted phone number is: " + phoneInput);
This should serve the purpose.

Related

Comparing character and String

I am writing a program to check wether the given sentence is a panagram or not, but I am not able to compare each character with the characters in a string.
Can anyone suggest me a method to get the desired output?
Scanner scan = new Scanner(System.in);
String panagram = scan.nextLine();
String word = panagram.toLowerCase();
System.out.println(word);
String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z" };
int count = 1;
System.out.println("a=" + a.length);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < word.length(); j++) {
if ((a[i]).equals(word.charAt(j)))// problem occurs here {
count++;
break;
}
}
}
if (count == 26) {
System.out.println("pangram");
} else {
System.out.println("not pangram");
}
You can use:
(a[i]).equals(String.valueOf(word.charAt(j)))
or change your a array from String[] to char[] and compare the character using the == operator.
You can convert it to a String and then compare it.
String s = Character.toString('c');
if(s.equals(s2)){..}
Or you could make it a String by adding it to an empty string.
String s = 'c' + "";
if(s.equals(s2)){..}
Or you could compare it's ascii value.
As there are only single characters, why don't you use a character array.
char []a = {'a', 'b', 'c'....};
if(a[i] == 'a')//you could use == in this case.
{..}
Also you don't need to check it that way.
You could create a boolean array of 26 size, as there are only 26 characters and check if every character is present atleast once or not
boolean []arr = new boolean[26];
for(int i = 0; i < word.length(); ++i)
{
arr[word.charAt(i) - 'a'] = true;
}
isPan(arr);
public boolean isPan(boolean[] arr)
{
for(int i = 0; i < arr.length; ++i)
if(!arr[i])
return false;
return true;
}
A simple O(n) solution.
Or you could use a Set and check it's size.
HashSet<Character> set = new HashSet();
for(int i = 0; i < word.length(); ++i)
set.add(word.charAt(i));
System.out.println(set.size() == 26 ? "Pangram" : "Not Pangram");
//A 2 liner would be
HashSet<Character> set = new HashSet<Character>(Arrays.asList(word.toCharArray()));
System.out.println(set.size() == 26 ? "Pangram" : "Not Pangram");
A point added by spookieCookie. These approaches apply only when the string has only lower case alphabets.
String s = "sdgosdgoih3208ABDDu23pouqwpofjew##$%^&".repalceAll("[^A-Za-z]+", "");
String s = s.toLowerCase();
//do the computation then
Convert the array into an arraylist
List<Character> alphabet = Arrays.asList(a);
Make a list to hold the characters that are read:
Set<Character> chars = new HashSet<>();
Then check whether every character in the sentence is part of the alphabet. Duplicates are not added due to the characteristics of Set
for (Character c : word.toCharArray()) {
chars.add(c);
}
Then check whether the size of the Set is equal to the given alphabet:
return (chars.size() == alphabet.size());
Try this instead:
public void checkPanagram() {
boolean isPanagram = false;
String word = "The quick brown fox jumps over the lazy dog";
String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z" };
for (int i = 0; i < a.length; i++) {
if (word.toLowerCase().contains(a[i])) {
isPanagram = true;
} else {
isPanagram = false;
break;
}
}
if (isPanagram) System.out.println("Yes, panagram");
else System.out.println("No, not panagram");
}
You can use String.contains(String) as every letter in a already is a String.
for (int i = 0; i < a.length; i++) {
if (word.contains(a[i])) {
count++;
// } else { break;
}
}
A remark: better use String[] a which is a more regular syntax instead of String a[] whose Syntax was added for C/C++ compatibility.
To check equality of a string with a char you could change your code minimally
if ((a[i]).equals(word.charAt(j)) to if ((a[i]).equals("" + word.charAt(j))
This gives you an equality check between two strings.
Alternatively you can change the line if ((a[i]).equals(word.charAt(j))
to if ((a[i]).equals(word.substring(j,j+1))

Output goes stray at START and at END of a java loop

I am writing a code for translating Signals from one form to another form.
My code works well but fails for the ends.
INPUT: String [] test = {"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };
REQUIRED OUTPUT: B/101 B/1 B/106 B/201 B/21 B/11
GOT OUTPUT: B/1 B/101 B/1 B/106 B/201 B/21
Comparison of Required Output and got output
The first term B/1 is not required in got output.
B/11 is missing at the end in the required output.
ALGORITHM: "B" is replaced by "B/", and followed by addition of numbers appearing in Strings like "2", "3","100" which gives 105 and "1"
is to be added for "B" hence 106 and final result becomes 'B/106'.
I am new comer to java and programming. I need help to get the required output.
This is my code:
public class SignalConversion {
public static void main(String args[]) {
String [] test ={"B","100","B","B","2","3","100","B","200","B","3","17","B","10" };
int i=0; int x=test.length;
String netSignal="";
int total=0;
while(!(x==0)){
StringBuilder sb_matra= new StringBuilder();
StringBuilder sb_sur= new StringBuilder();
if(!test[i].equals("B")) {
total=total+(Integer.valueOf(test[i]));
}
else {
total=total+1;
sb_sur.append(test[i]+"/"+Integer.toString(total)+" " );
total=0;
}
netSignal=sb_sur.toString()+sb_matra.toString();
System.out.printf(netSignal);
i++;
x--;
}
}
}
When you encounter a "B", you should start summing the numbers following it, but only output the result when you encounter the next "B". That's why you have a problem at the ends. You print the first "B" when you encounter it, before calculating the number that should come with it.
Similarly, at the end of the loop, you should add an additional B with the last sum.
Here's a potential way of doing it (I think this loop is simpler than yours):
StringBuilder sb_sur= new StringBuilder();
boolean first = true;
for (int i = 0; i < test.length; i++) {
if(!test[i].equals("B")) {
total=total+(Integer.valueOf(test[i]));
} else {
if (!first) {
total=total+1;
sb_sur.append("B/"+Integer.toString(total)+" " );
total=0;
}
first = false;
}
}
total=total+1;
// account for the last B
sb_sur.append("B/"+Integer.toString(total)+" " );
I would have done this way,
public static void main(String[] args) {
String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200",
"B", "3", "17", "B", "10" };
boolean foundB = false;
int total = 0;
for(int i=0;i<test.length;i++){
if(foundB){
if(test[i].equals("B")){
System.out.print("B/"+(total+1)+" ");
total=0;
}else{
total += Integer.parseInt(test[i]);
}
if(i==(test.length-1)){
System.out.print("B/"+(total+1)+" "); // The last B
}
}
if(test[i].equals("B")){
foundB = true; // start counting only after you find a B
}
}
}
Oh, i see Eran has made nearly the same attemp.
String[] test = { "B", "100", "B", "B", "2", "3", "100", "B", "200", "B", "3", "17", "B", "10" };
List<String> resultTest = new ArrayList<>();
int value = 0;
for (int i = 0; i < test.length; i++) {
if (i != 0 && test[i].equalsIgnoreCase("B")) {
resultTest.add("B\\" + (value + 1));
value = 0;
} else {
if (!test[i].equalsIgnoreCase("B")) {
value += Integer.parseInt(test[i]);
}
}
}
resultTest.add("B\\" + (value + 1));
resultTest.forEach(System.out::println);

(High school Java homework help) Confusion on converting English letters to morse code

I am doing a project for my high school computer science class and I am running into some confusion. I am instructed to write a program which translates a user-inputted message from English to Morse code. I have completed the bulk of the assignment, but I can't seem to figure out how exactly to go about actually converting the user's message to Morse code.
import java.util.*;
import java.io.File;
import java.io.IOException;
public class MorseCode
{
public MorseCode()
{
}
//Reads in file containing Morse code and stores into an array of Strings
public static String [] readFile() throws IOException
{
String [] codes = new String[36];
int index = 0;
Scanner fileScanner = new Scanner(new File("morsecode.txt"));
while( fileScanner.hasNextLine() )
{
codes[index] = fileScanner.nextLine();
index++;
}
return codes;
}
//Converts the array of morse codes to an array of its corresponding letter
public static String [] findChars(String [] morseCode)
{
String [] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
for( int index = 0; index < morseCode.length; index++)
{
morseCode[index] = alphabet[index];
}
return morseCode;
}
}
Main
As of right now just used to test to make sure everything works as intended. Both the Morse code and its alphabetical and numeric equivalent outputs correctly.
import java.util.*;
import java.io.File;
import java.io.IOException;
public class MorseCodeTester
{
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
//Original Morse Code
String [] morseCode = MorseCode.readFile();
//Conversion to standard english letters
String [] toConvert = MorseCode.readFile();
String [] codeToLetters = MorseCode.findChars(toConvert);
for( int index = 0; index < morseCode.length; index++)
{
System.out.println(morseCode[index]);
}
System.out.println();
for( int index = 0; index < morseCode.length; index++)
{
System.out.println(codeToLetters[index]);
}
}
}
As stated previously, my confusion is coming from the actual conversion of English to Morse code, after the user inputs the desired message. If anybody could point me in the right direction it would be very appreciated.
You could store each character in a Map along with its corresponding morse code string. e.g.
HashMap<String, String> codes = new HashMap<String, String>();
codes.put("a", ".-");
codes.put("b", "-...");
Then when converting a string, you could use the map to find the corresponding morse code string for each character of the string to be converted.
For example:
String str = "To be converted to morse";
StringBuilder morseSB = new StringBuilder();
for(int i=0; i< str.length(); i++)
{
morseSB.append(codes.get(Character.toString(str.charAt(i)).toLowerCase()));
}
String morseResult = morseSB.toString();
Note: you will need to add some precautionary checks. e.g. checking if the result of codes.get() is null and you may need to handle other characters like spaces and periods.
this function convert "Hello world" to Morse code
public static String charToMorse( String s ){
if( s.equals( "H" ) || s.equals("h"))
return "....";
else if ( s.equals( "E" ) || s.equals("e"))
return "."
// other alphabet will placed here
}
and write another method to traverse string's character
public static void showMorse( String s ){
for( int i=0 ; i < s.length() ; i++ )
System.out.printf( "%s " , charToMorse( s[i] ) );
System.out.println();
}
this image show Morse code you need

Comparing charAt(x) to String array = letter [y]

I am trying to write a piece of code that produces a letter frequency using arrays. I am getting a bit stuck on how to compare a letter in a string to a letter in an array. my basic pseudo code is as follows.
import java.util.Scanner;
public class test {
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.print ("Please enter a sentence: ");
String str = sc.nextLine();
String [] let = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
Float [] freq = new Float [25];
int x,a = 0,b = 0, strCount = 0;
String str1;
str1 = str.replaceAll(" ", "");
for (x = 0; x < str1.length(); x++)
{
strCount++;
}
System.out.println("The number of Characters in the string is :" + strCount);
System.out.println();
And now I'm stuck on how to compare the str1 to the let array. I have tried the following but it has a problem with the comparing.
while (b < strCount)
{
while (a < let.length)
{
if (let[a] == str1.charAt(b))
{
freq[a] = freq[a]++ ;
}
if(let[a] != str1.charAt(b))
{
a = a++;
}
}
b = b++;
}
Any help would be much appreciated.
Thank you.
Well, I see a number of other issues but the one you're asking about is simple enough.
if (let[a].charAt(0) == str1.charAt(b)) // <-- one letter
{
freq[a]++ ;
}
else
{
a++;
}
Also, strCount = str1.length(); with no need for a loop.
You might want to get rid of the risk of eternal loops by replacing your while loops with
for
loops.
Also,
a = a++;
is incorrect, while
a++;
or
a = a+1;
is correct.
Fix those two, and you will have fixed your problem.
Let me address some of the problems that seem to stand out. First of all, you might want to enlarge your Float[] since it has a size of 25 and there are 26 letters in the alphabet, meaning you would want to do Float[] freq = new Float[26]instead. Also, you use str.replaceAll(), but str.replace() will suffice - they both replace all matches in the string.
To count the number of occurrences, you may want to use str.charAt(index) or break it up into a char array (str.toCharArray()) to compare the character with the values stored in the array. Since they are all single characters, you might also want to store the values as the primitive char instead of as a String.
The two while loops are completely unnecessary, as it can be done with the one forloop. Also, use str.length() instead of creating your own variable and using a for loop to increment strCount, especially when you specify to loop through it str.length() times...

Java while loop not working

The program allows the user to enter a phrase and converts it to ROT13, where each English letter entered, becomes the letter 13 places after it(A becomes N). My current code works when 1 character is entered, however I need it to run through the code the number of times there are characters. I've tried to put in a while loop at the beginning, but it doesn't seem to be working. Why is this?
import java.io.*;
public class J4_1_EncryptionErasetestCNewTry
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
System.out.println("Enter a phrase: ");
String phrase = myInput.readLine();
int length = phrase.length();
int y = 0, i = 0, num = 0;
while (y <= length) {
String letter = Character.toString(phrase.charAt(y));
y++;
while(!(letter.equals(key[i]))){
i++;
}
num = i;
System.out.println(keyA[num]);
y++;
}
}
}
See comments on code.
public static void main(String[] args) {
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
System.out.println("Enter a phrase: ");
String phrase = "";
try {
phrase = myInput.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int length = phrase.length();
int y = 0, i = 0, num = 0;
while (y < length) { // This should be y < length. Otherwise, it would throw a StringIndexOutOfBoundsException.
i=0; // Re-initialize
String letter = Character.toString(phrase.charAt(y));
// y++; // Unecessary incremental
while(!(letter.equalsIgnoreCase(key[i]))){
i++;
}
num = i;
System.out.print(keyA[num]);
y++;
}
}
Although this doesn't answer your problem, it answers your intention:
public static String rot13(String s) {
String r = "";
for (byte b : s.getBytes())
r += (char)((b + 13 - 'A') % 26 + 'A');
return r;
}
Your code is far too complicated for what it's doing. Really, all the work can be done in one line. Use byte arithmetic rather than array lookups etc. Simple/less code is always the best approach.
Please no comments about inefficiencies etc. This is a basic implementation that works (tested). The reader is free to improve on it as an exercise.
You're code will most likely break in your inner while loop as you are not resetting the value of i. By not doing so your gonna hit StringIndexOutOfBounds. I would recommend initialising i in your outer while loop or better yet just move int i = 0; inside the outer while loop.
I have implemented it in a different way, but it works as you expect, only for uppercase as in your example:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class WhileLoopIssue {
public static void main( String[] args ) throws IOException {
BufferedReader myInput = new BufferedReader( new InputStreamReader(
System.in ) );// Buffered Reader reads the
// number inputed
final List<String> letterList = Arrays.asList( "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z" );
System.out.println( "Enter a phrase: " );
String phrase = myInput.readLine();
final String[] letters = phrase.split( "" ); // Split input phrase
final StringBuffer buffer = new StringBuffer(); // Variable to save letters. Could be a String as well.
for ( int i = 0; i < letters.length; i++ ) {
final int letterIndex = letterList.indexOf( letters[i] ); // Get the numeric value of the letter
if ( letterIndex < 0 ) // Skip iteration if not found. Maybe a lowercase, or an empty String
continue;
final int nextLetterIndex = 13 + letterIndex; // Actual value of the letter + 13
if ( nextLetterIndex > letterList.size() ) {
buffer.append( nextLetterIndex % letterList.size() ); // Actual value greater than the total number of letters in the alphabet, so we get the modulus for the letter
} else {
buffer.append( letterList.get( nextLetterIndex ) ); // Letter in the range, get it
}
}
System.out.println( buffer.toString() );
}
}
You need to reset i in each iteration. It may happen that first letter found toward the end of the array "key". Your code will find next input char there onward, I guess this is not what you want, and will not find that char and will throw SIOBException. I have changed the while loop, removed twice increment in variable y as well. Have a look
while (y < length) {
i = 0; //Every Time you want to search from start of the array
//so just reset the i.
String letter = Character.toString(phrase.charAt(y));
while(!(letter.equals(key[i]))){
i++;
}
num = i;
System.out.println(keyA[num]);
y++;
}
I am assuming what ever you enter as input is a phrase of ONLY upper-case alphabets, else you will come across the SIOBException as you you will not be able to locate that letter in your array.
Just on side note, instead of those arrays you should use some other data structures which are efficient for searching like hashmap. Your linear search across the array is not optimized.

Categories