• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trouble with operator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie for prompt reply !
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

try this code




Regards,
Moh Sak
 
edo travnik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic