lundi 29 juin 2015

US phone conversion from letters to numbers

Think I have gone wrong somewhere so I could use some help. OK the idea is to have the letters you usually find on phones. e.g. if I wanted to find the number for HAIRCUT it should output like 4247288.

public class PhoneNumber
{
    String s;
    int i;

    private void PhoneNumber()
    { 
      if ("ABC".contains(""+s.charAt(i))){
          i=2;
        }
          else if ("DEF".contains(""+s.charAt(i))){
              i=3;
            }
            else if ("GHI".contains(""+s.charAt(i))){
                i=4;
            }
            else if ("JKL".contains(""+s.charAt(i))){
                 i=5;
            }
            else if ("MNO".contains(""+s.charAt(i))){
                i=6;
            }
            else if ("PQRS".contains(""+s.charAt(i))){
                i=7;
            }
            else if ("TUV".contains(""+s.charAt(i))){
                i=8;
            }
            else if ("WXYZ".contains(""+s.charAt(i))){
                i=9;
            }                     
    }
    public void decode(){
        PhoneNumber pn = new PhoneNumber();
        pn.decode("HAIRCUT")
        pn.decode("NEWCARS"); 
    }
   }

I know i am missing stuff and even the println at the end but need guidance on how to do this. if you look at the code ABC will come out as 2, DEF as 3 etc. Any ideas? I'm very new to Java apologies in advance for my failed attempt.

Aucun commentaire:

Enregistrer un commentaire