Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

I am new to java programming and need some help here if you can. Else if statements are giving error  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the layout. I was not sure how to get the  line numbers  to  post. I am  trying to get it to do three different calculations. If an employee works less than 20 hrs. they receive 25% of the  tips. If they  work 20 -29.9 hours, they will receive 35% of the total tips. Finally if they  work 30-40 hrs., they will receive 40% of the  tip pool. They first calculation works for the 25%, but as soon as I added the other two conditions the program won't even compile. Any help will be very appreciated!
 
Rancher
Posts: 3314
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the program won't even compile.


Please copy the full text of the error message and paste it here. It has important info about the error.

Also please wrap posted code in code tags.  Select the code and press the [Code] button.
 
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed "Code" tags for you this time. To use the Code tags, first drag your cursor over the code (or any preformatted text) to highlight it, then click the Code button just above the edit window. To see if it worked correctly, click on the Preview button below the edit window.
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

the program won't even compile.


Please copy the full text of the error message and paste it here. It has important info about the error.

Also please wrap posted code in code tags.  Select the code and press the [Code] button.



Ok so first off thanks for responding. Here are the error messages.

C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:35: error: illegal start of type
       else if (hours > 20 && < 30){ <br /> C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:35: error: illegal start of expression <br />        else if (hours > 20 && < 30){ <br /> C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:35: error: ')' expected <br />        else if (hours > 20 && < 30){
C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:41: error: illegal start of type
          else {
C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:41: error: ';' expected
          else {
C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:43: error: <identifier> expected
                   answer = tips * .40;
C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:45: error: class, interface, or enum expected
               System.out.println("Your employee tip is " + answer);
C:\Users\JOSHUA\Documents\NetBeansProjects\TipCalculation\src\tipcalculation\TipCalculation.java:47: error: class, interface, or enum expected
   }

8 errors total
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What's supposed to happen if hours==20 ?



Sorry, the program should calculate for 35% of the total tip pool if it accepts an input of 20-29.9 hrs.
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What's wrong with this picture?

This is the first error reported. Often one error chain reacts generating a bunch of other errors so it's always best to start resolving them in order as show in the error messages. This often makes many of the other errors go away.
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joshua Braud wrote:

Carey Brown wrote:What's supposed to happen if hours==20 ?



Sorry, the program should calculate for 35% of the total tip pool if it accepts an input of 20-29.9 hrs.


That's not what your logic says.
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
What's wrong with this picture?

This is the first error reported. Often one error chain reacts generating a bunch of other errors so it's always best to start resolving them in order as show in the error messages. This often makes many of the other errors go away.



I saw it as soon as I read your initial post asking what it is supposed to do if ==20.
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I  changed it to  this:

 

still getting errors
 
Norm Radder
Rancher
Posts: 3314
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

still getting errors


A comparison operator requires two operands:  operand1 operator operand2  For example  A > b
Also a boolean operator requires two operands:   expression1 operator expression2  For example  A > b && d < 3
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

still getting errors


A comparison operator requires two operands:  operand1 operator operand2  For example  A > b
Also a boolean operator requires two operands:   expression1 operator expression2  For example  A > b && d < 3



You sir are a gentleman and a scholar! One word changed the whole program. It compiled in 1 second.
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now everything is compiling easy and calculating correctly. For some reason it is printing the answer double for the if and the  else if code blocks. The else statement works and prints fine. What am I  missing?
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to repost your code in the current state.

You declare hours as an double but only assign an int to it.

If you still have if( hours==20 && ... ), then it still has problems
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could simplify it to
       if (hours < 20) {
...
         }  
       else if (hours < 30){
...        
       }
           
          else {      
...
               }

 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I made it a double and not an int is because employees may clock in late or early giving the time clock decimals. If 15 mins. late. it will show .25 etc. But everything is calculating perfectly, but for some reason it is printing in double for the if and else if sections.
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the tip pool total?
500
How many hours did the employee work?
15.5
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at tipcalculation.TipCalculation.main(TipCalculation.java:27)
C:\Users\JOSHUA\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 14 seconds)

What in the  world! Glad you're here lol. I am new, so be gentle
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You might want to repost your code in the current state.

You declare hours as an double but only assign an int to it.

If you still have if( hours==20 && ... ), then it still has problems



p
 
Norm Radder
Rancher
Posts: 3314
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting needs some work.
The closing bracket on line 27 should be in line below the bracket on line 22.
line 28 also needs adjustment.

Otherwise the statements on lines 24 to 28 appear within the else on line 19
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it now thank  you  so  much!
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What's wrong with this picture?

What data type is "hours"?
 
Carey Brown
Saloon Keeper
Posts: 5140
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What if hours is 25?






run:
What was the tip pool total?
500
How many hours did the employee work?
25
Your employee tip is 175.0
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for responding and helping me out. Ill get it the more I do it and I enjoy this more and more each time. I will return in the future here first if I ever need help and will be telling others who may need it!    
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.PrintStream;
import java.util.Scanner;

public class TipCalculation {

 /**
  * @param args the command line arguments
  */
 public static void main( String[] args ) {
   double hours;
   double tips = 0;
   double answer = 0;

   Scanner input = new Scanner( System.in );


   System.out.println( "What was the tip pool total?" );
   // tips = input.nextInt();
   tips = input.nextDouble(); // I think data type must match with declaration

   System.out.println( "How many hours did the employee work?" );
   // hours = input.nextInt();
   hours = input.nextDouble(); // I think data type must match with declaration

   if ( hours < 20 ) {
     answer = tips * .25;
     System.out.println( "Your employee tip is " + answer );

   } else if ( hours > 20  || hours < 30 ) { // prefix hours before < 30
     answer = tips * .35;
     System.out.println( "Your employee tip is " + answer );

   } else {
     answer = tips * .40;
     System.out.println( "Your employee tip is " + answer );// Add here the below statement
   }
   // System.out.println("Your employee tip is " + answer);// Not here

 }
}
 
Marshal
Posts: 61718
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You didn't uite get the code tags right: more details here. Briefly, the [code=java] part goes first, in which case it looks like this:-Thank you for the code, but there is a serious logic error in it. That has actually been mentioned earlier in this discussion. It is also not good practice to have more than one statement in the main() method. It would appear you haven't been taught object‑oriented code yet.
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This is after I was told about the data types. I did fix it there, just had to leave to get my kids and never reposted the revised code. This is it. And no I  am still a baby when it comes to coding as Ive stated in the  beginning. I appreciate all of your advice though  
I wasn't aware of the multi-statement in the  main() method practice. Could you please explain? Thanks
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Asraful thanks for responding to help   When I move the printout statement as you suggested, It does NOT print out the solution. If I leave it the way it is, it calculates and prints out.
 
Norm Radder
Rancher
Posts: 3314
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code's formatting still needs fixing to make it easier to see the logic:

The formatting should be:
 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joshua Braud wrote: I wasn't aware of the multi-statement in the  main() method practice. Could you please explain? Thanks


Campbell already gave you the link, but here it is again: MainIsAPain
 
Joshua Braud
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!