2

After working on this code for a while and coming very close to the end, I've run into an unexpected problem. The if and else statements for choices 'B' and 'C' display nothing on the screen when they are typed in as user input. I've tried a few things but I cannot seem to figure it out.

import java.util.Scanner; public class Internet_Service_Provider { public static void main(String[] args) { double hours; char choice; Scanner input = new Scanner(System.in); System.out.println ("Enter the letter of your package: "); choice = input.nextLine().charAt(0); System.out.println ("Enter the number of hours used: "); hours = input.nextDouble(); double chargesa, chargesb, chargesc; chargesa = 9.95; chargesb = 13.95; chargesc = 19.95; double chargesa2, chargesb2; chargesa2 = (hours - 10) * 2; chargesb2 = (hours - 20); double extrafeesA, extrafeesB; extrafeesA = chargesa + chargesa2; extrafeesB = chargesb + chargesb2; if (choice == 'A') if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa); } else if (choice == 'A') if (hours > 10){ System.out.println ("your total charges are: " + extrafeesA); } else if (choice == 'B') if (hours <= 20) { System.out.println ("Your total charges are: " + chargesb); } else if (choice == 'B') if (hours > 20){ System.out.println ("Your total charges are: " + extrafeesB); } else if (choice == 'C'){ System.out.println ("Your total charges are: " + chargesc); } } } 

When the user types 'A' and then types the hours, the program runs perfectly and gives me the desired output. If typing 'B' or 'C' and then the hours, there is no output onto the screen. What is causing this?

-EDIT- I messed around with the code before checking the responses and discovered that if removing the else and creating this code:

 if (choice == 'A') if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa); } if (choice == 'A') if (hours > 10){ System.out.println ("your total charges are: " + extrafeesA); } if (choice == 'B') if (hours <= 20) { System.out.println ("Your total charges are: " + chargesb); } if (choice == 'B') if (hours > 20){ System.out.println ("Your total charges are: " + extrafeesB); } if (choice == 'C'){ System.out.println ("Your total charges are: " + chargesc);} 

... the program runs properly. I guess the else statements were unnecessary and caused the problem.

2
  • You can make it even clearer: instead of double checking if (choice == 'A') do it like this: if (choice == 'A'){ if (hours <= 10){ System.out.println ("Your total charges are: " + chargesa);} else{ /*here we already KNOW that choice is A and hours is >10!*/ System.out.println ("your total charges are: " +extrafeesA);}} Commented Oct 2, 2013 at 23:36
  • I see what you mean! Thanks for the tip. looks like I was underestimating the power of braces! Commented Oct 2, 2013 at 23:43

7 Answers 7

5
if (choice == 'A'){ if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa);} else if (choice == 'A') if (hours > 10){ System.out.println ("your total charges are: " + extrafeesA);} else if (choice == 'B') if (hours <= 20) { System.out.println ("Your total charges are: " + chargesb);} else if (choice == 'B') if (hours > 20){ System.out.println ("Your total charges are: " + extrafeesB);} else if (choice == 'C'){ System.out.println ("Your total charges are: " + chargesc);} } } 

Just adding some proper tabulation reveals, that you do not actually cover any other case than "A". Tabulation makes the code look much clearer, and therefore it is easier to find an error. You need to use your braces properly, as for now you only check if choice == 'A'. If it does not - your code doesn't do anything.

Sign up to request clarification or add additional context in comments.

Comments

3

You have if(choice == 'A') that is giving trouble you. Also check another braces that are not correct indented. It's difficult to read. Code should be readable by human beings

See:

if (choice == 'A')// this is giving trouble to you if (hours <= 10) { . . 

This code is equivalent to,(braces added)

if (choice == 'A'){ if (hours <= 10) { . . } 

So you have to remove it. What you want to do to be more clear is this. You can use switch

switch(choice){ case 'A': System.out.println ("Your total charges are: " + (hours <=10) ?chargesa:extrafeesA); break; case 'B': System.out.println ("Your total charges are: " + (hours <=10) ?chargesB:extrafeesB); break; case 'C': System.out.println ("Your total charges are: " + chargesc); break; } 

Comments

2

Try to use braces correctly :)

if (choice == 'A') { if (hours <= 10) System.out.println ("Your total charges are: " + chargesa); if (hours > 10) System.out.println ("your total charges are: " + extrafeesA); } else if (choice == 'B') { if (hours <= 20) System.out.println ("Your total charges are: " + chargesb); if (hours > 20) System.out.println ("Your total charges are: " + extrafeesB); } else if (choice == 'C') System.out.println ("Your total charges are: " + chargesc); 

Comments

2

You could simply use 'AND' in order to get your conditions work correctly.

if (choice == 'A' && hours <= 10) { System.out.println ("Your total charges are: " + chargesa); } else if (choice == 'A' && hours > 10) { System.out.println ("your total charges are: " + extrafeesA); } else if (choice == 'B' && hours <= 20) { System.out.println ("Your total charges are: " + chargesb); } else if (choice == 'B' && hours > 20) { System.out.println ("Your total charges are: " + extrafeesB); } else if (choice == 'C'){ System.out.println ("Your total charges are: " + chargesc); } 

Comments

0

change this:

if (choice == 'A') if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa);} 

to

if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa);} 

Comments

0
router.post('/login', (req, res)=>{ var user = req.body; query= "select email, password, status, role from user where email= ? "; connection.query(query, [user.email], (err, results)=>{ if(!err){ if(results.length<=0 || results[0].password != user.password){ return res.status(401).json({message:"username and password mis-match."}) } } else if(results[0].status === 'false'){ return res.status(401).json({message: "wait for admin approval. "}) } else if(results[0].password == user.password){ // const response = {email: results[0].email, role: results[0].role}; // const accessToken = jwt.sign(response, process.env.ACCESS_TOKEN, {expiresIn:'8h'}); // return res.status(200).json({token : accessToken}); return res.status(200).json({message : "user varyfied"}); } else{ return res.status(500).json({message:"something went wrong. Please try again. "}); } }) }) 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

From looking at it,

if (choice == 'A') if (hours <= 10) { 

The two are not indented, meaning if (hours <= 10) { will run regardless of the outcome of the first statement. So when the user types in B, and then the hours (less than 10), the first statement reads false, and the second reads true, therefore running that one.

Try putting {} around all the if statements and try running it again.

I may be incorrect though.

4 Comments

Although it does look like it's an if-statement scoping issue, indentation is not a part of it; Java does not care about whitespace like Python. However, proper bracing probably would fix the issue.
Also, your statement that if (hours <= 10) { will run regardless of the outcome of the first statement is incorrect...as you also note, if the user types B, then if (choice == 'A') { will return false, and nothing further will happen as that is the outermost conditional.
@knoight, the rest of it does not seem conditional to me, mind explaining how? And Dennis, it is indentation from the human view, when I look at it, I realised the first two statements are on the same indent, meaning the first one does not affect the second one's outcome.
what I mean is: If choice is not equal to 'A', then none of the other if statements will be evaluated. if (choice == 'A') is the first & outermost conditional statement. If that statement evaluates to false, as it would when choice == 'B', no other statements will be evaluated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.