Code2care : TechZone Apps & Tutorials

Java : Difference between using Scanner Class and String args for user input



To read user input in a Java program we can use

1. String args[] : Command Line Arguments


    These are command line arguments that has to be passed when the Java program is run on command prompt/terminal.

    java myProg Param1 Param2

    $ java myProg Args1 Args2


    args is an array of String values.

    Example :



    import java.util.Scanner;
    
    public class stringArgsDemo {
    
    public static void main(String[]arguments){
    
    Scanner input = new Scanner(System.in);
    
        String name;
        int age;
    
     System.out.print("Enter First multiple");
            number1 = input.nextInt();
    
          //This is not a interactive way!
            name =  args[0];
            age =  Integer.parseInt(args[1]);
    
           System.out.println("Name : "+name +"\n" +"Age : "+age);
       }
    
    }




2. Scanner : Standard Input


    This is a interactive way of accepting inputs from the user. When ever input.nextInt(), input.next() is encountered the prompt stops to get input from the user.

    Example :



    import java.util.Scanner;
    
    public class scannerClassDemo {
    
    public static void main(String[]arguments){
    
    Scanner input = new Scanner(System.in);
    
        String name;
        int age;
    
     System.out.print("Enter First multiple");
            number1 = input.nextInt();
    
           //Interactive way of getting input
            System.out.printlin ("Enter your Name : ");
            name = input. next();
    
            System.out.printlin ("Enter your Age : ");
            age = input. nextInt();
    
           System.out.println("Name : "+name +"\n" +"Age : "+age);
       }
    
    }




Top




Posts related to java.


1. Your System has Mac OS X Version 10.10. Java from Oracle requires 10.7.3 or later error

2. Detect if Cookies are enabled using javascript

3. Digital clock example using javaScript with Source code

4. Setting Java_Home Environment variable on Windows Operating System

5. How to get Timestamp in Java Android Programming

6. How to detect Browser and Operating System Name and Version using javaScript

7. Java JRE Installation location Mac OS X 10.10 Yosemite

8. Top 15 Programming Languages to learn 2014-2015

9. Difference between using Scanner Class and String args for user input in Java

10. Remove Trailing zeros BigDecimal Java

11. How to get UTC (GMT) using javascript

12. Get Device Screen Width and Height using javaScript

13. Comprehensive 256 Ascii code table with Html Hex IBM Microsoft Key

14. Eclipse Failed to load the JNI shared library jvm.dll

15. Add Animated Scrolling to Html Page Title Script

16. How to make jsfiddle bootstrap ready

17. JRE 8 dmg for Mac OSX 10.10 Yosemite downloading speed goes to 0 kb/s when trying to download from Oracle Website

18. Loading previous page using html button using javascript

19. Java location in Mac OS X

    more...






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17