Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: need to add a Double Button to the code

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need to add a Double Button to the code

    Let's add another button to this version of the WordPlay program, a "Double" button. Notice that we've added new code, so that WordPlay now contains a JButton component called doubleButton. When the Double button is clicked, the displayed string should be doubled, or repeated, in the program.

    They give me most of the code but it is incomplete, im going to post the code that they gave first, then my answers which are incorrect.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class WordPlay extends JPanel implements ActionListener{
     
      JLabel inst = new JLabel("Enter your text:");
      JTextField textField = new JTextField(5);
     
      JButton submit = new JButton("Submit");
      JButton doubleButton = new JButton("Double");
     
      JLabel display = new JLabel();
     
      public WordPlay() {
        this.add(inst);
        this.add(textField);
        this.add(submit);
        submit.addActionListener(this);
    CODE 1
        this.add(display);
      }
     
      public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if(source == submit){
          display.setText(textField.getText());
        }
    CODE 2
    }
    }

    MY answer for CODE 1
    doubleButton = new JButton("Double");
    this.add(doubleButton);
    doubleButton.addActionListener(this);
    My answer for CODE 2
    if(source == doubleButton){
    display.setText(textField.getText(Double));
    }

    Usually when I submit something, it gives me some sort of feedback or error but not in this problem, i get nothing back. it just says it is wrong.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    it just says it is wrong.
    Who/what is the 'it' that says it "it is wrong'?

    If there are error messages please copy and paste the full text here.
    Last edited by Norm; August 4th, 2010 at 08:59 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to add a Double Button to the code

    Quote Originally Posted by Norm View Post
    Who/what is the 'it' that says it "it is wrong'?

    If there are error messages please copy and paste the full text here.
    well this is a homework assignment and usually, when something is wrong, there is a Feedback section that usually says some error message. When i enter the code I entered as my answer it just says Feedback:
    then blank. there is no error message so i dont know what is wrong with my code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    Can you compile the program using javac?
    If you get errors, copy and paste them here.

    Can you execute the program using the java.exe command?
    If you get errors, copy and paste them here.

    there is a Feedback section
    What program has the Feedback section? I don't recognize the word in the context of compiling and executing a java program.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need to add a Double Button to the code

    I would say it cant compile.
    if(source == doubleButton){
    display.setText(textField.getText(Double));
    }

    textField.getText(Double) should be attempting to call the getText method of JTextField. JTextField inherits this from JTextComponent. There are two getText() methods in JTextComponent. Here are the API descriptions of them:
    getText
    public String getText()Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException. Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

    Returns:
    the text
    Throws:
    NullPointerException - if the document is null
    See Also:
    setText(java.lang.String)
    getText
    public String getText(int offs,
    int len)
    throws BadLocationExceptionFetches a portion of the text represented by the component. Returns an empty string if length is 0.

    Parameters:
    offs - the offset >= 0
    len - the length >= 0
    Returns:
    the text
    Throws:
    BadLocationException - if the offset or length are invalid
    As you can see, there is no JTextField.getText(Variable) method. Also, keep in mind that what you have attempted to send getText() is just Double. The compiler will attempt to find a variable named Double. The Double Class is wrapper class for the primitive double, but you cant reference a class as a variable or whatever you tried to do.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to add a Double Button to the code

    Ive also tried it without Double as the variable and just left is empty () and i still got the same no feedback.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    i still got the same no feedback
    Can you explain what "feedback" is?

    Can you show the code for the statements you are talking about? We can't see what it is you are doing and would have to guess.

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to add a Double Button to the code

    Quote Originally Posted by Norm View Post
    Can you explain what "feedback" is?

    Can you show the code for the statements you are talking about? We can't see what it is you are doing and would have to guess.
    Everything I know and was given is in my first post. The prompt followed by the incomplete Code. Then I gave you both of my codes.

    "Feedback" is a section that, once i submit my answer, usually gives me some sort of feedback/errors to tell me how my answer is wrong. In this case there isnt any information in Feedback once I hit submit so I don't know how my code is wrong because no error messages are given.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    YOU must be working with some kind of IDE. There is no "Feedback" section involved in java programming.

    If you are having problems with your IDE, you'll have to find a forum for that IDE to ask questions about the problems you are having with it.

  10. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to add a Double Button to the code

    well it is a class for java programming
    The feedback is just where the errors show up for the class via the website that the homework is on. we can rename Feedback "Errors" if it helps get my point across. REGARDLESS nothing shows up, no error messages, but it is still wrong

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    I have no idea what the website does with your java program. I'm not sure how that relates to writing and debugging java programs.

    What happens when you compile your program with the javac command?
    Do you get errors?
    If no errors from the javac compiler, then what happens when you execute the program using the java command?

  12. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need to add a Double Button to the code

    Ok, there are some fundamental issues here. This code does compile:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class WordPlay extends JPanel implements ActionListener{
     
      JLabel inst = new JLabel("Enter your text:");
      JTextField textField = new JTextField(5);
     
      JButton submit = new JButton("Submit");
      JButton doubleButton = new JButton("Double");
     
      JLabel display = new JLabel();
     
      public WordPlay() {
        this.add(inst);
        this.add(textField);
        this.add(submit);
        submit.addActionListener(this);
    doubleButton = new JButton("Double");
    this.add(doubleButton);
    doubleButton.addActionListener(this);
        this.add(display);
      }
     
      public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if(source == submit){
          display.setText(textField.getText());
        }
    if(source == doubleButton){
    display.setText(textField.getText());
    }
    }
    }

    So, let me ask you a question about this code:

    Do you have a main? If so, where is the code for that? Because I am having no issues with this. If you dont know what a main is, it would be the class in your other programs that contain the code public static void main(String args[]). Look at those to figure it out.
    If you don't have a main, here is the steps for your main (I wont make the code for you).
    1. Create a JFrame
    2. Create a WordPlay object
    3. Set your WordPlay object as the JFrame's Content Pane by using the method JFrame.setContentPane(WordPlay)
    4. Set your JFrame's size by using the method JFrame.setSize(int,int)
    5. Set your JFrame as visible by using the method JFrame.setVisible(boolean)
    6. Set your JFrame's Default Close Operation by using the method JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE)

    And presto, you have your main.

    Now, if the doubleButton should double the text, do you mean that if the input is Test, the output should be TestTest? If so, you need to get the textField text twice and just concatenate them. That is extremely simple, and I'm sure you can figure it out.

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need to add a Double Button to the code

    I think he's using an online submission of his code. So, he submits his source (either in a file or just text), then the webpage does some magic to compile and insert it into a testing framework. It then spits back if he got the answer right or wrong (usually with little to no feedback).

    I've worked with something similar in the past on physics homework called "Web-assign", and it just doesn't provide any feedback except whether you got it right or wrong

    However, since it is a Java program I would suggest you get the full code (or at least enough to test the section you are working on) and actually build/debug it yourself before submitting your answer to the web-page. Tell us what errors you get at compile time, what errors you get during runtime, and any problems with the actual output vs. the expected output.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need to add a Double Button to the code

    How does anyone learn programming by only coding a small subsection of a program?
    I suppose for the first couple of weeks while learning loops or other simple constructs.

  15. #15
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to add a Double Button to the code

    Quote Originally Posted by helloworld922 View Post
    I think he's using an online submission of his code. So, he submits his source (either in a file or just text), then the webpage does some magic to compile and insert it into a testing framework. It then spits back if he got the answer right or wrong (usually with little to no feedback).

    I've worked with something similar in the past on physics homework called "Web-assign", and it just doesn't provide any feedback except whether you got it right or wrong

    However, since it is a Java program I would suggest you get the full code (or at least enough to test the section you are working on) and actually build/debug it yourself before submitting your answer to the web-page. Tell us what errors you get at compile time, what errors you get during runtime, and any problems with the actual output vs. the expected output.
    YES this is what is going on, this online submission is making it difficult to learn and do well in this class, and the all online textbook isn't very in-depth also there arent any lectures or in-class lessons with a real professor. so the students of this class are basically teaching Java to themselves, which is probably why I find myself at this forum way too much...

  16. #16
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need to add a Double Button to the code

    Best advice, play around with non-GUI related things in your free time. Try to grab a good understanding of the language prior to going really indepth with GUIs because there is alot of overhead in GUIs that will distract you from understanding the code that matters.

    In a few weeks I'm starting college and I'll be starting a C++ class. I've done extremely little C++ (like 1 or 2 programs maybe), so I'm expecting to go to a forum and ask plenty of questions myself.

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  2. Formatting output of a Double
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:20 PM
  3. Limiting decimal places in a double
    By darek9576 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 14th, 2010, 11:27 AM
  4. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM