1. Member
Join Date
Mar 2011
Posts
7
Rep Power
0

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

2. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
10
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.

3. Member
Join Date
Jan 2011
Location
Beirut, Lebanon
Posts
90
Rep Power
0
Java 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

4. Member
Join Date
Jan 2011
Location
Beirut, Lebanon
Posts
90
Rep Power
0

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
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 :)

5. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
I tried to make the changes. This is what I have now, but still a no-go.

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

}
}```

6. Member
Join Date
Jan 2011
Location
Beirut, Lebanon
Posts
90
Rep Power
0
that is a good change, but you have } missing now, and one thing is that in :
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)

7. Member
Join Date
Mar 2011
Posts
7
Rep Power
0
If (day=1) and (time<= 3), the the price is set. How would you suggest I change it?

8. Java Code:
`if (moped = 1) {`
That is an assignment and not a logical comparison.

9. Member
Join Date
Apr 2011
Posts
7
Rep Power
0
if (moped = 1) {
should be
if (moped == 1) {

And one } is missing

10. Member
Join Date
Apr 2011
Posts
7
Rep Power
0
if (moped = 1) {
should be
if (moped == 1) {

And one {is missing

11. Also, it is supposed to be "scanner.nextDouble()" ;)