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, F are 3 etc., \"GOCOSC1046\" would return *4626721046* (even better would be \"462-672-1046\" but not required). Write a brief main method that collects input and shows off your methods.

Solution

/* Program code to string convertion to phone formate */

import java.util.Scanner;           //scanner class to read input
import java.io.*;
class stringconverter               //Grades class creation
{
   public static void main(String args[])   throws IOException       //Main method of class ,programme execution begins by calling this method
   {
   Scanner sc=new Scanner(System.in);           //scanner class object sc creation
       String str1=\"\";
       System.out.println(\"Enter String :\ \");           //Prompt for string
       str1=sc.nextLine();                               //read string
       stringconverter ob=new stringconverter();       //creating class object
       String result=ob.stringconverter(str1);           //callijng fucntion stringconverter
       System.out.println(\"Converted String to Phone Format \"+result);   //display output
     
  
   sc.close();
   }
   String stringconverter(String str2) throws IOException       //function defination of stringconverter
   {
       String str3=\"\";
       int len=str2.length();                   //finding string length
       System.out.println(len);
       if(len>10 || len <10)                   //if length is lessthan & morethan 10 chars then return bad string
       return \"Bad String\";
       else
       {
               StringReader reader = new StringReader(str2);   //string reader class to read string into char wise
              
              
          
           for(int i=0;i<len;i++)           //loop reapeats till ends of string
           {
               int singleChar = reader.read();   //read singlechar each time loop repeats from reader
               char ch = (char) singleChar;

               if (ch==\'A\' || ch==\'B\' || ch==\'C\')               //if else ladder comparisions accordiing given input comparisions
               {
                   str3+=2;
               }
               else if (ch==\'D\' || ch==\'E\' || ch==\'F\')
               {
                   str3+=3;
               }
               else if (ch==\'G\' || ch==\'H\' || ch==\'I\')
               {
                   str3+=4;
               }
               else if (ch==\'J\' || ch==\'K\' || ch==\'L\')
               {
                   str3+=5;
               }
               else if (ch==\'M\' || ch==\'N\' || ch==\'O\')
               {
                   str3+=6;
               }
               else if (ch==\'P\' || ch==\'Q\' || ch==\'R\')
               {
                   str3+=7;
               }
               else if (ch==\'S\' || ch==\'T\' || ch==\'U\')
               {
                   str3+=8;
               }
               else if (ch==\'V\' || ch==\'W\' || ch==\'X\')
               {
                   str3+=9;
               }
               else if (ch==\'Y\' || ch==\'Z\')
               {
                   str3+=1;
               }
               else
               {
                  
                   str3+=ch;
               }
                 

           }

      }
       return str3;           //return converted string format
   }
}

Output :


javac stringconverter.java //compile programme

java stringconverter //run programme

Sample Run :
Enter String :

ABCDEFGHI9
10
Converted String to Phone Format 2223334449

 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