Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By gimbal2

Thread: Help with currency applet

  1. #1
    tgg
    tgg is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

    Default Help with currency applet

    Can anyone have a quick look at my code, the program compiles with no errors but for some reason its not converting.
    I'm a beginner working through tutorials no go easy with me.



    package currency;

    import java.applet.Applet; // Provides the Applet class.
    import java.awt.*; // Provides Button class, etc.
    import java.awt.event.*; // Provides ActionEvent, ActionListener

    /**
    *
    * @author Tom
    */

    public class currencyconverter extends Applet implements ActionListener {


    // Declare the GUI components globally

    Label titleLabel, currencyLabel, inputLabel, convertedLabel;
    TextField inputBox, rateBox, ConvertedBox;
    Button poundButton, euroButton, clearButton;

    // Declare integer variables to hold the number input
    // by the user, the running count and the running total

    double euro = 0.85;
    double pound = 1.18;

    //integer variables for placing in the TextFields
    String poundString, euroString, ratestring;
    double number;
    //double inputString;

    double displayTotal, value;
    String displayString, inputString;



    @Override
    public void init() {

    setLayout(null);

    // Create the Labels
    titleLabel = new Label("Currency Converter");
    currencyLabel = new Label("Rate of exchange");
    inputLabel = new Label("Input amount to be exchanged");
    convertedLabel = new Label ("Converted amount");


    //create buttons
    poundButton = new Button("Pound");
    euroButton = new Button("Euro");
    clearButton = new Button ("Clear");

    // Create the TextFields

    inputBox = new TextField(10);
    rateBox = new TextField(30);
    ConvertedBox = new TextField(30);


    //position components

    titleLabel.setLocation(100, 5);
    titleLabel.setSize(250, 30);

    currencyLabel.setLocation(20,40);
    currencyLabel.setSize(130,20);

    inputLabel.setLocation(20, 70);
    inputLabel.setSize(180, 20);

    convertedLabel.setLocation(20, 100);
    convertedLabel.setSize(130, 20);

    poundButton.setLocation(30,140);
    poundButton.setSize(50,20);

    euroButton.setLocation(80,140);
    euroButton.setSize(50,20);

    clearButton.setLocation(130,140);
    clearButton.setSize(50,20);

    inputBox.setLocation(250,70);
    inputBox.setSize(70,20);

    rateBox.setLocation(250,40);
    rateBox.setSize(70,20);

    ConvertedBox.setLocation(250,100);
    ConvertedBox.setSize(70,20);

    // Add the components to the applet window

    add(titleLabel);

    add(inputLabel);
    add(inputBox);
    inputBox.setEditable(true);
    inputBox.addActionListener(this);

    add(currencyLabel);
    add(rateBox);
    rateBox.setEditable(false);
    rateBox.addActionListener(this);

    add(convertedLabel);
    add(ConvertedBox);
    ConvertedBox.setEditable(false);
    ConvertedBox.addActionListener(this);

    add(poundButton);
    poundButton.addActionListener(this);
    add(euroButton);
    euroButton.addActionListener(this);
    add(clearButton);
    clearButton.addActionListener(this);
    } // End of init()

    @Override
    public void actionPerformed(ActionEvent e) {

    String arg = e.getActionCommand();

    inputString = inputBox.getText();
    value = Double.valueOf (inputString);

    if (arg.equals("Pound")) {
    try {
    displayTotal = 0.85 * value;
    pound = 0.85*number;
    number = Integer.parseInt(inputString);
    showStatus("");
    rateBox.setText("0.85");
    inputBox.setText("");
    ConvertedBox.setText(poundString);
    inputBox.setText("");

    } catch (NumberFormatException entry) {
    showStatus("Error: Invalid Input");
    inputBox.setText("");
    }

    } else if (arg.equals("Euro")) {
    try {
    inputString = inputBox.getText();
    number = Integer.parseInt(inputString);
    showStatus("");
    euro = number*pound;
    rateBox.setText("1.18");
    inputBox.setText("");
    ConvertedBox.setText(euroString);
    inputBox.setText("");

    } catch (NumberFormatException entry) {
    showStatus("Error: Invalid Input");
    inputBox.setText("");
    }
    } else if (arg.equals("Clear")) {
    try {
    inputBox.setText("");
    rateBox.setText("");
    ConvertedBox.setText("");
    } catch (NumberFormatException entry) {
    showStatus("Error: Invalid Input");
    inputBox.setText("");

    }
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,965
    Rep Power
    31

    Default Re: Help with currency applet

    Please edit your post and wrap the code in code tags to preserve its formatting and make it more readable.

    for some reason its not converting.
    How are you debugging the code? One way is to add some println statements to show what the code is doing. The print out will be shown on the browser's java console.
    Last edited by Norm; 05-27-2015 at 01:00 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tgg
    tgg is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

    Default Re: Help with currency applet

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap the code in code tags to preserve its formatting and make it more readable.


    How are you debugging the code? One way is to add some println statements to show what the code is doing. The print out will be shown on the browser's java console.
    Fixed it, I didn't assign the answer anywhere stupid me.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with currency applet

    You should still answer the question: how were you trying to debug the problem?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    tgg
    tgg is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

    Default Re: Help with currency applet

    Quote Originally Posted by gimbal2 View Post
    You should still answer the question: how were you trying to debug the problem?
    I have not covered any debugging methods yet in my course. I fixed the fault by trawling through the code.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with currency applet

    Quote Originally Posted by tgg View Post
    I fixed the fault by trawling through the code.
    What does that mean exactly, "trawling through the code"?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with currency applet

    Looking at the code and hoping to spot the mistake.
    tgg likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with currency applet

    I guess that like means that Tolls is correct. I hope that Norm's advice did not go on deaf ears, of course you don't find problems by staring at the code.

    I make a point of it since it is a common illness for novices to not want to add extra code to try and make problems visible.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    tgg
    tgg is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

    Default Re: Help with currency applet

    Quote Originally Posted by gimbal2 View Post
    I guess that like means that Tolls is correct. I hope that Norm's advice did not go on deaf ears, of course you don't find problems by staring at the code.

    I make a point of it since it is a common illness for novices to not want to add extra code to try and make problems visible.
    Thanks for your advice, I have only been coding since January so steep learning curve.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with currency applet

    Correct, so please do everything in your power to not frustrate yourself more than necessary :) It is at this point more in your interest to abuse our experience to help you to learn how to find and solve problems than to ask us to help you find a particular problem. Learn how to fish rather than ask for a fish, so to say.
    tgg likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with currency applet

    One of the reasons for not doing the "stare at the code and hoping to spot" is that as far as we're concerned we've written correct code...after all, why would we write incorrect code? That's just silly.

    Consequently we tend to be blind to the errors...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JFormattedTextField and Currency
    By Dcalladi in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2012, 09:00 PM
  2. java currency
    By alfarida in forum New To Java
    Replies: 4
    Last Post: 05-23-2011, 01:11 PM
  3. Currency Converter Applet Help...
    By MarkL410 in forum Java Applets
    Replies: 3
    Last Post: 11-09-2010, 05:27 PM
  4. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 07:23 AM
  5. Setting currency
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •