• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's wrong in my control structures?  RSS feed

 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi JavaRanch, I'm new to your site but I would like to find some help with the Java programming problems that I'm frequently having trouble at. I still consider myself as novice in Java programming but continue to enhance my Java programming skills by reading Java programming books and researching on the internet. Now I hope you can help me with this: I have a program here that requires a mechanic to enter the tire pressure from four tires on any given car that comes into the garage. The program should first print out a menu for driving types: normal, hauling, and rugged. After the mechanic chooses a driving type, the program should ask for the four tire pressures with appropriate prompts. Finally, the program should tell the mechanic what adjustments to make. Assume the following:

For normal driving, all four tires should be inflated between 33 and 43 pounds per square inch (psi).
For hauling, the rear tires should be appropriately 10% greater.
For rugged, the rear tires should be approximately 15% greater.

For refer to my code below:



There are no compile errors on my code but what I wanted here is when I enter the correct tire pressure for any driving type, it should go straight to the respective else blocks displaying the message that the right tire pressure was entered. Please help me with the control structures of this code and by the way, this is not a homework because I'm already done with school last October 2013 and I'm just further enhancing my Java programming skills before I take the certification examination here in my country. And I hope people are nicer here unlike in other sites. Thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
Welcome to CodeRanch!

I added [ code ] tags to make the code easier to read, but there is still a lot of it. One technique to troubleshoot is to create a SSCCE. That way, it is easier to see where the problem lies.

For your question, which line of code is the programming running and which line are you expecting it to run?
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


public static void check_Pressure(double LFTire, double RFTire, double LRTire, double RRTire, int methodCode) throws IOException
{
switch(methodCode)
{
case 1:
if((LFTire < 33 || LFTire > 43) || (RFTire < 33 || RFTire > 43))
{
if((RRTire < 33 || RRTire > 43) || (LRTire < 33 || LRTire > 43))
{
System.out.println("Please adjust all four tires.");
System.out.println("For normal, all four tires should have a pressure between 33 and 43 pounds psi.");
calc_PressureNormal(LFTire, RFTire, LRTire, RRTire);
}
}
else
{
System.out.println("Thank you for entering the correct tire pressure.")
}

Is my control structure or decision-making structure here correct? Once I enter the correct tire pressure for all four tires, it should execute the else block and not execute those in the if block.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Daguman wrote:Is my control structure or decision-making structure here correct? Once I enter the correct tire pressure for all four tires, it should execute the else block and not execute those in the if block.

Well, for starters I think you're mixing up the business of input and calculation far too much. Separate the two processes completely, and don't call your calculation routine until you're absolutely sure you have four valid pressures. I think you'll find the code is a lot simpler then.

You might also want to look at the UserInput page. Data input is a notoriously fiddly procedure, and there's quite a lot to know about it.

HIH

Winston
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The small example works fine (except for a missing semi-colon which I assume is a copy/paste error. I defined the variables explicitly and it ran the else Can you add the printlns I added to see what the actual values are at the start of your method. I'm guessing they aren't what you expect.



Also, you might want to make a helper method such as


This lets you write the more readable
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!