Java I/O How to - Scan a String by value type








Question

We would like to know how to scan a String by value type.

Answer

import java.util.Scanner;
//from  www  .  j av  a2  s.  c  o m
public class MainClass {
  public static void main(String args[]) {
    String instr = "10 99.88 scanning is easy.";
    Scanner conin = new Scanner(instr);

    while (conin.hasNext()) {
      if (conin.hasNextDouble()) {
        System.out.println(conin.nextDouble()); 
      }
    }

  }
}

The code above generates the following result.