Solved

converting my code to match gui

Posted on 2004-10-28
185 Views
Last Modified: 2010-03-31
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();
  }
}
0
Question by:DreamWeaver777
    1 Comment
     
    LVL 2

    Accepted Solution

    by:
    Change:

         private static int transferr(){
     

    to:

         public static String transferr(String phoneNumber){
             this.phonenumber = phoneNumber;

    In order to pass a string into the method and output a string from the method
    ----

    Change:

              System.out.println(result);
              return 0;
         }

    to:

              System.out.println(result);
              return result;
         }

    in order to output your string
    ----

    Change:

          theModel.setPhonenumber(inValue);
          outputArea.setText(theModel.getTelephoneWords());


    to:

       outputArea.setText(test.transferr(inText ));

    To input your new number and then send it to the testarea

    ----

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    Title # Comments Views Activity
    Conflicting setter definitions for property Exception 5 29
    seriesUp challenge 7 54
    hash value 2 22
    Updating Java 9 47
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now