Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

converting my code to match gui

Posted on 2004-10-28
1
Medium Priority
?
216 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
Comment
Question by:DreamWeaver777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
tdisessa earned 2000 total points
ID: 12440676
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

597 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