Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble with operator

 
edo travnik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ritchie for prompt reply !
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

try this code




Regards,
Moh Sak
 
edo travnik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic