jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Else without if error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Else without if error" Watch "Else without if error" New topic
Author

Else without if error

Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
I am getting the following error: c:\Projects>javac PayrollProgram2.java
PayrollProgram2.java:30: 'else' without 'if'
else

This payroll program has to be modified so it continues to request employee information until the user enters stop as the employee name. I also have to program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.



I just do not understand why I am getting this error. Can you please help?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

You have an extra semi-colon on line 24.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
When I remove the semi colon from line 24, I now have 8 errors, which are the following:

PayrollProgram2.java:20: package system does not exist
system.out.println ( "Enter the employee's full name: " ); // prompt
^
PayrollProgram2.java:22: cannot find symbol
symbol : variable Payroll
location: class PayrollProgram2
Payroll.setEmployeeName( theName ); // set the employee name
^
PayrollProgram2.java:26: package system does not exist
system.out.println( "Enter total hours employee worked: " ); // prompt
^
PayrollProgram2.java:31: package system does not exist
system.out.println( "Re-enter employee's full name: "); //prompt
^
PayrollProgram2.java:35: package system does not exist
system.out.println( "Enter hourly pay rate: " ); // prompt
^
PayrollProgram2.java:40: package system does not exist
system.out.println( "Please enter a positive number: ");// prompt
^
PayrollProgram2.java:47: package system does not exist
system.out.println( "Employee Name:,Weekly pay is $ %d\n", sum );
^
PayrollProgram2.java:53: package system does not exist
system.out.println("Enter a positive number: ");// prompt
^
8 errors
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kevin:

You may be looking for the System class. Remember that in Java case matters.

John.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

When I remove the semi colon from line 24, I now have 8 errors, which are the following:

PayrollProgram2.java:20: package system does not exist


Also, this isn't a case of -- you cause those errors when you removed the semicolon. What happened is that the semicolon cause an error that obfuscated the other errors. So, when you fixed the semicolon error, the other errors showed up.

Fixing the semicolon was correct. Now you need to fix the rest.

Henry
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
Now I am getting more errors:

PayrollProgram2.java:22: cannot find symbol
symbol : variable Payroll
location: class PayrollProgram2
Payroll.setEmployeeName( theName ); // set the employee name
^
PayrollProgram2.java:47: cannot find symbol
symbol : method println(java.lang.String,int)
location: class java.io.PrintStream
System.out.println( "Employee Name:,Weekly pay is $ %d\n", sum );
^
2 errors

Am I putting in the wrong symbols here? My text book seems the same.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kevin:

For the second error, the println() method does not behave like C/C++'s printf(). Check the API for PrintStream (System.out is an object of class PrintStream) for a very similar method. For the first error, you may need to do an import, or check the method signature to get that to work.

John.
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
I am not sure what I am supposed to do here.

For the first error, what am I supposed to import?

For the second error, I am not following what you are saying.

I am assuming by the way you are responding that this is not a simple problem?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

For the first error, what am I supposed to import?


You have to tell us. The compiler doesn't know what to do with "Payroll". What is that? Is it a variable? If it is, then you need to declare it somewhere. Is it another class? If it is, then you have to import it. We can't tell you exactly what to do, since we don't know what you are trying to use.


For the second error, I am not following what you are saying.


Basically, there is no such a thing as a println() method that takes a string and an int. Perhaps you want to use the printf() method?

Henry
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Kevin Quarles wrote:I am not sure what I am supposed to do here.
For the first error, what am I supposed to import?
For the second error, I am not following what you are saying.
I am assuming by the way you are responding that this is not a simple problem?


If you wrote the code, I suggest writing a little at a time and resolving errors as you see them. That's better than typing a lot of code, and dealing with many errors at a time. Whether or not you wrote it, it looks like you should start out with something simpler.

As far as your errors go, just take them one at a time. For example, "cannot find symbol... Payroll" means the compiler encountered the symbol "Payroll" but couldn't find where it was declared. What is Payroll? If it's another class you've defined, you either need to qualify that reference with its package name, or use the "import" keyword and specify the package there. The "package system does not exist" is probably caused by the dot notation in "system.out.println..." -- since there is no variable "system" in scope, I guess it's trying to interpret that as a package name. You probably mean to use "System.out.println..." -- "out" is a public field defined in class System, and its data type has a println() method.

But again, if you're trying to learn Java, I'd start with a simple "Hello World" example, and go from there.
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
If I do an import, that would not be on that line, right?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Kevin Quarles wrote:If I do an import, that would not be on that line, right?

I'm not sure what you're asking here.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kevin:

Look at line 4 of the code you posted. That should help with import placement.

John.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

The first time the word "Payroll" appears in your program (and not as a comment) is on line 24. You have not declared it as a variable and it is not part of the Scanner class (which you DID import)...So the compiler has NO IDEA what that is. It can't figure it out.

Neither can we.

If this is a PayrollProgram2 , do you have a working PayrollProgram1 you could reference to see how it got a Payroll object?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Else without if error