Im getting "Variable rr may not have been initialized" with it pointing at one of my if statements at the very bottom.
This was the problem:Code:import java.util.Scanner; class TirePressure2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rf; //right front int lf; //left front int rr; //right rear int lr; //left rear int gi; //interger for good inflation. Either 0 or 1 int gf; //good front inflation int gr; //good rear inflation System.out.println("Enter right front tire pressure:"); rf = scan.nextInt(); if (rf > 45 || rf < 35) { gi = 0; System.out.println("Bad right front tire pressure"); System.out.println("Enter left front tire pressure:"); lf = scan.nextInt(); if (lf > 45 || lf < 35) { gi = 0; System.out.println("Bad left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } } } else { gi = 1; System.out.println("Good right front tire pressure"); System.out.println("Enter left front tire pressure:"); lf = scan.nextInt(); if (lf > 45 || lf < 35) { gi = 0; System.out.println("Bad left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } } else { gi = 1; System.out.println("Good left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } else { gi = 1; System.out.println("Good right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } else { gi = 1; System.out.println("Good left rear tire pressure"); } } } } if (rf == lf) { gf = 1; } else { gf = 0; } if (rr == lr) { gr = 1; } else { gr = 0; } if (gi == 0) { System.out.println("Bad inflation!"); } else { if (gf == 1) { if (gr == 1) { System.out.println("Good inflation!"); } else { System.out.println("Check rear tire pressures, not equal!"); } } else { System.out.println("Check front tire pressures, not equal!"); } } } }
And this was exercise one, if you need it:Its not enough that the pressures are the same in the tires, but the pressures must also be within range. Modify the program in exercise 1 so that it also checks that each tire has a pressure between 35 and 45. If a tire is out of range, write out an error message immediately, but continue inputting values and processing them:
Input right front pressure
32
Warning: pressure is out of range
Input left front pressure
32
Warning: pressure is out of range
Input right rear pressure
42
Input left rear pressure
42
Inflation is BAD
If there have been any warnings, write out a final error message. (To do this, declare a boolean variable goodPressure that is initialized to true but is changed to false when an out of range tire is first found.)
I feel like I'm making this problem way longer and way more complicated than it needs to be, any tips?The front tires of a car should both have the same pressure. Also, the rear tires of a car should both have the same pressure (but not neccessarily the same pressure as the front tires.) Write a program that reads in the pressure of the four tires and writes a message that says if the inflation is OK or not.
Input right front pressure
38
Input left front pressure
38
Input right rear pressure
42
Input left rear pressure
42
Inflation is OK
Results 1 to 16 of 16
Thread: One last java question
- 25 Nov. 2012 07:17pm #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.00
One last java question
- 25 Nov. 2012 07:30pm #2
My lord. This is your biggest serving of spaghetti code yet. Design patterns man, find them and use them. This being Java isn't exactly helping your case or making it any more compelling to help you either.
I'll let Artificial handle your homework this time, lol. Maybe he could even give you a few pointers on code quality as well.
Good luck.
- 25 Nov. 2012 07:52pm #3
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.00
I know, the moment I started writing this I knew it would end up a giant pile of crap. I know that there's some way of making this more streamlined and way less lines, but I got too lazy to figure it out.
So, right now I'm looking for libraries to interact with the web (HTTP POST and GET and stuff) using Java, but should I just stop and learn python and then try it with python instead? Right now I'm using the Java IDE BlueJ, and I'm sucking at adding libraries to it. I have absolutely no idea how. I'm trying to add in Apache's HTTP Components to my project to interact with the web and stuff.Last edited by 323; 25 Nov. 2012 at 07:54pm.
- 25 Nov. 2012 10:34pm #4
For starters, learn how to use abstractions. It would be counter-intuitive not to use them especially when you're using a heavily OO language like Java.
You could also try condensing some of the extraneous code like the excess 'if' statements and replace them with switch/case or if-elseif-else statements. But again, I can't stress the importance of design patterns and classes enough. Learn it, live it, love it.
--
As for web interaction, that should be simple enough. Deferring Java for Python isn't necessary, but web interaction and things of the like is worlds easier in Python. A companion of mine made a simple wrapper/class in Java for handling simple GET/POST requests. You can find it on Github. The Tester file is an example on basic usage of it.
- 26 Nov. 2012 04:29pm #5
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.00
Looking into abstractions right now, thanks. And I'll be researching Java design patters, for nice formatting of code. Generally I can have fairly nice code, but if I get extremely lazy (like with this example) it becomes a mass of shit.
And thanks for the web interaction tips. That wrapper that your friend made is working great for me, I appreciate it. Right now I'm modifying it trk with my needs, but it's not changing the library as much as I'm just re-writing the Tester file.
EDIT: One last question. Any idea if it would be possible for me to change the user agent? I'm thinking of changing it to firefox or something, but don't know if it could be implemented in Java. Actually, I might look into adding it to the HTTP wrapper with Apache HTTPComponenets. Of course, this would require more libraries be added to the project, but it's a price to be paid to have added functionality to the program/bot.Last edited by 323; 26 Nov. 2012 at 04:39pm.
- 26 Nov. 2012 06:13pm #6
Lol, yes, there is a way to change the user agent. Though you have to do it manually. The http bindings are HttpURLConnection/URL. You can change it by accessing the HTTP instance somehow (you'd probably have to globalize the appropriate properties in the class to make it available). But that's considerably less straightforward than just adding a method like this:
PHP Code:public void setUA(String User_Agent)
{
URLConnection.setRequestProperty("User-Agent", User_Agent);
}
Adding a different library like the Apache HTTP Components is not only redundant, it's also frankly stupid. You'd seriously put yourself back at square one and defeat the purpose of this wrapper if you did that. This wrapper works out of the box unlike the third party Apache libs.
Lesson #1 - If it isn't broke don't fix it.
Lesson #2 - Learn to adapt: when you're in Greek you must speak Greek, not English. It's easier to adapt to an existing code base than to rewrite it.Last edited by The Unintelligible; 26 Nov. 2012 at 06:22pm.
- 26 Nov. 2012 06:37pm #7
Oh and by the way, ditch the BlueJ IDE and start using Eclipse or NetBeans. Best Java IDEs. Period. BlueJ was originally used for teaching purposes since it's a simple and minimalist IDE. Simple is fine, but if you're looking for comprehensiveness you should look elsewhere.
- 26 Nov. 2012 10:07pm #8
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.00
Yeah, I figured I could do it without something like apache, but I don't know enough about the default java.net.* library (I couldn't find any documentation on it anywhere)
Will do, I was just using BlueJ because it's what we use in class. It's also fairly simple I guess. I've been meaning to download Eclipse though, so doing that now.
- 27 Nov. 2012 03:19am #9
Like Unintelligible said, this code is pretty dirty, but if you haven't resolved it already your specific error comes from your issues with variable scope. The code is trying to evaluate a variable that's only initialized within a conditional. Java doesn't do things like that. You could always just initialize whatever variable to 0 to start out with.
- 27 Nov. 2012 03:24am #10
- 27 Nov. 2012 03:29am #11
- 27 Nov. 2012 03:33am #12
- 27 Nov. 2012 03:39am #13
- 27 Nov. 2012 03:42am #14
- 27 Nov. 2012 08:51am #15
One of your main issues in that block of code is redundancy. See how you're executing almost identical code time and time again? That really should be handled by a function/procedure. Also, especially in Java, having a procedure that long is never a good idea. Something like this is a much better design pattern:
PHP Code:import java.util.Scanner;
public class TirePressure
{
Scanner scanner = new Scanner(System.in);
public TirePressure()
{
checkTirePressure();
}
private void checkTirePressure()
{
String[] tires = {"right front", "left front", "right rear", "left rear"};
for (String tire : tires)
System.out.println(" " + (validPressure(tire) ? "Good" : "Bad") + " " + tire + " tire pressure.\n");
}
private boolean validPressure(String name)
{
System.out.print("Enter " + name + " pressure: ");
int pressure = scanner.nextInt();
return pressure >= 35 && pressure <= 45;
}
}
Code:Enter right front pressure: 43 Good right front tire pressure. Enter left front pressure: 32 Bad left front tire pressure. Enter right rear pressure: 37 Good right rear tire pressure. Enter left rear pressure: 52 Bad left rear tire pressure.
- 27 Nov. 2012 01:40pm #16