I see a lot of folks asking for help in this sub as well as /r/java. Allow me to give a few words of friendly advice.
Use a concise, descriptive title. What if I titled this post omg u guise! read this! would you read it? Me neither.
Indent all of your code to properly format it. To trigger "code mode", every line must be indented a minimum of 4 spaces. Each level of further nesting should be 2 additional spaces.
This line is indented 4 spaces.
This line is 6.
Here is 8.
Six again.
Now 4.
public class ThisEnsures {
private final String yourCode = "Looks like this.";
}
If it seems tedious to type 4 spaces at the front of every line, type all of your code into a code editor like eclipse, highlight it and hit tab until you get the 4 spaces you need.
I strongly recommend using 2 spaces additional indent for each level of nesting, but that's a style thing. At least make it readable for us to help you.
Use descriptive variable names. You should be doing this anyway as a matter of writing self-documenting code. But especially if you're posting here for help, your code should be easy to read. This includes your main class name, even if it's the only class in your code sample.
Include all relevant code. It's very difficult to help you if you post something like: Hey guys, I need help with this code snippet, can anyone find the bug?
String foo() {
funiculate(bar);
String desc = "I don't even.";
for (int i = bar; i< 2*bar; i--) { System.out.println(desc); }
}
Where on earth are all these things being referred to defined?
Don't put all your code in your main() method. I see lots of beginners do this, but you should get out of that habit. Also, don't just create a bunch of static methods and variables and have main() call those either.
Instead, take advantage of all that OO has to offer by creating an instance of your application, configure it with IO stuff, and then run it.
public class ExampleApp implements Runnable {
private final Scanner in;
private final PrintStream out;
private final PrintStream err;
public ExampleApp(InputStream in, PrintStream out, PrintStream err) {
this.in = new Scanner(in);
this.out = out;
this.err = err;
}
@Override
public void run() {
// write code for your app here
}
public static void main(String[] args) {
new ExampleApp(System.in, System.out, System.err).run();
}
}
Never, ever repeat code. If in the implementation of your run() method you notice that you are typing the same three lines of code over and over again, make a new private method and call it instead of typing that code over and over again. If that code doesn't require access to any instance state, make it static.
This is a great idea because it allows you to write short, easily understandable methods and give them (and their parameters) good descriptive names. Easy to understand, self-documenting code. This will also help you avoid bugs.
When you get a handful of methods that all seem to be operating on some subset of state, consider encapsulating that state with those methods into a new class. This is great practice for doing real programming, and helps you structure your thinking by clumping problems into high level chunks.
For example, in the example app above, I would consider letting the ExampleApp class create a class that handles all interaction with the user (maybe called UserInterface) and another class that does the actual business logic of whatever problem you're trying to solve, and then coordinate interaction between the two. This way, the flow of data between the user and the code that does the crunching is all encapsulated by the application class, and the code that does the crunching is separate from the code that does the interaction with the user.
Write unit tests before asking for help. If you have a bug, and you've written a lot of small, easy to understand methods, try writing JUnit tests for all those methods that exercise them in basic ways. Use a mocking framework like Mockito or EasyMock (I prefer Mockito) to configure your classes if needed.
You'll be surprised how often you don't need to post your question in the first place.
Good luck!