Home

Solve Me First

Let’s get started on the first challenge!

We will be given two integer inputs, and our code has to print the sum of the two integers.

Sample input:

2
3

Sample output:

5

The template Java code HackerRank has put on right now has a useful hint //Hint: Type return a+b; below, but other than that they don’t explain very much, so we’ll start from scratch instead. Delete all the template code!

First let’s make a class.

public class Solution {
}

A class is like a blueprint for an object. In this case, a blueprint for our solution. Classes are very important, but in the case of these challenges, (where solutions are written entirely within one file) we don’t really need to know much about them other than the fact that we have to write our solutions in a class called Solution.

Next, we have to create an entry point for our code to start, this is called main and it’ll look something like this:

public class Solution {
    public static void main(String[] args){
    }
}

Now that we’ve got that down, we need to actually read the integers as they come into our code. To do that, we need the help of a scanner. Instead of writing our own scanner, we’ll use Java’s scanner. In order to use it, we’ll have to import it first like so:

import java.util.*;

public class Solution {
    // rest of the code

Why java.util.*? Because when we looked up the scanner, above Class Scanner, we can see java.util. This means that it’s in the java util package. The * just means to import everything that belongs in the package, which will include the scanner that we need.

To use it, we must first create one:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
}

So now we’ve created a new scanner called in for input, and we’ve also told it that we need it to scan System.in. System.in is our standard input stream in Java, in other words, where our input will come in.

Now that we have this scanner, we can ask it to do work for us by telling it to read something, in this case, we need it to read the nextInt(), and store it in a variable.

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    int a = in.nextInt();
    int b = in.nextInt();
}

Using our sample input above with 2 and 3, our variables a will now hold 2 and b will hold 3. The challenge asks us to print the sum of the two integers a+b. In order to print, we’ll have to tell the system to output a new line to the standard output stream. This is what the completed code will look like:

import java.util.*;

public class Solution {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        System.out.println(a + b);
    }
}

And that’s it! A bit more work then just copying return a+b; but I hope it makes more sense.



Questions? Have a neat solution? Comment below!