Your First Problem
So now you've opened the book. You want to learn something. You've gotten over the hardest step: starting. Welcome to your first problem. First we're going to explain how to start and how to create your basic Java file and run it.
First open JCreator or whatever IDE you prefer to use. Create a Hello.java file.
Your first file will need a class, right? Add one.
public class Hello {
}
What about a main method?
public class Hello {
public static void main(String[] args) {
}
}
Why not add some output?
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
You can run this and get the output of Hello, world!. But you knew how to do that didn't you. Let's add some input. Yeah! Well, first we're gonna need some libraries (it's like updating your toolbox so you can do more stuff). Don't worry they're just import statements in Java like always. We'll just import all the ones you may commonly want or use.
import java.io.*; // includes input and output streams, files, etc. Anything related to input and output
import java.util.*; // includes most data structures, built in algorithms, and the all important Scanner
// OPTIONAL
import java.math.BigInteger.*; // a class used for unbounded integer math (not needed in every program)
import static java.lang.System.*; // eliminates the need to type System on System.out
import static java.lang.Math.*; // eliminates the need to type Math on Math.sqrt() or any other math function
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
So now we can do some input right? Yep, let's use Scanner for that. Scanner will take a String or input object and allow you to parse it bit by bit. This allows us to gradually take in the input and process it as the problem requires.
import java.io.*;
import java.util.*;
// OPTIONAL
import java.math.BigInteger.*; // a class used for unbounded integer math (not needed in every program)
import static java.lang.System.*; // eliminates the need to type System on System.out
import static java.lang.Math.*; // eliminates the need to type Math on Math.sqrt() or any other math function
public class Hello {
public static void main(String[] args) throws IOException /* required to read from a file */ {
Scanner in = new Scanner(System.in);
/*
* //If you want to read from a file do this
* Scanner in = new Scanner(new File("filename.txt"));
*/
}
}
Now we have a scanner that reads from System.in (which is just through the console i.e. not from a file). But how do we use it. Well there are a few different methods available. We'll show these off and then send you to your first problem. Also remember you can use this file you made as a template for other problems.
import java.io.*;
import java.util.*;
// OPTIONAL
import java.math.BigInteger.*; // a class used for unbounded integer math (not needed in every program)
import static java.lang.System.*; // eliminates the need to type System on System.out
import static java.lang.Math.*; // eliminates the need to type Math on Math.sqrt() or any other math function
public class Hello {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt(); // takes in an integer
String word = in.next(); // takes in a word as a string (no spaces)
String line = in.nextLine(); // takes in an entire line as a string
double decimal = in.nextDouble(); // takes in a floating point number
}
}
There are more of which can be googled or asked about e.g. in.nextFloat() and in.nextLong() - input for taking in special numbers or datatypes.
Here is a good template to start from if you want.
import java.io.*;
import java.util.*;
import java.math.BigInteger.*; //optional
import static java.lang.System.*; //optional
import static java.lang.Math.*; //optional
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
}
}
Also some final advice for your first problem. Remember to read the entire problem description at the top along with the input and output statements. These areas often contain crucial information especially as you work into harder problems. Then use the sample input and output to test your program. If it succeeds on those, feel free to continue. If you really want to make sure it's right ask somebody more advanced. We'll help.