Example usage for java.util Scanner nextLine

List of usage examples for java.util Scanner nextLine

Introduction

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

Prototype

public String nextLine() 

Source Link

Document

Advances this scanner past the current line and returns the input that was skipped.

Usage

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a program that asks the user for a filename. It then reads a file
 * line-by-line, and outputs its contents in uppercase.
 *
 * Make sure that the program exits gracefully when the file does not exist.
 *
 *///from   w w w . ja v a 2s.c  o m
public static void doBuildIt1() {
    // Prompt the User for a Filename
    Scanner kb = new Scanner(System.in);
    System.out.println("Filename: ");
    String filename = kb.next();

    try {
        // Attempt to Access the File, this may cause an Exception
        File file = new File(filename);
        Scanner input = new Scanner(file);

        // Iterate through the File and Output in Upper Case
        while (input.hasNext()) {
            System.out.println(input.nextLine().toUpperCase());
        }
    } catch (IOException ex) {
        // If the File is Not Found, Tell the User and Exit Gracefully
        System.err.println("File not found: " + filename);
    }
}

From source file:azureml_besapp.AzureML_BESApp.java

/**
 * Read the JSON schema from the file rrsJson.json
 * /*from w ww  .j a v a2s .c  om*/
 * @param filename It expects a fully qualified file name that contains input JSON file
 */
public static void readJson(String filename) {
    try {
        File apiFile = new File(filename);
        Scanner sc = new Scanner(apiFile);
        jsonBody = "";
        while (sc.hasNext()) {
            jsonBody += sc.nextLine() + "\n";
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:fi.jyu.it.ties456.week38.Main.Main.java

public static void createCourse(TeacherRegistry teacher, CourseIS course)
        throws NoSuchTeacherException_Exception {
    Scanner inputID = new Scanner(System.in);
    Scanner cinName = new Scanner(System.in);
    Scanner cinID = new Scanner(System.in);
    Scanner cinCredit = new Scanner(System.in);
    Scanner cinDP = new Scanner(System.in);
    System.out.println("Course Name");
    String cName = cinName.nextLine();
    System.out.println("Search Teacher ID");
    String SID = cinID.nextLine();
    System.out.println(teacher.searchForPerson(SID).get(0).getID());
    System.out.println("Input Teacher ID");
    String ID = inputID.nextLine();
    System.out.println("Course Credit");
    int credit = 0;
    try {/*w w  w  .  j  a  v  a 2  s  .co m*/
        credit = cinCredit.nextInt();
    } catch (InputMismatchException e) {
        System.err.println("Course credit must be an int");
        main(null);
    }
    System.out.println("Course Description");
    String description = cinDP.nextLine();
    System.out.println(course.createCourse(cName, ID, credit, description));
}

From source file:Main.java

/**
 * @return A map of all storage locations available
 *///  www .j  a v a 2s .  com
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:com.ExtendedAlpha.SWI.SeparatorLib.InventorySeparator.java

public static ItemStack[] getInventory(File jsonFile, int size) {
    String source = "";
    try {//from   w  w w . j a  v a2  s .c  om
        Scanner x = new Scanner(jsonFile);
        while (x.hasNextLine()) {
            source += x.nextLine() + "\n";
        }
        x.close();
        return getInventory(source, size);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.ifgi.mosia.wpswfs.Util.java

public static String readContent(InputStream is, String enc) {
    if (is == null) {
        return null;
    }/*from   ww  w. ja v  a2  s .  c  om*/

    Scanner sc = new Scanner(is, enc == null ? CharEncoding.ISO_8859_1 : enc);
    StringBuilder sb = new StringBuilder();

    String sep = System.getProperty("line.separator");
    while (sc.hasNext()) {
        sb.append(sc.nextLine());
        sb.append(sep);
    }

    sc.close();
    return sb.toString();
}

From source file:io.github.seanboyy.lotReset.json.ReadJSON.java

/**Read JSON file by using configuration file's JSON value,
 * which points to the file/*from w w  w. j a v a 2s .  c  om*/
 * @param configLocation String which specifies the location of the config.properties file
 * @return <code>ArrayList&ltArrayList&ltLot&gt&gt lots</code> if no errors are encountered
 * @since 1.0 - Implemented config.properties in 2.0
 */
public static ArrayList<ArrayList<Lot>> read(String configLocation) {
    JSONParser parser = new JSONParser();
    URL url;
    File f;
    //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...}
    ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>();
    Config config = ReadConfig.read(configLocation);
    final String[] alphabet = config.getAlpha();
    final String[] types = config.getType();
    final String[] worlds = config.getWorld();
    try {
        url = new URL(config.getJSON());
        InputStream in = url.openStream();
        f = File.createTempFile("temp", "json");
        FileWriter file = new FileWriter(f);
        Scanner input = new Scanner(in);
        while (input.hasNextLine()) {
            file.write(input.nextLine());
        }
        input.close();
        file.close();
        Object obj = parser.parse(new FileReader(f));
        JSONObject jsonObj = (JSONObject) obj;
        JSONObject regions = (JSONObject) jsonObj.get("Regions");
        for (String a : worlds) {
            for (String b : types) {
                for (String c : alphabet) {
                    for (int d = 1; d <= alphabet.length; ++d) {
                        String lotId = a + "-" + b + "-" + c + d;
                        String lotIdA = a + "_" + b + "-" + c + d;
                        String lotIdB = a + "-" + b + "_" + c + d;
                        String lotIdC = a + "_" + b + "_" + c + d;
                        JSONObject lot = (JSONObject) regions.get(lotId);
                        JSONObject lotA = (JSONObject) regions.get(lotIdA);
                        JSONObject lotB = (JSONObject) regions.get(lotIdB);
                        JSONObject lotC = (JSONObject) regions.get(lotIdC);
                        ArrayList<Lot> lotInfo = new ArrayList<Lot>();
                        if (lot != null) {
                            lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"),
                                    (long) lot.get("source_minY"), (long) lot.get("source_maxY"),
                                    (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"),
                                    (String) lot.get("source_file"), lotId));
                            lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"),
                                    (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"),
                                    (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"),
                                    (String) lot.get("dest_file"), lotId));
                            lots.add(lotInfo);
                        }
                        if (lotA != null) {
                            lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"),
                                    (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"),
                                    (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"),
                                    (String) lotA.get("source_file"), lotIdA));
                            lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"),
                                    (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"),
                                    (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"),
                                    (String) lotA.get("dest_file"), lotIdA));
                            lots.add(lotInfo);
                        }
                        if (lotB != null) {
                            lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"),
                                    (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"),
                                    (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"),
                                    (String) lotB.get("source_file"), lotIdB));
                            lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"),
                                    (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"),
                                    (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"),
                                    (String) lotB.get("dest_file"), lotIdB));
                            lots.add(lotInfo);
                        }
                        if (lotC != null) {
                            lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"),
                                    (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"),
                                    (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"),
                                    (String) lotC.get("source_file"), lotIdC));
                            lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"),
                                    (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"),
                                    (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"),
                                    (String) lotC.get("dest_file"), lotIdC));
                            lots.add(lotInfo);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        System.out.println("FILE ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    } catch (ParseException e) {
        System.out.println("PARSER ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    }
    return lots;
}

From source file:org.apache.streams.lucene.TestLucenSimpleTaggingProcessor.java

@BeforeClass
public static void setUpTags() {
    tags = Lists.newLinkedList();/*www.  j a v  a2 s.  c  o  m*/
    Scanner scanner = new Scanner(TestLucenSimpleTaggingProcessor.class.getResourceAsStream("/TestTags.tsv"));
    while (scanner.hasNextLine()) {
        String[] line = scanner.nextLine().split("\t");
        tags.add(new LanguageTag(line[0], line[1], "en"));
    }
    mapper = new ObjectMapper();
}

From source file:org.springframework.hateoas.alps.JacksonSerializationTests.java

private static String read(Resource resource) throws IOException {

    Scanner scanner = null;

    try {/* w  w w  . j  a  v  a  2  s  .  c o  m*/

        scanner = new Scanner(resource.getInputStream());
        StringBuilder builder = new StringBuilder();

        while (scanner.hasNextLine()) {

            builder.append(scanner.nextLine());

            if (scanner.hasNextLine()) {
                builder.append("\n");
            }
        }

        return builder.toString();

    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:org.springframework.hateoas.alps.JacksonSerializationTest.java

private static String read(Resource resource) throws IOException {

    Scanner scanner = null;

    try {//from   w  w w  .j av a 2  s  . co  m

        scanner = new Scanner(resource.getInputStream());
        StringBuilder builder = new StringBuilder();

        while (scanner.hasNextLine()) {

            builder.append(scanner.nextLine());

            if (scanner.hasNextLine()) {
                builder.append(System.getProperty("line.separator"));
            }
        }

        return builder.toString();

    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}