Coding essentials and Java Input

Coding essentials and Java Input

In this tutorial you will learn: 1. Some coding essentials 2. Taking input in java console 3. Basic JAVA console program on conditional statements I have setting up the IDE now it’s time to start coding. The learning curve will be a little steep as we have much to cover. Some Coding Essentials This perhaps one of the most important things while programming in any language. There are a few main practices that every programmer must follow: 1. Indentation: To make your code more understandable indentation is a must. Try to balance the braces and statements. Try to use tab spaces and new lines as much as possible. You have a lot of free space so try your best to make your program neat. For example
  1.  
  2. Public static void main(String []args){
  3. If(statement){
  4. }
  5. Else{
  6. }
  7. }
2. Comments: Try to comment your program as much as possible. Comments are basically small notes so that you can understand what a specific line is doing in your program. They are very helpful whenever you have long lines of code or someone else opens your program.
  1. // single line comments
  2.  
  3.  /*  If(){
  4. }
  5. Else{ }
  6. */
3. Meaningful names: In order to make your program more readable you should try your outmost best to come up with meaningful variable names for all the classes, variables and methods. This will make your improve the readability of your program. e.g good names
  1. Int number1 or num1;
  2. Int add(int num1,int num2);
  3. Char choice =+;
  4. Boolean isFull(){}
  5. Boolean isEmpty(){}
  6. Int setX(int num){}
  7. Int getX(){}
Basic Java console program: • Open Eclipse > Workspace • File > new > java project • File > new > class Write the following program
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Input {
  5.         public static void main(String[] args) {
  6.                 int num;
  7.                 char choice;
  8.                 float decimal;
  9.                 String name;
  10.                
  11.                 System.out.print("Enter number e.g 123 :");
  12.                 Scanner inputScanner= new Scanner(System.in);
  13.                         num = inputScanner.nextInt(); //integer input
  14.                
  15.                  System.out.print("Enter character e.g z :");
  16.                         choice = inputScanner.next().charAt(0); //character input
  17.              
  18.              System.out.print("Enter floating point number e.g 34.53 :");
  19.                 decimal = inputScanner.nextFloat(); //float input
  20.                
  21.              System.out.print("Enter String e.g my name is xyz :");
  22.                 name = inputScanner.next(); //string input
  23.                
  24.                 //simply priting values
  25.                 System.out.println("Number = "+num);
  26.                 System.out.println("Choice = "+choice);
  27.                 System.out.println("Decimal = "+decimal);
  28.                 System.out.println("Name = "+name);
  29.                
  30.         }//end main
  31. }//end class
Note: The name of your public class should be the same as your class name. Here we can see that inorder to take input we first have to make an object of the scanner class and then we are calling different functions for different data types. Like nextInt() for integer, next().charAt(0) for characters, next() for strings. Then we are simply displaying these values to check whether the variables got the required input or not. output We will learn more coding in next tutorials. If you have any questions or want to learn programming from me simply add me on skype Username: mkprogrammer2121

Add new comment