GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2006, 05:24
JavaLost JavaLost is offline
New Member
 
Join Date: Oct 2006
Posts: 1
JavaLost is on a distinguished road

how to write a simple calculator code? Help!


Hey, guys,

I am now have to write a java programm to implement a simple calculator of basic operations(addition, subtraction, multiplication and division) on integers.

The user may enter a string comprising an operand, a binary operator, and a second operand, with any number of space characters between them. There may be leading spaces and trailing spaces in the input.

If an invalid input is encountered, a message "Error in expression" is displayed and the calculation is skipped. Otherwise, the respective operation is carried out, and the result displayed. The user then enters another string. To end the calculation, the user enters 'q'.

I also have to handle division-by-zero error so that your program does not crash when it happens.

The operand is an integer. Examples of valid operands are 23, 0, -123, but not 23.5 (not an integer), 51.0 (not an integer), +123 (unary plus not accepted).

Anyone has idea about how to write this program??

Below , i attached a partial code for this program. Hope someone can help me with this program. Thanks a lot!
Attached Files
File Type: txt calculator.txt (804 Bytes, 578 views)
  #2  
Old 11-Oct-2006, 06:29
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: how to write a simple calculator code? Help!


Senior members might not reply to this thread if you:
  • used "Please help", "C problem" or something equally vague as the title for your thread.
  • included example code in your message and not use the syntax highlighting [c] tags.
  • did not describe the problem accurately or include the error message (if any).
  • tried to get your homework assignment solved by someone else (and did not show your attempts at solving the problem)
The guidelines for posting can be found here.

We encourage you to post again once you've done that.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 24-Oct-2006, 01:41
Justin Fox Justin Fox is offline
Junior Member
 
Join Date: Sep 2006
Posts: 46
Justin Fox is on a distinguished road

Re: how to write a simple calculator code? Help!


i did a program almost exactly like this in my datastructures class...

except all we had to use is digits 0-9...

if i were you, i would definately use StringTokenizer...

you can set the delimiter to "+-*/"..


this mean when you call

JAVA Code:

String currToken = a.nextToken();

// where a is set up like so..

a = new StringTokenizer(userInput,"+-/*",true);


it not only accepts strings(substrings) separated by space characters,
but also any string separated by +,-,/ and *.

so what you can do is have a string that is called operators...

that is set up like so..

JAVA Code:

String operators = "+-/*";


and when you call StringTokenizer.nextToken() you know you're going
to get a integer or a operator(+.-,/,*).

so when you want to decide if it is a int or a operator try the following...

JAVA Code:

if(currToken.indexOf(operators) > -1) // index of return indexOf currToken
currToken = a.nextToken();              // relative to operators.
else                                              // if currToken isnt in operators, 
{                                                 // indexOf returns -1

// when you try to cast a string 
// to an integer, if the currToken isn't
// and int, it will throw an exception.

try{
  int a = Integer.parseInt(currToken);
    }
    catch(Exception e){System.out.println("invalid expression"); }

}


and if you have to paranthesis the expression....

just iterate through the string a see if the number of '(' and ')' are equal..

hope this stuff helps a lil...

the stringtokenizer should make it a lot easier on you...

and if you want to know if a certain char is a digit...

use Character.isDigit(char a);

Justin

to use StringTokenizer..

import java.util.StringTokenizer;
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Starting in the business? Time to write a Business Plan ! WEBDOMAIN.com Web Hosting Forum 2 01-Aug-2006 04:12
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
How to write simple program like ........? laputa9000 C++ Forum 3 29-Oct-2003 12:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.