Results 1 to 4 of 4

Thread: Java Calculator

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

    Default Java Calculator

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


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

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



    <style type="text/css">
    body
    {
    font-family: Arial;
    }
    div.table
    {
    font-weight: bold;
    }
    legend
    {
    color: #0000ff;
    font-weight: bold;
    font-size: xx-large;
    }
    </style>

    <style type="text/javascript">

    function gettheresult()
    {

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


    }

    </style>

    </head>

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

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




    <td>
    <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>

    </select>

    <input type="reset">

    </td>

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


    </table>

    </div>


    </fieldset>

    </form>


    </body>




    </html>

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

    Default

    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 04:25 AM.
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

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

    Default

    Honestly correct!!! i agree...

    regards,
    sukatoa

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

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

Posting Permissions

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