A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

One last java question

1.5k views · started by 323 ·
#1
One last java question
Im getting "Variable rr may not have been initialized" with it pointing at one of my if statements at the very bottom.

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


This was the problem:


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


And this was exercise one, if you need it:


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


I feel like I'm making this problem way longer and way more complicated than it needs to be, any tips?
#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.
#3
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.


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.
#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.
#5
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.


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 to work 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.
#6

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.


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:

public void setUA(String User_Agent)
{
URLConnection.setRequestProperty("User-Agent", User_Agent);
}


That should work. I didn't test it though but that should do.

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.
#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.
#8
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:

public void setUA(String User_Agent)
{
URLConnection.setRequestProperty("User-Agent", User_Agent);
}


That should work. I didn't test it though but that should do.

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.


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)

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.


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.
#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.
#10
Isonyx wrote:
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.


^ Java fgt and creator of the wrapper here.

Man we haven't talked in so long
#11
^ Java fgt and creator of the wrapper here.

Man we haven't talked in so long


Yeah, going to attempt to be active here since I don't really have a forum I frequent anymore.
Drop me some messages on what projects you're doing.
#12
Isonyx wrote:
Yeah, going to attempt to be active here since I don't really have a forum I frequent anymore.
Drop me some messages on what projects you're doing.


Ye ye. That'd be fine.

And will do. Just try to at least be semi-active online (in general). But I can tell you I'm working on absolutely nada right now. Though I do have a few planned. Just don't have the wherewithal to get around to them yet. Will though in due time.
#13
Ye ye. That'd be fine.

And will do. Just try to at least be semi-active online (in general). But I can tell you I'm working on absolutely nada right now. Though I do have a few planned. Just don't have the wherewithal to get around to them yet. Will though in due time.


Alright then, I'm trying to get back to independent online projects. Once I get one started I'll hit you up about it.
#14
Isonyx wrote:
Alright then, I'm trying to get back to independent online projects. Once I get one started I'll hit you up about it.


Let's have an interesting run this time.

And alright, I'll try to check into MSN sometimes. If I'm not on I'm usually here or offline.
#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:

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


Output:
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.


Good luck.
#16
Isonyx wrote:
Spoiler
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.



Artificial wrote:
Spoiler
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:

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


Output:
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.


Good luck.


Gave both you kind gentlemen +rep. Great posts.