Thursday, 18 August 2016

Java Tutorial : Java IO (Scanner class with delimiter)


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

ScannerDemo.java
import java.util.Scanner;

/*
 *  Scanner class with delimiter(-). 
 */
public class ScannerDemo
{

    public static void main(String[] args)
    {
        Scanner scanner = null;
        try
        {
            String inputValue = "10-20-30-40";
            scanner = new Scanner(inputValue);
            scanner.useDelimiter("-");

            int sum = 0;
            /*
             * Returns: true if and only if this scanner's
             * next token is a valid int value
             */
            while (scanner.hasNextInt())
            {
                /*
                 * Returns: the int scanned from the input
                 */
                int intValue = scanner.nextInt();
                System.out.println(intValue);
                sum = sum + intValue;
            }
            System.out.println("sum = " + sum);
        }
        finally
        {
            if (scanner != null)
            {
                scanner.close();
            }
        }
    }
}
Output
10
20
30
40
sum = 100

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

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Scanner_delimiter_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/16638d65b46bec1088dab744ba69a31424c4a14f/BasicJava/JavaIODemo_Scanner_delimiter_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