aspose file tools*
The moose likes Beginning Java and the fly likes trouble with operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with operator " Watch "trouble with operator " New topic
Author

trouble with operator

edo travnik
Greenhorn

Joined: Jun 18, 2006
Posts: 7
Hello,
I am playng with a simple java program which when started the user is asked to enter a number than an arthimetic operator +,-,*,/ than again a number
and at the and to display the result depending on the operator used in the process...
I managed to do this but only when the operator is hardcoded :


import java.io.*;
public class kalkulacijaZbirDvaBroja
{


public static void main(String[] args)throws IOException
{//citanje i unos prvog broja

BufferedReader stdin = new BufferedReader (new
InputStreamReader(System.in));
String first, second;
int firstNum, secNum, sum;


//Citanje prvog korismickog unosa
System.out.print("Unesi prvi broj :");
first = stdin.readLine();
firstNum = Integer.parseInt(first);







//Citanje drugog korisnickog unosa
System.out.print("Unesi drugi broj :");
second = stdin.readLine();
secNum = Integer.parseInt(second);


//Kalkulacija unesenih brojeva

sum = firstNum + secNum;
System.out.println("rezultat je :" + sum);


}

}


Thank you !
edo
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Dobrodosli v Ranchu. That more-or-less exhausts my Eastern European language skills.

Somebody else on the Ranch has a similar task here.

You know you can read in the operator as '+' '-' '*' '/' or '%'? Use the same technique you have used to read a number. That gives you a String. See whether you can find any way of getting the first character of a String (look in the String class in the API specifications.
When you have the character into "char" format, see whether you can work out how to use it so you get first + second or first - second, etc.

CR
edo travnik
Greenhorn

Joined: Jun 18, 2006
Posts: 7
Thank you Ritchie for prompt reply !
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

"edo edo",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
I trust you got it to work? Pleased to be able to help, but Bear Bibeault is right, you do need to correct your displayed name.
edo travnik
Greenhorn

Joined: Jun 18, 2006
Posts: 7
Hello,
I updated my profile !
By the way, I am still having trouble with the proper formating characters from the strings...
I hope someone can help !?

regards,
edo
moh sak
Ranch Hand

Joined: May 11, 2005
Posts: 78
Hi,
when you read the operator check it like this :



hope i get the question you are trying to ask and hope this code will help you.

regards,
Moh Sak
edo travnik
Greenhorn

Joined: Jun 18, 2006
Posts: 7
Hi Sak,
here is a code that when executed a user enters a number than another number and at the end it gets result based on the operator which is hard coded.
I would like to edit code in a way that user would be able to enter an operator ( + , - , * , / )and get the result based on the operator he or she entered...


public static void main(String[] args)throws IOException

{

//create a text input stream
BufferedReader stdin = new BufferedReader (new
InputStreamReader (System.in));
String first,second;
int firstNum, secNum, sum;

// retreive two numbers from the user
System.out.print("Enter first number: ");
first = stdin.readLine();
firstNum = Integer.parseInt(first);


System.out.print("Enter second number:");
second = stdin.readLine();
secNum = Integer.parseInt(second);

//Izracunaj i pokazi rezultat
sum = (firstNum + secNum);
System.out.println("Result is :" + sum);

}
moh sak
Ranch Hand

Joined: May 11, 2005
Posts: 78
Hi,

try this code




Regards,
Moh Sak
edo travnik
Greenhorn

Joined: Jun 18, 2006
Posts: 7
Hi Sak,
thank you very much the code works well and it gave me a great idea how I can approach my problem !
regards,
edo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with operator
 
Similar Threads
New to Java and need help
while loop
whats wrong with my code
Problem with JCreator LE
Can't understand complier error