![]() |
|
[Java] Guide to the Java Forum | Etiquette and FAQ | Read Before Posting! - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Guide to the Java Forum | Etiquette and FAQ | Read Before Posting! (/showthread.php?tid=5172423) |
Guide to the Java Forum | Etiquette and FAQ | Read Before Posting! - _wind - Feb 16, 2016 Java Section Info, Etiquette, and FAQ After spending a lot of time in this forum, I noticed a lot of recurring mistakes that people make -- which can be easily prevented by following a set of guidelines. Anyways, here's the guide to the Java Forum, with basic information, do's and dont's, and some common questions and answers. What is the Java Section? The Java section is:
The Java section is NOT:
Posting Guidelines
Formatting Code It's recommended that you stick to the following format when posting your code, as it'll make work much easier for those helping you. Where do I post my code?
Indents Don't be this guy: Code import java.io.*;Indent your code with either four spaces or an indent with each new block. Naming Rather simple, but a common mistake that often confuses me when reading others' code. Variables and Methods are named starting with a lowercase first letter, followed by uppercase first letters of following words. Code int myInteger;Classes are named with uppercase letters on each word. Code public class HelloWorld{}Frequently Asked Questions Why doesn't my String comparison work? Example problem: Code String myString = "foobar";The above is wrong, as Strings are compared using myString.equals("foobar"); instead of the == operator. When do I use Arrays? ArrayLists? Arrays have fixed lengths, so use them when you know the length of your list. Array Syntax (Click to View) ArrayLists are flexible and you can add items without having to define a fixed length. ArrayList Syntax (Click to View) NullPointerException Every single person who has coded Java gets this error at one point. This is caused by a variable that you're calling that has a null (nonexistent) value, which throws an error. Solution: Look at the error dump, and refer to the line which the error occurred on. Check to see that each variable has been defined. At times* you'll need to check if the variable is == null, and only run your code if it isn't. How do I use a library? Maybe you've gone online and found a library that you want to use in your project. After all, it wouldn't be practical to re-invent the wheel. To add a library to your Java project (Eclipse) right click on your project, go to 'Build Path' -> 'Configure Build Path' -> 'Add external jars' -> Select, and import. Why does my code keep running without stopping? Chances are, you're in a while loop and either:
General notes: while(true){} will run forever and ever and ever until you break out of it. while(comparison){} will run until the comparison becomes false break; breaks out of a loop How do I convert from one data type to another? Maybe you want to convert an integer to a String, or vice-versa. Or maybe a int to double, or any other conversion. Converting datatype x to a String has two options: Code //method 1And method 2 works for most other data types as well. For example, converting a double to integer would look like: Code Integer.parse(myDouble);Why am I not writing to file? A common mistake when writing text to a file would be not closing the writer. Example: Code PrintWriter writer = new PrintWriter("myfilename.txt", "UTF-8");This won't write anything to the file, because you're missing the Code writer.close();My code doesn't work/crashes, but I'm not seeing any errors? Make sure you don't have an empty try/catch statement with exceptions. A common mistake is doing the following: Code try {Make sure that you're always printing the stack trace when handling exception. You can do this with Code e.printStackTrace();My program skips over the first value in array? Don't forget that Java (and every other programming language) is zero-indexed, meaning that you count from zero. Say you had an array: Code int[] myArray = {1, 2, 3, 4, 5};So myArray[0] is equal to 1 myArray[1] is equal to 2 myArray[2] is equal to 3 and so on... That's basically it. There are also probably a lot of JavaFX questions that I missed, but I don't have experience in that. +Haze would be your best bet. Happy coding! * - csrk - Feb 16, 2016 HQ post, but isn't a moderator supposed to do this? * - hoLe[R]a - Feb 16, 2016 I thought this would be a redundant thread but found some interesting things here actually. * - Sunquyman - Feb 16, 2016 Nice, can this get stickied? It would definitely help a lot for newer users * - youw0t - Feb 16, 2016 Definitely a nice addition to the forum and very helpful for people that are new to Java. |