Recently I’ve been going through the HackerRank.com 30 Days of Code course to learn Java. Below is my code for their Day 1 lesson.
After reviewing the code I think I would prefer to add some data type checking to ensure that the data accepted from scan.nextLine() is actually the data type that I expect. It passed the unit tests provided by HackerRank but I doubt this would pass negative testing in a Test Driven Development organization.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { int i = 4; double d = 4.0; String s = "HackerRank "; Scanner scan = new Scanner(System.in); /* Declare second integer, double, and String variables. */ int j; double e; String t; /* Read and save an integer, double, and String to your variables.*/ j = Integer.parseInt(scan.nextLine()); e = Double.parseDouble(scan.nextLine()); t = scan.nextLine(); /* Print the sum of both integer variables on a new line. */ System.out.println(i + j); /* Print the sum of the double variables on a new line. */ System.out.println(d + e); /* Concatenate and print the String variables on a new line; the 's' variable above should be printed first. */ System.out.println(s + t); scan.close(); } }