A Month of Coding Challenges with Java: Day 0 - Hello World

Hello world ! as I said in previous post, for the future post I will try to make solutions from challenges website. As for beginning to advanced we will start from 30 Days of Code Challenges. If you already registered in HackerRank you will asked if you want enroll this challenge. Well the purpose is to give you basic foundation. If you already finished one challenge you must wait for 24 hour to unlock the next challenge. If you tired of waiting you can enroll another challenge. There are so many of them, so you can choose as you like. So lets begin.




We will start from Day 0 : Hello World. Most of programming tutorial will start to run hello world program. The purpose is to make sure that component of the said programming language is already installed properly in your PC. Besides that it gives you a nice introduction to that language. Imagine a toddler starts to learn speak from difficult word, I wonder how the outcome :D. So in the picture above the “Try Again” button used to “Challenge” button. If you already finished it you can try the challenge again anytime you want.


Well, the task is you must print out a string  “Hello World” in the first line then followed by input that will stored in string variable. So here is the solution of this task.



public class Solution {
  public static void main(String[] args) {
    // Create a Scanner object to read input from stdin.
    Scanner scan = new Scanner(System.in); 
    
    // Read a full line of input from stdin and save it to our variable, inputString.
    String inputString = scan.nextLine(); 

    // Close the scanner object, because we've finished reading 
        // all of the input from stdin needed for this challenge.
    scan.close(); 
      
    // Print a string literal saying "Hello, World." to stdout.
    System.out.println("Hello, World.");
      
      // TODO: Write a line of code here that prints the contents of inputString to stdout.
      System.out.println(inputString);
  }
}


To input data you need object from class Scanner. When you make Scanner instance object you must insert an argument which belong to interface class. System.in will return an interface object.


When you input a text from your keyboard. The value will be read by scan.nextLine() then stored in String variable. You have to close the scanner object. If you don’t you will get a warning (not an error). This calles resources leak which is for the most part a mistaken example of asset utilization where a program does not discharge the asset it has procured. This can prompt poor services in your program.


Then print it out with System.out.println(). Remember if you want to print with println, the end of string will give break end make a new line.  If you do not want that you can print it with Systrem.out.print(). But you must insert \n in the end of your text to make new line.

Then let's run the program and get the result. Input is already handled by HackerRank. However if you want to input by yourself, you can check "Test against custom input" check box.



Comments

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL