★ wanayoo — archive 1999 http://www.webtechniques.com/sourcecode/1998/11/java/1.lstNouvelle recherche | Portail wanayoo

 
| home | current issue | archives | source code | events | demo express | search | editorial calendar | advertising | customer service | author guidelines | jobs | about

S O U R C E   C O D E / 1998 / 11 / java / 1.lst

// Telnumapp.java
// Telephone number applet -Williams
// Simple applet to convert a 
// phone number into a string

import java.util.*;
import java.applet.*;
import java.awt.*;

public class Telnumapp extends Applet
  {
  // UI parts
  TextField num;  
  Button go;
  List list;

// Old-style event handler for the button
// Obviously, just for applet
 public  boolean action(Event evt, Object what)
  {
   if (evt.target==go)
     {
           Vector vec;
           Enumeration e;
           int i;
           String s;
           s=num.getText();
           int nums[]=new int[s.length()];
           // create integer input array
           for (i=0;i<s.length();i++)
           nums[i]=Integer.parseInt(s.substring(i,i+1));
// process it
           vec=process(nums);
// display results
           e=vec.elements();
           list.removeAll();
           while (e.hasMoreElements())
           list.addItem((String)e.nextElement());
           return true;
     
     }
  return false;
  }

// Initialize applet (applet only)
  public void init()
  {
  setLayout(new BorderLayout());
  num=new TextField();
  list=new List();
  go=new Button("Process");
  add(num,"North");
  add(go,"Center");
  add(list,"South");
  show();
  }


// This begins the non-applet portion of the code
// The process routine is common to both applet and
// non-applet

// Translation array
  static String ary[][]=
    {
      {"0","0","0"},
      {"1","1","1"},
      {"A","B","C"},
      {"D","E","F"},
      {"G","H","I"},
      {"J","K","L"},
      {"M","N","O"},
      {"P","R","S"},
      {"T","U","V"},
      {"W","X","Y"}
    };

// This function actually processes a phone number
// (in integer array form) into a vector of
// strings
static Vector process(int []iary)
  {
  int[] idx=new int[iary.length];
  String result;
  Vector rv=new Vector();
  mainloop:
  while (true)
    {
    boolean carry=true;
// Construct a new name
    result=new String();
    for (int q=0;q<iary.length;q++)
     result=result+ary[iary[q]][idx[q]];
// Add it to the list of returned names
   rv.addElement(result);
// Add 1 to iary using base 3 carry semantics 
   for (int i=iary.length-1;i>=0&&carry;i--)
     {
// but not for 0 or 1, skip those
     if (ary[iary[i]][0].equals("0")||ary[iary[i]][0].equals("1"))
       {
       carry=true;
       continue;  // only do "0" and "1" one time each
       }
     idx[i]=idx[i]+1;  // add 1 to the current digit
     if (idx[i]==3)    // overflow?
       {
	 if (i==0) break mainloop;  // done!
         idx[i]=0;
         carry=true;
       }
     else
       {
       carry=false;
       }
     }
    }
  return rv;
  }

// main program - not used when we are an applet
public static void main(String argv[])
  {
  int i;
  Vector v;
  Enumeration e;
  String s;
  int nums[]=new int[argv[0].length()];
// build input array
  for (i=0;i<argv[0].length();i++)
    nums[i]=Integer.parseInt(argv[0].substring(i,i+1));
// do processing 
  v=process(nums);
// print elements
  e=v.elements();
  while (e.hasMoreElements()) System.out.println(e.nextElement());
  }
}


| home | current issue | archives | source code | demo express | events | search | editorial calendar | advertising | customer service | author guidelines | jobs | about




Entire contents copyright 1996-2000 CMP Media Inc.
Read our privacy policy.