Perform the conversions after parsing the String : Scanner « File Input Output « Java






Perform the conversions after parsing the String

 

import java.io.File;
import java.util.Scanner;

public class Main {
  static void parseLine(String line) {
    Scanner lineScanner = new Scanner(line);
    lineScanner.useDelimiter("\\s*,\\s*");
    String name = lineScanner.next();
    int age = lineScanner.nextInt();
    boolean isCertified = lineScanner.nextBoolean();
    System.out.println("It is " + isCertified + " that " + name + ", age " + age
        + ", is certified.");
  }

  public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(new File("fileName"));
    scanner.useDelimiter(System.getProperty("line.separator"));
    while (scanner.hasNext()) {
      parseLine(scanner.next());
    }
    scanner.close();
  }
}

   
  








Related examples in the same category

1.new Scanner(new BufferedReader(new FileReader("xanadu.txt")))
2.Split a string using Scanner class
3.Read file using Scanner class
4.Read user input from console using Scanner class
5.Scanning text with java.util.Scanner
6.Simplify the code in TextReader by using java.util.Scanner
7.Read in the current contents of the java.net homepage