Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

In this page you can find the example usage for java.util Scanner Scanner.

Prototype

public Scanner(ReadableByteChannel source) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();//from   ww  w .  j  a va  2 s .c  o  m

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();//from www.j a  v a2s . co m

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            System.out.println("int: " + src.nextInt());
        } else if (src.hasNextDouble()) {
            System.out.println("double: " + src.nextDouble());
        } else if (src.hasNextBoolean()) {
            System.out.println("boolean: " + src.nextBoolean());
        } else {
            System.out.println(src.next());
        }
    }
    fin.close();
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();//from   ww w.j a va2s .  c o  m

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;/* w  w  w . java 2  s .  co  m*/
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();/*from  ww  w.j av a2 s.co  m*/

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:LogTest.java

public static void main(String[] args) throws IOException {
    String inputfile = args[0];/*from   ww w .j  a v a 2s.  co m*/
    String outputfile = args[1];

    Map<String, Integer> map = new TreeMap<String, Integer>();

    Scanner scanner = new Scanner(new File(inputfile));
    while (scanner.hasNext()) {
        String word = scanner.next();
        Integer count = map.get(word);
        count = (count == null ? 1 : count + 1);
        map.put(word, count);
    }
    scanner.close();

    List<String> keys = new ArrayList<String>(map.keySet());
    Collections.sort(keys);

    PrintWriter out = new PrintWriter(new FileWriter(outputfile));
    for (String key : keys)
        out.println(key + " : " + map.get(key));
    out.close();
}

From source file:Main.java

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());/*  www  .  j a  va 2  s.  co  m*/
    }
    scanner.close();
}

From source file:com.skynetcomputing.skynettools.Main.java

/**
 * @param args the command line arguments
 *///w ww.  j  a  va 2 s.com
public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(System.in);
    boolean isRunning = true;
    while (isRunning) {
        System.out.println("Enter file path for MD5 hash. Leave empty to quit");
        String input = s.nextLine();
        isRunning = !input.isEmpty();
        if (isRunning) {
            File inputFile = new File(input);
            if (inputFile.length() > 0) {
                try (FileInputStream fis = new FileInputStream(inputFile)) {
                    String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
                    System.out.println("MD5: " + md5);
                }
            }
        }
    }

}

From source file:Matrix_Operations.java

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    //Allow user to enter number of columns
    int rows = getRowsNumberFromUser();
    int columns = getColumnNumberFromUser();
    double matrixA[][] = new double[rows][columns];

    //Enter values for matrix
    System.out.println("Enter values for each position in the matrix below. ");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            matrixA[i][j] = keyboard.nextDouble();

        }/*from  ww w  .j  a v a2  s  .c om*/
        System.out.println("");
    }

    showMatrix(matrixA);
    System.out.println("");
    RealMatrix A = MatrixUtils.createRealMatrix(matrixA);

    LUDecomposition lu = new LUDecomposition(A);

    showMatrix(matrixA);

    System.out.println(lu.getDeterminant());

}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int i;//w w  w .  j  av  a2s.c  o m
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("string true false 1 2 3 4.12");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}