ZOFTINO.COM android and web dev tutorials

Java Scanner Tutorial

Scanner class breaks its input into tokens using the default or the specified delimiter and the tokens can be accessed using various next methods to get different types of values. The default delimiter of Scanner is space. You can change delimiter using useDelimiter() method. The source of data for Scanner can be strings, stream objects which are subclasses of InputStream or implementations of Readable, and files.

Scanner with String Input

Scanner object can be instantiated using its string argument constructs and passing a string to it. Once scanner object is created, you can loop thru all tokens. You can check to see if it has next token using hasNext() method and get next token using next method.

	String offer = "upto 90% off on fashion";
	
	Scanner scanner = new Scanner(offer);
	while(scanner.hasNext()) {
		String val = scanner.next();
		System.out.println(val);
	}
	scanner.close();

Output:

upto
90%
off
on
fashion

Specifying Scanner Delimiter

You can specify delimiter as regular expression or string using useDeimiter() method.

	String offer = "upto 90% off on fashion|flat 35% off on shoes|get 40% off on appliances";
	
	Scanner scanner = new Scanner(offer);
	scanner.useDelimiter("\\|");
	while(scanner.hasNext()) {
		String val = scanner.next();
		System.out.println(val);
	}
	scanner.close();

Output:

upto 90% off on fashion
flat 35% off on shoes
get 40% off on appliances

Scanner with File Input

If you have a file and it contains list of integer numbers. You can read the numbers using Scanner. First create Scanner object by passing file object as an argument to its constructor, then check if next number exists using appropriate method such as hasNextInt, hasNextFloat, hasNextLong, hasNextBoolean, hasNextShort, etc. and then read next number using nextInt, nextFloat, nextLong, nextBoolean, nextShort, etc. Like this, you can break any type of data from a file and get token using appropriate has next method and next methods.

	File fileObj = new File(file);
	Scanner scanner;
	try {
		scanner = new Scanner(fileObj);
	} catch (FileNotFoundException e) {
		return;
	}
	int total=0;
	while(scanner.hasNextInt()) {
		total = total + scanner.nextInt();
	}
	System.out.println(total);
	scanner.close();

Scanner with InputStream Input

You can use InputStream with scanner to break the data which the input stream supplies and get tokens. To use scanner with input stream, instantiate Scanner object by calling its constructor and passing an instance of any subclass of InputStream, such as ByteArrayInputStream, FileInputStream, ObjectInputStream, SequenceInputStream, StringBufferInputStream, etc.

	String offers= "upto 50% off on shoes,upto 30% off on mobiles,get 5% off appliances";

	try {
		ByteArrayInputStream inputStream = 
				new ByteArrayInputStream(offers.getBytes());
					
		Scanner scanner = new Scanner(inputStream);
		scanner.useDelimiter(",");
		while(scanner.hasNext()) {
			String val = scanner.next();
			System.out.println(val);
		}
		scanner.close();
	} catch (Exception e) {

	}

Output

upto 50% off on shoes
upto 30% off on mobiles
get 5% off appliances

Scanner with Readable Input

You can use Scanner with Readable implementation such as BufferedReader, CharArrayReader, FileReader, InputStreamReader, StringReader, etc.

	try {
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		
		Scanner scanner = new Scanner(br);
		scanner.useDelimiter("\\|");
		while(scanner.hasNext()) {
			String val = scanner.next();
			System.out.println(val);
		}
		scanner.close();
	} catch (FileNotFoundException e) {

	}

Other Tutorials Related to IO