Results 1 to 4 of 4

Thread: Java Calculator

  1. #1
    aapanju is offline Member
    Join Date
    Apr 2008
    Rep Power

    Default Java Calculator

    Can any 1 get this to work and tell me where I am going wrong ... kauz now im totally lost.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>My Web Calculator</title>

    <style type="text/css">
    font-family: Arial;
    font-weight: bold;
    color: #0000ff;
    font-weight: bold;
    font-size: xx-large;

    <style type="text/javascript">

    function gettheresult()

    var FirstNumber = document.getElementById("firstnumber").value;
    var SecondNumber = document.getElementById("secondnumber").value;
    var Option = document.getElementById("pick").value;




    <fieldset title="web calculator">
    <legend>My Calculator</legend>

    <div class="table">
    <td>First Number:</td>
    <td><input type="text" size="10" name="first" id="firstnumber" /></td>
    <br />
    <td>Second Number:</td>
    <td><input type="text" size="10" name="second" id="secondnumber" /></td>
    <br />
    <td><input type="text" size="10" name="result" id="resultofboth" /></td>
    <br />

    <select id="pick">
    <option value="+">ADD</option>

    <option value="firstnumber-secondnumber">SUBTRACT</option>

    <option value="firstnumber*secondnumber">MULTIPLY</option>

    <option value="firstnumber/secondnumber">DIVIDE</option>


    <input type="reset">


    <td><button onclick="theresult">Calculate</button></td>







  2. #2
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Rep Power


    OK, first off, this is a Java forum, not a JavaScript forum. There is a huge difference! Java vs. JavaScript

    Second, you want to use the "script" tag, not the "style" tag.
    Third, your button isn't calling the "gettheresult()" function.
    Fourth, you can't preform the operation while setting it equal to Option.
    Fifth, your trying to add two strings, that is "6"+"4" will equal 64.
    Sixth, you haven't told javascript to put the answer in the result box.
    Seventh, even if you do get the result to display in the box, the way you are scripting this, the page will simply refresh and the result will be gone.

    As far as what you did wrong, that is about it...

    What you should do, is use a form to submit the data to javascript. You can then use javascript to stop the page from reloading.

    Change your pick options to a simple word, letter, or symbol to represent the option. Then use an ifelse statement to figure out the operation.

    Then use the pasreInt() function to to convert your string value into an integer value.

    Preform the operation, and place the result in the result box.

    Not to hard, your just going at it wrong... :D
    Last edited by bobleny; 04-17-2008 at 05:25 AM.
    -- --
    Cheer up, the worst has yet to come...

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Cebu City, Philippines
    Rep Power


    Honestly correct!!! i agree...


  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    Quote Originally Posted by bobleny View Post
    OK, first off, this is a Java forum, not a JavaScript forum. There is a huge difference! Java vs. JavaScript
    Thanks for the link bobleny. Lots of people really mess-up with Java and JavaScript. Since there is a word Java, they think both are same. :)

Similar Threads

  1. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  2. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 04:27 PM
  3. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:01 AM
  4. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 12:07 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