PDA

View Full Version : Java Help - Very Basic Calculator



Sax
08-12-2013, 07:54 PM
So I have been taking some video lessons on Java programming and after watching a video on doing a very basic calculator, as I have some good knowledge of scripting for simba bots myself I though I would give it a try and try to make it to be capable of doing more operations and not just adding.

Although I get some errors and need someone to help me out here. So if you have time I appreciate.

My code:


package bucky;

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner bucky = new Scanner(System.in);
double num1, num2, answer;
String add;
String sub;
int operation;
add = "+";
sub = "-";

System.out.println("Choose your operation: + or - ");
if (add = bucky.nextLine("+")){
operation = 1;
}
if (sub = bucky.nextLine()){
operation = 2;
}
System.out.println("Enter first number: ");
num1 = bucky.nextDouble();
System.out.println("Enter second number: ");
num2 = bucky.nextDouble();
if (operation == 1){
answer = num1 + num2;
}
if (operation == 2){
answer = num1 - num2;
}
System.out.println("Your answer is: " + answer);
}
}




Errors I got:


Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method nextLine() in the type Scanner is not applicable for the arguments (String)
Type mismatch: cannot convert from String to boolean

at bucky.apples.main(apples.java:16)



If you are willing to help me please can you tell me what I am doing wrong, and give me some extra tips on that? :D


Thanks in advance! :)

Brandon
08-12-2013, 08:16 PM
If statements require a boolean result within their parameters..

You have:


if (add = bucky.nextLine("+"))...


This cannot be. The result of the asignment operator is still a string. To get a boolean, you need a comparison operator such as:

==, ||, &&, !=, ===, etc..

So perhaps:


if ((add = bucky.nextLine("+")) != null)...


Same for the other if statements. :S Still confused as to why ppl use the scanner in this day and age.

Hoodz
08-12-2013, 08:28 PM
You are following the boston tutorials right?

Sin
08-12-2013, 09:23 PM
Ofcourse ^ :P

riwu
08-13-2013, 09:47 AM
For your first error


The method nextLine() in the type Scanner is not applicable for the arguments (String)

This occurs because Scanner.nextLine() has no parameter.

If you are attempting to compare the value of add and Bucky.nextLine():

add.equals(Bucky.nextLine())

Also it may be better to use a switch block for the operation control flow.

Sax
08-14-2013, 03:03 PM
Thanks for the replies Brandon and riwu

In here:

if (add = bucky.nextLine("+")){
operation = 1;

I don't want to assign add to bucky.nextLine("+").
I assigned add to be the signal + , and I want to know if it finds the + symbol in the line the user will imput. if it will then operation will be 1 (the add operation).
You see the user will input + or - according to the operation he wants and then if he wrote + then the operation he wants is the operation 1 (add).
How could I compare the add string (+) to the symbol it finds on the user imput line?

Sorry for my ignorance on this matters and thank you for your patience.

riwu
08-15-2013, 02:11 PM
Thanks for the replies Brandon and riwu

In here:

if (add = bucky.nextLine("+")){
operation = 1;

I don't want to assign add to bucky.nextLine("+").
I assigned add to be the signal + , and I want to know if it finds the + symbol in the line the user will imput. if it will then operation will be 1 (the add operation).
You see the user will input + or - according to the operation he wants and then if he wrote + then the operation he wants is the operation 1 (add).
How could I compare the add string (+) to the symbol it finds on the user imput line?

Sorry for my ignorance on this matters and thank you for your patience.
Gave u the solution in prev post:


add.equals(Bucky.nextLine()) //returns true if add contains the same string literal as Bucky.nextLine()


As Brandon mentioned, '=' is for assignment in java (equivalent to ':=' in Pascal). '==' is for equality comparison in Java (equivalent to '=' in Pascal), although using the equals() function for comparing objects is a better practice as using '==' returns true only if the objects point to the same memory address.
Eg.


String a = new String("a");
String b = new String("a");
System.out.print(a == b); //returns false even though they have the same string literal as a and b are 2 distinct objects stored in distinct locations in the memory
System.out.print(a.equals(b)); //returns true as equals() has been overridden to compare the wrappers' string literal

Although in cases where a String object is not created through its constructor '==' would work due to the String pool feature of Java.

Sax
08-16-2013, 08:47 PM
Gave u the solution in prev post:


add.equals(Bucky.nextLine()) //returns true if add contains the same string literal as Bucky.nextLine()


As Brandon mentioned, '=' is for assignment in java (equivalent to ':=' in Pascal). '==' is for equality comparison in Java (equivalent to '=' in Pascal), although using the equals() function for comparing objects is a better practice as using '==' returns true only if the objects point to the same memory address.
Eg.


String a = new String("a");
String b = new String("a");
System.out.print(a == b); //returns false even though they have the same string literal as a and b are 2 distinct objects stored in distinct locations in the memory
System.out.print(a.equals(b)); //returns true as equals() has been overridden to compare the wrappers' string literal

Although in cases where a String object is not created through its constructor '==' would work due to the String pool feature of Java.

Thank you but now I got this errors:


The local variable operation may not have been initialized
The local variable operation may not have been initialized
The local variable answer may not have been initialized

Any idea what it may be causing this error?

Again thank you for helping me


The code:


package bucky;

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner bucky = new Scanner(System.in);
double num1, num2, answer;
String add;
String sub;
int operation;
add = "+";
sub = "-";

System.out.println("Choose your operation: + or - ");
if (add.equals(bucky.nextLine())){
operation = 1;
}
if (sub.equals(bucky.nextLine())){
operation = 2;
}
System.out.println("Enter first number: ");
num1 = bucky.nextDouble();
System.out.println("Enter second number: ");
num2 = bucky.nextDouble();
if (operation == 1){
answer = num1 + num2;
}
if (operation == 2){
answer = num1 - num2;
}
System.out.println("Your answer is: " + answer);
}
}

Brandon
08-16-2013, 10:49 PM
Thank you but now I got this errors:


The local variable operation may not have been initialized
The local variable operation may not have been initialized
The local variable answer may not have been initialized

Any idea what it may be causing this error?

Again thank you for helping me



package bucky;

import java.util.Scanner;

class apples{
public static void main(String args[]) {
Scanner bucky = new Scanner(System.in);
double answer = 0;
int operation = 0;

System.out.println("Choose your operation: + or - ");

switch(bucky.nextLine()) {
case "+": operation = 1; break;
case "-": operation = 2; break;
default: throw new Exception("Invalid Operation"); break;
}

System.out.println("Enter first number: ");
double num1 = bucky.nextDouble();

System.out.println("Enter second number: ");
double num2 = bucky.nextDouble();

switch(operation) {
case 1: answer = num1 + num2; break;
case 2: answer = num1 - num2; break;
}
System.out.println("Your answer is: " + answer);
}



Initializing variables are sometimes important and usually recommended so just create them on the fly and init them with the value of w/e..

Another thing is you have branching in your program.. If statements like yours need else statements..


if (somecondition) {
} else if (nextcondition) {
} else {
}


Don't keep doing if, if, if, if.. It ends up checking every if statement and not all compilers are going to optimize that out.. In some cases, you need if, if, if.. however, in this case you don't.


For faster responses, wrap your code in java tags. You can do so by doing:





//Put code here..

Sax
08-22-2013, 10:30 PM
package bucky;

import java.util.Scanner;

class apples{
public static void main(String args[]) {
Scanner bucky = new Scanner(System.in);
double answer = 0;
int operation = 0;

System.out.println("Choose your operation: + or - ");

switch(bucky.nextLine()) {
case "+": operation = 1; break;
case "-": operation = 2; break;
default: throw new Exception("Invalid Operation"); break;
}

System.out.println("Enter first number: ");
double num1 = bucky.nextDouble();

System.out.println("Enter second number: ");
double num2 = bucky.nextDouble();

switch(operation) {
case 1: answer = num1 + num2; break;
case 2: answer = num1 - num2; break;
}
System.out.println("Your answer is: " + answer);
}



Initializing variables are sometimes important and usually recommended so just create them on the fly and init them with the value of w/e..

Another thing is you have branching in your program.. If statements like yours need else statements..


if (somecondition) {
} else if (nextcondition) {
} else {
}


Don't keep doing if, if, if, if.. It ends up checking every if statement and not all compilers are going to optimize that out.. In some cases, you need if, if, if.. however, in this case you don't.


For faster responses, wrap your code in java tags. You can do so by doing:





//Put code here..




Thanks a lot. That switch statement is very helpful. Thank you for your time :D