Link to home
Start Free TrialLog in
Avatar of DreamWeaver777
DreamWeaver777

asked on

converting my code to match gui

I am having trouble turning code that I have into strings so it can compile with the GUI I have written.  The code does what it needs to do but I just need it to complie with GUI so it can run properly.  I know I need to use strings but I am very shady on how to convert what to srtings and how.  I not sure if I can keep my arrays and add in the needed methods.  Any and all help is appreciated.  Here is my model code and my GUI.

Model Code:

import java.util.*;

public class test {    
     static String phonenumber="2334567";
     public static void main(String[] args){
          int i=transferr();
     }
     private static int transferr(){
          ArrayList a1=new ArrayList();
          ArrayList a2=new ArrayList();
          a2.add("a");
          a2.add("b");
          a2.add("c");
          ArrayList a3=new ArrayList();
          a3.add("d");
          a3.add("e");
          a3.add("f");
          ArrayList a4=new ArrayList();
          a4.add("g");
          a4.add("h");
          a4.add("i");
          ArrayList a5=new ArrayList();
          a5.add("j");
          a5.add("k");
          a5.add("l");
          ArrayList a6=new ArrayList();
          a6.add("m");
          a6.add("n");
          a6.add("o");
          ArrayList a7=new ArrayList();
          a7.add("p");
          a7.add("q");
          a7.add("r");
          a7.add("s");
          ArrayList a8=new ArrayList();
          a8.add("t");
          a8.add("u");
          a8.add("v");
          ArrayList a9=new ArrayList();
          a9.add("w");
          a9.add("x");
          a9.add("y");
          a9.add("z");
          ArrayList a0=new ArrayList();    

          ArrayList b=new ArrayList();
         
          for(int i=0;i<phonenumber.length();i++){
               char c=phonenumber.charAt(i);
               switch(c){
                    case '1':
                         System.out.println("Invalid number");
                         return -1;                                                  
                    case '2':
                         b.add(a2);
                         break;
                    case '3':
                         b.add(a3);
                         break;
                    case '4':
                         b.add(a4);
                         break;
                    case '5':
                         b.add(a5);
                         break;
                    case '6':
                         b.add(a6);
                         break;
                    case '7':
                         b.add(a7);
                         break;
                    case '8':
                         b.add(a8);
                         break;
                    case '9':
                         b.add(a9);
                         break;
                    case '0':
                         System.out.println("Invalid number");
                         return -1;                                            
               }              
          }    
         
          System.out.println(b);
          ArrayList result=new ArrayList();          
          ArrayList result1=new ArrayList();
          String s=new String();
          for(int m=0;m<b.size();m++){              
               ArrayList thisresult=(ArrayList)b.get(m);
               for(int n=0;n<thisresult.size();n++){                    
                    if(result.size()<thisresult.size()){
                         result.add(thisresult.get(n));
                    }else{          
                         int l=result.size();
                         for(int k=0;k<l;k++){
                              s=(String)result.get(k);
                              s=s+(String)thisresult.get(n);
                              result.add(s);    
                         }                        
                    }                    
               }              
               for (int mm=0;mm<result.size();mm++){
                    if(((String)result.get(mm)).length()==m+1){                        
                         result1.add(result.get(mm));
                    }
               }
               result=new ArrayList(result1);
               result1=new ArrayList();
          }
               
          System.out.println(result);
          return 0;
     }
}


GUI:

private TelephoneWordsModel theModel;

  /**
   * JButton to let the stuff go
   */
   private JButton goButton;

  /**
   * JTextArea to get the input from the user
   */
  private JTextArea inputArea;

  /**
   * JTextArea to show the output to the user
   */
  private JTextArea outputArea;

  /**
   * Default constructor to setup the window
   * @since 1.0
   */
  public MoneyGUI() {
    //For the application setup the window
    if (this instanceof JFrame) {
      setSize(500,200);
      setLocation(40,40);
      setTitle("Telephone Words");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    //Initialize instance variables
    theModel = new TelephoneWordsModel();
    goButton = new JButton("Go");
    goButton.addActionListener(this);
    inputArea = new JTextArea(1,10);
    outputArea = new JTextArea(3,40);
    outputArea.setEditable(false);
    outputArea.setLineWrap(true);
    outputArea.setWrapStyleWord(true);

    //Setup top panel
    JPanel topPanel = new JPanel();
    JLabel numLabel = new JLabel("Number to translate: ");
    topPanel.add(numLabel);
    topPanel.add(inputArea);    

    //Setup center panel
    JPanel centerPanel = new JPanel();
    JLabel outLabel = new JLabel("Number as text: ");
    centerPanel.add(outLabel);
    centerPanel.add(outputArea);
   
    this.getContentPane().add(topPanel,BorderLayout.NORTH);
    this.getContentPane().add(centerPanel,BorderLayout.CENTER);
    this.getContentPane().add(goButton,BorderLayout.SOUTH);
  }

  public void actionPerformed (ActionEvent e) {
    try {
      String inText = inputArea.getText();
      double inValue = Double.parseDouble(inText);
      theModel.setPhonenumber(inValue);
      outputArea.setText(theModel.getTelephoneWords());
    }
    catch (NumberFormatException except) {
      outputArea.setText("You have input a non-numeric value");
      inputArea.setText("");
    }
  }

  /**
   * Main method to get stuff started
   * @since 1.0
   */
  public static void main (String args[]) {
    TelephoneWordsGUI myGUI = new TelephoneWordsGUI();
    myGUI.show();
  }
}
ASKER CERTIFIED SOLUTION
Avatar of tdisessa
tdisessa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial