Write a method that takes a string as an argument If your st

Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return \"Bad number\". Otherwise, return the telephone number equivalent of the input Your return can be of type String. Eg. A, B, or C are 2, D, E, E, F are etc., \"GOCOSC1046\" would return \"4626721046\" (even better would be \"462-672-1046\" but not required).

Solution

StringToTelephoneNumber.java

import java.util.Scanner;

public class StringToTelephoneNumber {

   public static void main(String[] args) {
      
   //Declaring variables  
   String str3=\"\";
   int len;
  
   //Scanner class object is used to read the inputs entered by the user
   Scanner sc=new Scanner(System.in);
  
   //Getting the String entered by the user
   System.out.print(\"Enter String :\");
   str3=sc.next();
  
   //calling the method by passing the string as input
   stringToTelephoneNo(str3);
  
   }

   /* This method will convert the String to telephone number
   * Params:String
   * Return :void
   */
   private static void stringToTelephoneNo(String str3) {
       String str=\"\",str1=\"\";
       if(str3.length()>10 ||str3.length()<1)
       {
           System.out.println(\":: Bad number ::\");
       }
       else
       {
           //Converting the lower case to upper case
           str=str3.toUpperCase();
          

           //This for loop will convert the string to telephone number by using concatenation
           for(int i=0;i<str.length();i++)
           {
               if(str.charAt(i)==\'A\' || str.charAt(i)==\'B\'||str.charAt(i)==\'C\')
               {
                   str1+=\"2\";
               }
           if(str.charAt(i)==\'D\' || str.charAt(i)==\'E\'||str.charAt(i)==\'F\')
               {
                   str1+=\"3\";
               }
           if(str.charAt(i)==\'G\' || str.charAt(i)==\'H\'||str.charAt(i)==\'I\')
               {
                   str1+=\"4\";
               }
           if(str.charAt(i)==\'J\' || str.charAt(i)==\'K\'||str.charAt(i)==\'L\')
               {
                   str1+=\"5\";
               }
           if(str.charAt(i)==\'M\' || str.charAt(i)==\'N\'||str.charAt(i)==\'O\')
               {
                   str1+=\"6\";
               }
           if(str.charAt(i)==\'P\' || str.charAt(i)==\'Q\'||str.charAt(i)==\'R\'||str.charAt(i)==\'S\')
               {
                   str1+=\"7\";
               }
           if( str.charAt(i)==\'T\'||str.charAt(i)==\'U\' ||str.charAt(i)==\'V\'|| str.charAt(i)==\'W\')
               {
                   str1+=\"8\";
               }
           if( str.charAt(i)==\'X\' ||str.charAt(i)==\'Y\' || str.charAt(i)==\'Z\')
               {
                   str1+=\"9\";
               }

           if(Character.isDigit(str.charAt(i)))
           {
               str1+=str.charAt(i);
           }
           }
           if(str.length()==10)
           {
               System.out.printf(\"The Generated Telephone Number is : %s-%s-%s\", str1.substring(0, 3), str1.substring(3, 6),
               str1.substring(6, 10));
           }
           else
               //Displaying the String type telephone number in particular format
               System.out.println(\"The Generated Telephone Number is :\"+str1);      
       }
      
       }
  
}

_________________

Output:

Enter String :GOCOSC1046
The Generated Telephone Number is : 462-672-1046

_________________

Output1:

Enter String :GOCOSC10467
:: Bad number ::

_____Thank You

 Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return \
 Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site