Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have written a program, it can run. But I have difficulties to produce an error message if the input are not letters ,

Hi, I have written a program, it can run.

But I have difficulties to produce an error message if the input are not letters, for example, if user key in "999999999" or "995$&*", how to show an error message?

Here is the program:

package assignment

import java.util.Scanner;

public class Assignment {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

String phoneLetter; //accept phone number to key in as letters

int i, mark, len; //'mark' stores the location where a hyphen will be placed

char c;

Scanner kb = new Scanner (System.in); //taking input from keyboard

do { //use do-while loop to accept the phoneNo with any length

i=0; mark =0;

System.out.println("Enter the phone number in letters: ");

phoneLetter = kb.nextLine();//read the input

phoneLetter = phoneLetter.toUpperCase();//accepting upper or lower case

phoneLetter = phoneLetter.replaceAll(" ", "");//remove blank spaces

while (i < phoneLetter.length() ) { //part of do-while loop

//use 'length' to accept as phoneNo in any length

c = phoneLetter.charAt(i);

switch (c) {

case 'A': case 'B': case 'C':

System.out.print("2");

mark++; // to put in "-"

break;

case 'D': case 'E': case 'F':

System.out.print("3");

mark++;

break;

case 'G': case 'H': case 'I':

System.out.print("4");

mark++;

break;

case 'J': case 'K': case 'L':

System.out.print("5");

mark++;

break;

case 'M': case 'N': case 'O':

System.out.print("6");

mark++;

break;

case 'P': case 'Q': case 'R': case 'S':

System.out.print("7");

mark++;

break;

case 'T': case 'U': case 'V':

System.out.print("8");

mark++;

break;

case 'W': case 'X': case 'Y': case 'Z':

System.out.print("9");

mark++;

break;

default://default keyword specifies some code to run

//if there is no case match:

System.out.print(phoneLetter);

mark++;

break;

}

if (mark==3||((mark-3)%4==0 && mark != phoneLetter.length()))

//to set "-" after first 3 digits, subseqently after every 4 digits

System.out.print("-");

i++;

}

System.out.print(" Do it again (Y/N)? Enter 'Y' to cotinue; "

+ "Else, press ANY OTHER key to stop input. ");

//determine whether to accept next input or stop taking input

phoneLetter = kb.nextLine();

phoneLetter = phoneLetter.toUpperCase();

phoneLetter = phoneLetter.replaceAll(" ", "");

} while (phoneLetter.charAt(0)=='Y');//if input is 'Y", continue

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions