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);
	}
	
}

Recommended Answers

All 16 Replies

If you wish to get help with your code you better provide full code not just one class. Calculator class is missing.
By-the-way to insert code into post do not use INLINECODE tag, but press hash sign "#" in the post bar or just simple type CODE tags and place your code between these tags

I edited my first post to include the Calculator class file.

Update in code marked with red colour. There is no method nextChar for Scanner !

//Calculate Program
import java.util.Scanner;

public class Calculate
{	
	public static void main(String args[])
	{
		Calculator calc = new Calculator();// new Calculator
	
		char selection;
		char yesNo;
		Scanner input = new Scanner(System.in);
		//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.nextLine().charAt(0);
		
		//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.nextLine().charAt(0);
			
			//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.nextLine().charAt(0);
			}
			
			//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.nextLine().charAt(0);
		}
	}
}

Oh wow. But there IS a method nextInt, nextDouble, etc., right? I changed the relevant lines and I'm still getting the same 2 error messages. I was wondering if nested while loops were even legal in java, but my books haven't said that, plus someone on here would probably have already caught it if that were my problem.

Thanks for helping and trying to help. I've been over and over my code. Why do errors have to usually make no sense? It's like there's some unwritten law!

I actually hadn't changed my while loop in my last post. How could I miss that extra && in the test so many times? Anyhow, I recompiled and the original errors were replaced with a stack of them saying "non-static variable input cannot be referenced from a static context." These correspond to each "input.nextLine().charAt(0);" line.

Interestingly enough, I get the same error message next to each of my four method calls. "non-static variable calc cannot be referenced from a static context." It seems to think that my calc instance of the Calculator class is a variable!

I'm going to refresh myself on static variables and hopefully the problem will be clarified. Thanks again!

I think I have the static problem solved. Apparently I needed to have the following block

Calculator calc = new Calculator();// new Calculator
char selection;
char yesNo;
Scanner input = new Scanner(System.in);

located inside the main method instead of before it. I was then able to compile both files with no errors showing. When I try to run it, however, I get a command window saying "no class definition" .. something or other. (Oh give me a break!) It can't just work, right?

Anyhow, I'm too burned out to continue tonight. I'll tackle it again tommorrow.

Man!!!!
Why so many posts about something to which you have solution in my post?

Man!!!!
Why so many posts about something to which you have solution in my post?

I seem to have pissed you off. I'm sorry for that, though I don't know how. You showed some changes to make. I made the changes. Now the old errors appear to have been fixed but were replaced with new errors. I managed to fix the new errors, and now a new one has appeared. That's the one I need to fix now. The program won't work until I do.

Read the error in its entirety, especially the name of the class it reports and the line number. Try to figure out why the compiler cannot see that class.

Read the error in its entirety, especially the name of the class it reports and the line number. Try to figure out why the compiler cannot see that class.

Now this is interesting. The error message appears to refer to a class definition for a class which doesn't exist. My classes are Calculator and CalculatorTest.

The error message is "Exception in thread "main" java.Lang.NoClassDefFoundError: Calculate" The error shows up in a command window, rather than at the bottom of the screen. (I'm using JECreator)

One of my classes was probably named "Calculate" at one point, but has long since been renamed. I did a text search in the code for "Calculate" and it only appeared in one spot, inside a comment. I wonder why it wouldn't have updated that that name was no longer relevant?

edit: I just checked the code as I quoted it at the beginning of this thread. A class was named Calculate at one point, but this was returning an error so I renamed it CalculatorTest. As far as I can tell, I updated all mentions of the old name to the new one. As I said, a text search didn't turn up any culprits. The reason for the change was because the class name didn't match the file name it was in. The original error message disappeared when I changed the name. So what could be holding out for the old name that didn't update during one of numerous compile attempts?

edit 2: There's no line # given for the error messsage since it's in a command window rather than the usual place.

I've not used JECreator, but did you perhaps set a main class for your project somewhere? A properties window or some such? It sounds as if it still thinks that is your main class.

I've not used JECreator, but did you perhaps set a main class for your project somewhere? A properties window or some such? It sounds as if it still thinks that is your main class.

I have a main method in my CalculatorTest file. It's the starting method for the program though, not a class. I haven't explicitly set any properties windows regarding this program. I'm a newb and wouldn't know how to, or why.

Calculator is the class with the arithmetic methods. An instance of it named 'calc' is instantiated by the CalculatorTest class. CalculatorTest also contains the main method which contains the algorithm for getting getting user selections until the user is done. Each selection calls the relevant Calculator method for +,-,*,/.

Both Calculator and CalculatorTest are in files of the same names. The CalculatorTest class was originally named Calculate, but the compiler apparently didn't like that, so I renamed it to match the file it was in. Now it still sees Calculate somewhere, apparently. But I sure can't find it!

I'm going to Email the files to my instructor. Maybe he can compile it himself and it will work, because his machine will lack whatevers holding out in mine.

Thank you for trying!

Just compile and run it from the command line.

Eeks. I'm not sure how to do that. I'll do some checking.

Try some of the things mentioned in this thread from JCreator forums:
http://www.jcreator.com/forums/lofiversion/index.php/t525.html

I think you just need to update the project properties.

That was it! The Project properties listed both the old name and the new name, but had the old name selected. I changed it and now the program runs fine. Thanks alot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.