Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I just need error validation added to the code. import java.util.Scanner;? impor

ID: 3911366 • Letter: I

Question

I just need error validation added to the code.

import java.util.Scanner;?

import java.util.*;

/**

Converts a numeric pin to an equivalent word using the digit to

letter mapping on a standard telephone keypad.

*/

public class PinWordEnumerator

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.print("Enter a pin number-> ");

String num = scan.nextLine();

System.out.println();

System.out.printf("The keypad encodings for %s are:%n",num);

enumerateWords(num);

}

  

/**

A wrapper for a resultursive method that enumerates all the

phone keypad encodings for a number.

@param n a string representing the number

*/

public static void enumerateWords(String n) {

// Implement a resultursive method that generates

// all possible phone keypad encodings for a

// number. Implement additional auxiliary methods

// if necessary.

//these are the keypad letters

String[] keypadLetters = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

//storing the result to result list

List<String> result = new LinkedList<>();

StringBuilder string = new StringBuilder();

//calling function

letterCombinations(n, 0, keypadLetters, string, result);

//printing the result

for(String i:result) System.out.println(i);

}

  

private static void letterCombinations(String n, int number, String[] keypadLetters, StringBuilder string, List<String> result) {

//if it is zero length returning same

if (n.length() == number) {

result.add(string.toString());

return;

}

String letter = keypadLetters[n.charAt(number) - '2'];

//this is the main logic to produce keypad combination letters

for (int i = 0; i < letter.length(); i++) {

string.append(letter.charAt(i));

letterCombinations(n, number + 1, keypadLetters, string, result);

string.deleteCharAt(string.length() - 1);

}

}

}

Explanation / Answer


Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.Scanner;

import java.util.*;

/**

Converts a numeric pin to an equivalent word using the digit to

letter mapping on a standard telephone keypad.

*/

public class PinWordEnumerator

{


public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);


System.out.print("Enter a pin number-> ");
String num = scan.nextLine();

while(!isValid(num))
{
System.out.println("Invalid pin. Should contain only digits 2-9!");
System.out.print("Enter a pin number-> ");
num = scan.nextLine();
}
System.out.println();

System.out.printf("The keypad encodings for %s are:%n",num);

enumerateWords(num);

}


private static boolean isValid(String numericPin)
{
for(int i = 0; i < numericPin.length(); i++)
{
char c = numericPin.charAt(i);
if(c < '2' || c > '9')
return false;
}

return true;

}
/**

A wrapper for a resultursive method that enumerates all the

phone keypad encodings for a number.

@param n a string representing the number

*/

public static void enumerateWords(String n) {

// Implement a resultursive method that generates

// all possible phone keypad encodings for a

// number. Implement additional auxiliary methods

// if necessary.

//these are the keypad letters

String[] keypadLetters = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

//storing the result to result list

List<String> result = new LinkedList<>();

StringBuilder string = new StringBuilder();

//calling function

letterCombinations(n, 0, keypadLetters, string, result);

//printing the result

for(String i:result) System.out.println(i);

}

private static void letterCombinations(String n, int number, String[] keypadLetters, StringBuilder string, List<String> result) {

//if it is zero length returning same

if (n.length() == number) {

result.add(string.toString());

return;

}

String letter = keypadLetters[n.charAt(number) - '2'];

//this is the main logic to produce keypad combination letters

for (int i = 0; i < letter.length(); i++) {

string.append(letter.charAt(i));

letterCombinations(n, number + 1, keypadLetters, string, result);

string.deleteCharAt(string.length() - 1);

}

}

}
output
-----


Enter a pin number-> 124a
Invalid pin. Should contain only digits 2-9!
Enter a pin number-> 123
Invalid pin. Should contain only digits 2-9!
Enter a pin number-> 2345

The keypad encodings for 2345 are:
adgj
adgk
adgl
adhj
adhk
adhl
adij
adik
adil
aegj
aegk
aegl
aehj
aehk
aehl
aeij
aeik
aeil
afgj
afgk
afgl
afhj
afhk
afhl
afij
afik
afil
bdgj
bdgk
bdgl
bdhj
bdhk
bdhl
bdij
bdik
bdil
begj
begk
begl
behj
behk
behl
beij
beik
beil
bfgj
bfgk
bfgl
bfhj
bfhk
bfhl
bfij
bfik
bfil
cdgj
cdgk
cdgl
cdhj
cdhk
cdhl
cdij
cdik
cdil
cegj
cegk
cegl
cehj
cehk
cehl
ceij
ceik
ceil
cfgj
cfgk
cfgl
cfhj
cfhk
cfhl
cfij
cfik
cfil