• 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

calculater problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi I'm new to java and trying to build a calculator.
see my code please and tell me what is the problem?
yours
Tal


import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
int num1, num2, sum, multi, sub, div;
String input = scan.next();
char [] letter;
letter = input.toCharArray();
System.out.println("please enter a number:");
num1 = scan.nextInt();
System.out.println("add now another number:");
num2 = scan.nextInt();
System.out.println("choose:");
if (letter == s);
{
System.out.println("You Chose: sum");
sum = num1 + num2;
System.out.println("the outcome is: " + sum);
}
if (letter == m);
{
System.out.println("You Chose: multi");
multi = num1 * num2;
System.out.println("the outcome is: " + multi);
}
if (letter == b);
{
System.out.println("You Chose: sub");
sub = num1 - num2;
System.out.println("the outcome is: " + sub);
}
if (letter == d);
{
System.out.println("You Chose: divide");
div = num1 / num2;
System.out.println("the outcome is: " + div);
}

}

}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:I'm new to java and trying to build a calculator.
see my code please and tell me what is the problem?



It's probably better if you tell us what you think is the problem. For example...

Does it compile? If not, what is the compiler error?

Does it run correctly? If not, what are you seeing that is wrong? Is there an exception thrown? What is the stack trace? etc.


And what do you think is the problem? And why you come to this conclusion? etc. Basically, you can learn better, if you do most of the heavy lifting yourself.

Henry
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the error I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
s cannot be resolved to a variable
m cannot be resolved to a variable
b cannot be resolved to a variable
d cannot be resolved to a variable

at Main.main(Main.java:17)

I want to choose between different operations: sum, multiply, sub, divide.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:this is the error I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
s cannot be resolved to a variable
m cannot be resolved to a variable
b cannot be resolved to a variable
d cannot be resolved to a variable

at Main.main(Main.java:17)

I want to choose between different operations: sum, multiply, sub, divide.




Okay, you are using an IDE that allows you to run code that does not compile. Don't do that!! Always make sure that code compiles before you run it.

In fact, it may be a good idea to not use the IDE at all. The IDE does a lot of stuff for you, which will prevent you from learning about it.

Compile your code, and tell us what the compiler is complaining about.



But... to answer your question. You are using variables, specifically, s, m, b, and d, which you have not declare anywhere.

Henry
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm using eclipse...
it compile and run the program together!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:i'm using eclipse...



Like Henry, I would suggest you don't use an IDE until you are comfortable writing, compiling, and running code.

Tal Rofeh wrote:it compile and run the program together!


Well, not quite, it actually compiles shortly after you type or when you save (which you should do often). there will be console somewhere in the UI with errors and warnings, and the locations where the variables s, m, b, and d are used will be highlighted with a red underscore.

Regardless, what does this line of code do: if (letter == s)
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is what I'm trying to do:
typing to numbers and according to the letter I press it make a different action:
if I type "s" it will sum the 2 numbers, "m" will multiply them, etc.

how do I do that?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:this is what I'm trying to do:
typing to numbers and according to the letter I press it make a different action:
if I type "s" it will sum the 2 numbers, "m" will multiply them, etc.

how do I do that?


You aren't far off. The question remains: what does this line of code do: if (letter == s).
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the keyboard input equals "s" do:
System.out.println("You Chose: sum");
sum = num1 + num2;
System.out.println("the outcome is: " + sum);
if the keyboard input equals "m" do:
System.out.println("You Chose: multi");
multi = num1 * num2;
System.out.println("the outcome is: " + multi);

that what I meant when I wrote "letter == s".
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:if the keyboard input equals "s" do:
...
that what I meant when I wrote "letter == s".


The key there is if the keyboard input equals "s". What you are doing is if (letter == s). What is different in those two statements that would make the second one think s is a variable, while in the first statement it is 100% clear you mean the String "s"

Actually, it becomes easier if we UseCodeTags (<-link):


There will be a second portion to this answer once you solve this problem, as a sneak peak, read AvoidTheEqualityOperator.
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the keyboard input equals "s" - doesn't work for me.
I use eclipse program.
it gives me an error.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:while in the first statement it is 100% clear you mean the String "s"


@Tal: Or indeed, the character 's', which is what you'll need if you want to use '=='.
See the AvoidTheEqualityOperator (←click) page for details.

Winston
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:if the keyboard input equals "s" - doesn't work for me.
I use eclipse program.
it gives me an error.


Right, that isn't Java code, but there is something that make you, me, (and the compiler) know for absolute sure that you mean the String "s". And when you do if (letter == s) you don't use them, so the compiler thinks s is a variable.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:if the keyboard input equals "s" - doesn't work for me.
I use eclipse program.


No, you're using the Eclipse IDE to write Java code. And you've already told us this.

it gives me an error.


And you've already been given advice on how to fix it. I'm afraid you need to read the replies, because nobody here is simply going to supply you with code.
We require you to ShowSomeEffort (←click) here.

Winston
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Steve Luke wrote:while in the first statement it is 100% clear you mean the String "s"


@Tal: Or indeed, the character 's', which is what you'll need if you want to use '=='.
See the AvoidTheEqualityOperator (←click) page for details.

Winston


Just realized that letter is a character array. I assumed it was a String. Tal, consider either getting one character from the input to compare (as a character like Winston mentions above). See the API for String, (specifically the method charAt()) to see how to get just one character from the String.
 
Tal Rofeh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after some changes I get an initialize error on "smbd" (instead of "letter"):


import java.io.IOException;
import java.util.Scanner;
public class Main {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
int num1, num2, sum, multi, div, sub;
String smbd;
System.out.println("please enter a number:");
num1 = scan.nextInt();
System.out.println("add now another number:");
num2 = scan.nextInt();
System.out.println("choose:");
if (smbd == "s");
{
System.out.println("You Chose: sum");
sum = num1 + num2;
System.out.println("the outcome is: " + sum);
}
if (smbd == "m");
{
System.out.println("You Chose: multi");
multi = num1 * num2;
System.out.println("the outcome is: " + multi);
}
if (smbd == "b");
{
System.out.println("You Chose: sub");
sub = num1 - num2;
System.out.println("the outcome is: " + sub);
}
if (smbd == "d");
{
System.out.println("You Chose: divide");
div = num1 / num2;
System.out.println("the outcome is: " + div);
}

}

}
edited: smbd = scan.next(); - how to make that only when smbd = one of the "char"s it will make one of the things??
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tal Rofeh wrote:after some changes I get an initialize error on "smbd" (instead of "letter"):


Tal, it really helps if you read the replies you've been given:

if (smbd == "s");

is not only wrong, it is specifically what I warned you about: AvoidTheEqualityOperator.

Furthermore, the statement itself will not do what you want it to because you've written it wrong. I suggest you read the tutorials, rather than trying things out at random.

Winston
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like this
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic