# "If statement" issue

• 04-12-2011, 04:03 PM
jdm113497
"If statement" issue
I am writing a program that determines the price to rent two different mopeds. The program will not compile and I'm not sure where to go from here.

The 50cc mopette is 15\$ for the first 3 hours, and \$2.50 an hour after the first 3 hours on a weekday. It is \$30 for the first 3 hourse and \$7.50 an hour after that on the weekend.

The 250cc mohawk is 25\$ for the first 3 hours, and \$3.50 an hour after the first 3 hours on a weekday. It is \$35 for the first 3 hourse and \$8.50 an hour after that on the weekend.

import java.text.*;
import java.util.*;
import java.lang.*;

class Ch5Pr21 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

double moped, days, time;

System.out.println("Choose your moped: Enter 1 for 50cc Mopette or 2 for 250 cc Mohawk.");
double moped = scanner.next( );

System.out.println("Weekday or weekend: Enter 1 for weekday or 2 for weekend.");
double days = scanner.next( );

System.out.println("How many hours?");
double time = scanner.next( );

if (moped = 1) {
if (days = 1) {
if (time <= 3)
System.out.println("Cost is \$15");
} else {
System.out.println("Cost is \$" + (15+((time-3)*2.5)));
}
} else {
if (time <= 3) {
System.out.println("Cost is \$30");
} else {
System.out.println("Cost is \$" + (30+((time-3)*7.5)));
}
} else {
if (days = 1) {
if (time <= 3)
System.out.println("Cost is \$25");
} else {
System.out.println("Cost is \$" + (25+((time-3)*3.5)));
}
} else {
if (time <= 3)
System.out.println("Cost is \$35");
} else {
System.out.println("Cost is \$" + (35+((time-3)*8.5)));
}
}
}
}
}
}
• 04-12-2011, 04:08 PM
dlorde
Please use CODE or PHP tags when posting code, so it is readable.

If you get a compiler error, please post the full compiler error message.

Java error messages usually tell you pretty much what is wrong and where.
• 04-12-2011, 04:15 PM
baf06
Code:

```        if (moped = 1) {             if (days = 1) {                 if (time <= 3)                     System.out.println("Cost is \$15");             } else {                 System.out.println("Cost is \$" + (15+((time-3)*2.5)));             }         } else {             if (time <= 3) {                 System.out.println("Cost is \$30");             } else {                 System.out.println("Cost is \$" + (30+((time-3)*7.5)));             }         } else {             if (days = 1) {                 if (time <= 3)                     System.out.println("Cost is \$25");             } else {                 System.out.println("Cost is \$" + (25+((time-3)*3.5)));             }         } else {             if (time <= 3)                 System.out.println("Cost is \$35");         } else {             System.out.println("Cost is \$" + (35+((time-3)*8.5)));         }```
first thing I noticed is that you have more than one else after the first main if, and that may never work, since an if takes one else only, if you want more than one else then you have to set a condition on every one until the last one when none of the conditions is satisfied.
And another thing is that you have about 3 extra } at the end of your code
• 04-12-2011, 04:29 PM
baf06

1) It seems that you have some problems if setting the braces in their right places, this what is causing the biggest trouble with the code.

2) your initialized the three double in the main and then you did that again in the body, and that makes an error as well
Quote:

double moped, days, time;
double moped = scanner.next( );
double days = scanner.next( );
double time = scanner.next( );
3) your set the doubles to read scanner.next() and that won't work since you have to specify that you will read a double by using scanner.nextDouble()

I fixed these three problem in my code and it just worked perfectly. Try with what I suggest and if you faced any problem understanding or fixing them I am glad to help :)
• 04-12-2011, 04:44 PM
jdm113497
I tried to make the changes. This is what I have now, but still a no-go.

Code:

```import java.text.*; import java.util.*; import java.lang.*; class Ch5Pr21 {         public static void main(String[] args) {                 Scanner scanner = new Scanner(System.in);                                 double moped, days, time;                                 System.out.println("Choose your moped: Enter 1 for 50cc Mopette or 2 for 250 cc Mohawk.");                 moped = scanner.nextdouble( );                                 System.out.println("Weekday or weekend: Enter 1 for weekday or 2 for weekend.");                 days = scanner.nextdouble( );                                 System.out.println("How many hours?");                 time = scanner.nextdouble( );                                 if (moped = 1) {                         if (days = 1) {                                 if (time <= 3) {                                         System.out.println("Cost is \$15");                                 } else {                                         System.out.println("Cost is \$" + (15+((time-3)*2.5)));                         } else if (time <= 3) {                                         System.out.println("Cost is \$30");                                 } else {                                         System.out.println("Cost is \$" + (30+((time-3)*7.5)));                 } else if (days = 1){                                 if (time <= 3)                                         System.out.println("Cost is \$25");                                 } else {                                         System.out.println("Cost is \$" + (25+((time-3)*3.5)));                         } else if (time <= 3){                                         System.out.println("Cost is \$35");                                 } else {                                         System.out.println("Cost is \$" + (35+((time-3)*8.5)));                                         }                                 }                                                 } }```
• 04-12-2011, 04:49 PM
baf06
that is a good change, but you have } missing now, and one thing is that in :
Quote:

else if (time <= 3) {
System.out.println("Cost is \$30");
You mean if (day = 1) is false then if (time <= 3) ??? I don't think is is what you mean, so change that to perform the right thing you want it to do.

Another HINT is that when you want to compare double int or float you should use double equal signs == and not = (= is for boolean us)
• 04-12-2011, 06:47 PM
jdm113497
If (day=1) and (time<= 3), the the price is set. How would you suggest I change it?
• 04-12-2011, 11:16 PM
Junky
Code:

`if (moped = 1) {`
That is an assignment and not a logical comparison.
• 04-12-2011, 11:58 PM
to_devendra
if (moped = 1) {
should be
if (moped == 1) {

And one } is missing
• 04-13-2011, 12:00 AM
to_devendra
if (moped = 1) {
should be
if (moped == 1) {

And one {is missing
• 04-13-2011, 12:39 AM
ra4king
Also, it is supposed to be "scanner.nextDouble()" ;)