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:com.aerofs.baseline.http.HttpUtils.java

public static String readStreamToString(InputStream in) throws IOException {
    Scanner scanner = new Scanner(in).useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
}

From source file:Matrix_Operations.java

public static int getRowsNumberFromUser() {
    Scanner keyboard = new Scanner(System.in);
    //Allow user to enter number of rows
    int rows;//w  ww  .  ja  va2 s  .c  o m
    System.out.print("Enter rows: ");

    rows = keyboard.nextInt();
    return rows;

}

From source file:de.document.service.UmlsService.java

public List<Umls> readAll() {
    List<Umls> umlsList = new ArrayList<>();

    try (Scanner s = new Scanner(
            (Thread.currentThread().getContextClassLoader().getResourceAsStream("Umls" + umlsterm)))) {
        while (s.hasNextLine()) {
            umlsList.add(new Umls(s.nextLine()));
        }/*www  . ja  v a2 s.c o  m*/
    }

    /* 
      Umls ob= new Umls();
      Umls ob1= new Umls(); 
      ob.setName("name");
      ob1.setName("name1");
      System.out.print(ob);
      }*/
    return umlsList;
}

From source file:org.spring.data.gemfire.app.main.AbstractApp.java

protected static void block() {
    System.out.printf("Press enter to exit...%n");
    new Scanner(System.in).next();
}

From source file:formatMessage.VerifyInputScanner.java

public static int verifyInt() {

    while (true) {
        Scanner input = new Scanner(System.in);
        try {// w ww  . j a v  a2s  . c o m
            int verified = input.nextInt();

            return verified;

        } catch (InputMismatchException e) {
            System.out.println("Geen geldig nummer. Probeer opnieuw.");

        }
    }
}

From source file:Main.java

/**
 * @return A map of all storage locations available
 *//*  ww w  . ja  v  a  2 s.  c  om*/
public static Map<String, File> getAllStorageLocations() {
    Map<String, File> map = new HashMap<String, File>(10);

    List<String> mMounts = new ArrayList<String>(10);
    List<String> mVold = new ArrayList<String>(10);
    mMounts.add("/mnt/sdcard");
    mVold.add("/mnt/sdcard");

    try {
        File mountFile = new File("/proc/mounts");
        if (mountFile.exists()) {
            Scanner scanner = new Scanner(mountFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.startsWith("/dev/block/vold/")) {
                    String[] lineElements = line.split(" ");
                    String element = lineElements[1];

                    // don't add the default mount path
                    // it's already in the list.
                    if (!element.equals("/mnt/sdcard"))
                        mMounts.add(element);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        File voldFile = new File("/system/etc/vold.fstab");
        if (voldFile.exists()) {
            Scanner scanner = new Scanner(voldFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.startsWith("dev_mount")) {
                    String[] lineElements = line.split(" ");
                    String element = lineElements[2];

                    if (element.contains(":"))
                        element = element.substring(0, element.indexOf(":"));
                    if (!element.equals("/mnt/sdcard"))
                        mVold.add(element);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (int i = 0; i < mMounts.size(); i++) {
        String mount = mMounts.get(i);
        if (!mVold.contains(mount))
            mMounts.remove(i--);
    }
    mVold.clear();

    List<String> mountHash = new ArrayList<String>(10);

    for (String mount : mMounts) {
        File root = new File(mount);
        if (root.exists() && root.isDirectory() && root.canWrite()) {
            File[] list = root.listFiles();
            String hash = "[";
            if (list != null) {
                for (File f : list) {
                    hash += f.getName().hashCode() + ":" + f.length() + ", ";
                }
            }
            hash += "]";
            if (!mountHash.contains(hash)) {
                String key = SD_CARD + "_" + map.size();
                if (map.size() == 0) {
                    key = SD_CARD;
                } else if (map.size() == 1) {
                    key = EXTERNAL_SD_CARD;
                }
                mountHash.add(hash);
                map.put(key, root);
            }
        }
    }

    mMounts.clear();

    if (map.isEmpty()) {
        map.put(SD_CARD, Environment.getExternalStorageDirectory());
    }
    return map;
}

From source file:testFileHandler.java

public void readFile(String path, javax.swing.JTextArea textField) {

    Scanner sc = new Scanner(System.in);
    BufferedReader br = null; //used BufferedReader to set Data to the textField line by line that passed as a parameter.
    String line;// ww w  .j av  a2s  . co m

    try {
        br = new BufferedReader(new FileReader(path)); //FileReader read the content of the file from that given path,and return value pass to the BufferedReader.
        while ((line = br.readLine()) != null) { // reading line by line untill end of the lines
            textField.append(line + '\n'); //append that line get at a time and move the cursor to the next line
        }

    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:IO.java

public static double[] readDoubleVec(File file) {
    double[] data;
    try {//w  ww . j  a v a2s. c o  m
        int nData = countLines(file.getAbsolutePath());
        data = new double[nData];

        Scanner sc = new Scanner(file);
        sc.useDelimiter(",|\\n");
        for (int i = 0; i < nData; i++) {
            data[i] = sc.nextDouble();
        }
        return data;
    } catch (IOException e) {
        e.printStackTrace();
        data = new double[0];
        return data;
    }
}

From source file:edu.usf.cutr.obascs.utils.CommandLineUtil.java

public static String getSpreadSheetId(CommandLine cmd) {

    String spreadSheetId;//w w  w  .  j a  v  a2s  .c o m
    if (cmd.hasOption(GeneralConstants.CL_OPTION_SPREADSHEET_ID)) {
        spreadSheetId = cmd.getOptionValue(GeneralConstants.CL_OPTION_SPREADSHEET_ID);
    } else {
        Scanner scanner = new Scanner(System.in);
        Logger.getInstance().log("Enter SpreadSheet id:");
        spreadSheetId = scanner.nextLine();
        scanner.close();
    }
    return spreadSheetId;
}

From source file:jmap2gml.ItemImage.java

private static HashMap<String, Image> readConfig() {
    HashMap<String, Image> out = new HashMap<>();
    JSONTEXT = "";
    Image img;/*from   w  w w .j a  va 2  s.  c om*/

    Scanner scan;
    try {
        scan = new Scanner(new File("ImageItemConfig"));

        while (scan.hasNext()) {
            JSONTEXT += scan.nextLine();
        }

        config = new JSONObject(JSONTEXT);

        for (String str : config.keySet()) {
            if (!str.contains("XOFFSET") && !str.contains("YOFFSET")) {
                img = (new ImageIcon(config.getString(str))).getImage();
                out.put(str, img);
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(ItemImage.class.getName()).log(Level.SEVERE, null, ex);
    }

    return out;
}