Friday 19 August 2016

Java Tutorial : Java IO (Scanner class - Read names from Keyboard)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=d_hl_GdDH5c&list=UUhwKlOVR041tngjerWxVccw

ScannerDemo.java
import java.util.ArrayList;
import java.util.Scanner;

public class ScannerDemo
{

    public static void main(String[] args)
    {
        Scanner scanner = null;
        try
        {
            ArrayList<String> listOfNames = new ArrayList<String>();
            scanner = new Scanner(System.in);
            System.out.print("Enter the name: ");
            while (scanner.hasNextLine())
            {
                
                String name = scanner.nextLine();
                if (name.equalsIgnoreCase("quit"))
                {
                    break;
                }
                listOfNames.add(name);
                System.out.print("Enter the name: ");
            }
            System.out.println("listOfNames =  "+listOfNames);
        }
        finally
        {
            if (scanner != null)
            {
                scanner.close();
            }
        }

    }
}
Output
Enter the name: peter
Enter the name: dave
Enter the name: ram
Enter the name: quit
listOfNames =  [peter, dave, ram]

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Scanner_while%20loop_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Scanner_while%20loop_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/16638d65b46bec1088dab744ba69a31424c4a14f/BasicJava/JavaIODemo_Scanner_while%20loop_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment