Java array and parseInt troubles

Discussion in 'Software' started by Pure-D, Dec 1, 2005.

  1. Pure-D

    Pure-D Private E-2

    Hi, I'm having trouble with my java code at the moment.
    I've created an array of buttons and I have a textField.
    I know that I have to perform a conversion for when a button is pressed, using string, paraseInt, etc... but I've been trying every combination and I'm ending up with sqat :( The program has an array of 4 buttons numbered 0-4 and all I'm trying to do is get the button number that is pressed to appear in the text box. So has anyone got any advice on where I'm going wrong? any ideas? Thank, D

    Code:
    import java.awt.*; 
    import java.awt.event.*;
    import java.applet.*;
    public class Calc1 extends Applet implements ActionListener{ 
    Button[] aNum;
    TextField Answer;
    
    
    public Calc1() { 
    }
    
    public void init() { 
     aNum = new Button[5];
     Answer = new TextField(10);
     add (Answer); 
     for (int i = 0; i<5; i++) { 
         aNum[i] = new Button(Integer.toString(i)); 
         add (aNum[i]); 
         } 
    }
    
    public boolean actionPerformed (Event e, Object o) { 
     if (e.getActionCommand() instanceof String){
     	String s = Integer.parseInt(Answer.setText());	
     }
     	 
        
    
    }
    } 
    
     
  2. bytegoddess

    bytegoddess Private E-2

    Hello Pure-D,

    I have to ask... do you have a copy of the Java API, and if so have you spent some time getting familiar with it? The one single thing that you can do make life easier for you is to become comfortable in using the API: http://java.sun.com/j2se/1.4.2/docs/api/index.html.

    Here are your two free hints for the day:

    First, if you look up the ActionListener interface, you will see that the actionPerformed method must not return anything, and that it needs an ActionEvent object passed to it in order for it to work.

    Second, you haven't added an actionListener to the buttons you've created, so no one can hear them when they scream ;-)

    My questions for you: Are you using a text editor or an IDE to write your programs? If you are using an IDE, I suggest that you stop, and start using a plain text editor until you are more familiar with the language. IDEs only get in the way of beginners, and if you try to learn programming from the way they write code, you will be done for. Second, what are you using as reference/tutorial materials? If you are trying to learn this "by ear," without starting at the beginning and learning from the ground up, you are doing yourself a great disservice. I suspect this, mostly because you had a question in another thread that made it clear that you hadn't yet mastered the basic data types. I am not saying this to dis you in any way, but only to suggest that you slow down and get it right before you learn a whole bunch of nasty habits... trust me, I have been there, and it's a bummer if you have to go back and re-learn stuff.

    Best of luck,
    Barbara ;-)
     
  3. rmStar-R

    rmStar-R Private E-2

    I think the following line is the problem.
    Code:
    String s = Integer.parseInt(Answer.setText());
    Doesn't Integer.parseInt() function return an Integer type? In this line, you are assigning a string variable with an integer value. Please correct me if im wrong.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds