Hi all. I'm a new user here. Some of you may have noticed a thread I started in C++ a few weeks ago. I'm actually taking C++ and java classes simultaneously. It might not have been a good idea, but I had no real choice. It's good immersion in programming!
I've written a homework program to simulate a simple calculator. It performs the four basic arithmetic operations. I'm having a couple of errors with my driver(?) file. My class definition file isn't returning any errors, so I'll assume it's fine unless you guys think it could be related to the errors in the other one. I'll just post the one for now. I marked the two error lines in red.
//Calculate Program
import java.util.Scanner;
public class Calculate
{
Calculator calc = new Calculator();// new Calculator
char selection;
char yesNo;
Scanner input = new Scanner(System.in);
public static void main(String args[])
{
//This is the priming question. It starts the calculator and
//should only run once.
System.out.printf("Do you want to perform a calculation now?\n");
System.out.printf("Select 'Y' for yes, or anything else for no.\n");
yesNo = input.nextChar();
//This while loop tests the initial answer. If user didn't enter 'Y'
//then loop and program should terminate
while(yesNo == 'Y' || yesNo == 'y')
{
//This section is where user selects type of operation
System.out.printf("What operation would you like to perform?\n");
System.out.printf("Enter '+' for add, '-' for subtract\n");
System.out.printf(" '*' for multiply '/' for divide\n");
selection = input.nextChar();
//This inner loop tests for operation type. User must enter valid
//response or loop won't end
while(selection != '+' && selection != '-' && selection != '*' && selection != '/' &&)
{
System.out.printf("You need to enter a correct character.+-*/\n");
selection = input.nextChar();
}
//At this point, a valid operation should have been entered. This
//section should call appropriate method for the operation
if(selection == '+')
calc.add();
if(selection == '-')
calc.sub();
if(selection == '*')
calc.mult();
if(selection == '/')
calc.div();
//This section is the end of the first while loop. It gives user
//the option to exit or perform another operation.
System.out.printf("Do you want to perform another calculation\n");
System.out.printf("Enter 'Y' for yes, or anything else for no.\n");
yesNo = input.nextChar();
}
}
}
Wow. The inline code tag seems to have cancelled all my spacing. I hope it's not too hard to read!
The first error is said to be on the line containing the start of my inner while loop. "illegal start of expression." My {}'s all seem to be in order.
The second error is said to be on the line containing my first method call "calc.add" The error message is " ')' expected" I'm suspecting that this one might dissappear if the first error is fixed.
Any tips to fix this you guys could provide would be greatly appreciated!
edit: Here's the class file.
import java.util.Scanner;
//Calculator Class File
public class Calculator
{
private double num1 = 0, num2 = 0, result = 0;
Scanner input = new Scanner(System.in);
public void add()
{
System.out.printf("Enter the first number. ");
num1 = input.nextDouble();
System.out.printf("\nEnter the number you're adding to it.");
num2 = input.nextDouble();
result = num1 + num2;
System.out.printf("\nThe result is " + result);
}
public void sub()
{
System.out.printf("Enter the first number. ");
num1 = input.nextDouble();
System.out.printf("\nEnter the number you're subtracting from it.");
num2 = input.nextDouble();
result = num1 - num2;
System.out.printf("\nThe result is " + result);
}
public void mult()
{
System.out.printf("Enter the first number. ");
num1 = input.nextDouble();
System.out.printf("\nEnter the number you're multiplying it by.");
num2 = input.nextDouble();
result = num1 * num2;
System.out.printf("\nThe result is " + result);
}
public void div()
{
System.out.printf("Enter the first number. ");
num1 = input.nextDouble();
System.out.printf("\nEnter the number you're dividing it by.");
num2 = input.nextDouble();
result = num1 + num2;
System.out.printf("\nThe result is " + result);
}
}