Scanner scanner = new Scanner(myString); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // process the line } scanner.close();
private static void abortIfUserIsNotSure(final String message) { if (FORCE) { return; } System.out.println(message); System.out.print("Are you sure? [Y/N] ?: "); try { java.awt.Toolkit.getDefaultToolkit().beep(); } catch (final Throwable t) { /* Ignore */ } final String line = new Scanner(System.in).nextLine().toUpperCase().trim(); if (!line.equals("Y") && !line.equals("YES")) { System.err.println("User aborted."); System.exit(0); } }
/** * @param service * @param input */ private static void getPersonDetails(final Scanner scanner,final PersonService service) { while(true) { System.out.print("Please enter the name of the person and press<enter>: "); String input = scanner.nextLine(); final List<Person> personList = service.findPersonByName(input); if(personList != null && !personList.isEmpty()) { for(Person person:personList) { System.out.print( String.format("Person found - Person Id: '%d', Person Name is: '%s', Gender: '%s'", person.getPersonId(),person.getName(), person.getGender())); System.out.println(String.format(", Date of birth: '%1$td/%1$tm/%1$tC%1$ty'", person.getDateOfBirth())); } } else { System.out.println( String.format("No Person record found for name: '%s'.", input)); } System.out.print("Do you want to find another person? (y/n)"); String choice = scanner.nextLine(); if(!"y".equalsIgnoreCase(choice)) { break; } } } }
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(" "); 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(" "); File root = new File(mount); if (root.exists() && root.isDirectory() && root.canWrite()) { File[] list = root.listFiles();
int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = Integer.parseInt(in.nextLine().trim()); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.nextLine(); }
/** * visible width for the given string. * * for example: "abc\n1234"'s width is 4. * * @param string the given string * @return visible width */ private static int width(String string) { int maxWidth = 0; try (Scanner scanner = new Scanner(new StringReader(string))) { while (scanner.hasNextLine()) { maxWidth = max(length(scanner.nextLine()), maxWidth); } } return maxWidth; }
@Override public void run(ResourceManager resourceManager, String... args) { String projectId = args[0]; System.out.printf("Going to delete project \"%s\". Are you sure [y/N]: ", projectId); Scanner scanner = new Scanner(System.in); if (scanner.nextLine().toLowerCase().equals("y")) { resourceManager.delete(projectId); System.out.printf("Successfully deleted project %s.%n", projectId); } else { System.out.printf("Will not delete project %s.%n", projectId); } scanner.close(); }
@Override public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain chain) throws IOException { resource = chain.transform(request, resource); if (!this.fileExtension.equals(StringUtils.getFilenameExtension(resource.getFilename()))) { return resource; } byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); String content = new String(bytes, DEFAULT_CHARSET); if (!content.startsWith(MANIFEST_HEADER)) { if (logger.isTraceEnabled()) { logger.trace("Skipping " + resource + ": Manifest does not start with 'CACHE MANIFEST'"); } return resource; } @SuppressWarnings("resource") Scanner scanner = new Scanner(content); LineInfo previous = null; LineAggregator aggregator = new LineAggregator(resource, content); while (scanner.hasNext()) { String line = scanner.nextLine(); LineInfo current = new LineInfo(line, previous); LineOutput lineOutput = processLine(current, request, resource, chain); aggregator.add(lineOutput); previous = current; } return aggregator.createResource(); }
class myProg { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Printing the file passed in:"); while(sc.hasNextLine()) System.out.println(sc.nextLine()); } }
public class ConsoleReadingDemo { public static void main(String[] args) { // ==== BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter user name : "); String username = null; try { username = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } System.out.println("You entered : " + username); // ===== In Java 5, Java.util,Scanner is used for this purpose. Scanner in = new Scanner(System.in); System.out.print("Please enter user name : "); username = in.nextLine(); System.out.println("You entered : " + username); // ====== Java 6 Console console = System.console(); username = console.readLine("Please enter user name : "); System.out.println("You entered : " + username); } }
private static String readKey(String key) { Scanner stdin = new Scanner(System.in); System.out.println("Choose a Spider demo:"); for (Map.Entry<String, Class> classEntry : clazzMap.entrySet()) { System.out.println(classEntry.getKey()+"\t" + classEntry.getValue() + "\t" + urlMap.get(classEntry.getKey())); } while (key == null) { key = stdin.nextLine(); if (clazzMap.get(key) == null) { System.out.println("Invalid choice!"); key = null; } } return key; } }
private static void createPersonDetails(final Scanner scanner,PersonService service) { while(true) { System.out.print("\nEnter the Person's name:"); String name = scanner.nextLine(); Gender gender; while(true) { System.out.print("Enter the Person's gender(M/F):"); String genderStr = scanner.nextLine(); if("m".equalsIgnoreCase(genderStr) || "f".equalsIgnoreCase(genderStr)) { gender = Gender.getGenderByIdentifier(genderStr.toUpperCase()); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); while(true) { System.out.print("Enter the Person's Date of birth in DD/MM/YYYY format:"); String dobStr = scanner.nextLine(); try { dateOfBirth = format.parse(dobStr); person.setName(name); person = service.createPerson(person); System.out.println("Created person record with id: " + person.getPersonId()); System.out.print("Do you want to create another person? (y/n)"); String choice = scanner.nextLine(); if(!"y".equalsIgnoreCase(choice)) { break;
.map(String::trim) .map(path -> path.replaceAll("^~", "$HOME")) .map(path -> new File(path).exists() ? path : StringUtils.expandEnvironmentVariables(path)) .toArray(String[]::new); } else if (!"".equals(props.getProperty("", ""))) { Scanner scanner = new Scanner(System.in); String line; try { line = scanner.nextLine(); } catch (NoSuchElementException e) { log.info("No lines found on standard in"); processDocument(pipeline, "stdin", line); try { line = scanner.nextLine(); } catch (NoSuchElementException e) { return; if (!new File(file).exists() || !new File(file).canRead()) { log.error("Cannot read file (or file does not exist: '" + file + '\'');
/** * visible width for the given string. * * for example: "abc\n1234"'s width is 4. * * @param string the given string * @return visible width */ private static int width(String string) { int maxWidth = 0; try (Scanner scanner = new Scanner(new StringReader(string))) { while (scanner.hasNextLine()) { maxWidth = max(length(scanner.nextLine()), maxWidth); } } return maxWidth; }
private Address readNewAddress(PrintStream out, Scanner scanner) { Address a = new Address(); out.print("Street name (NULL for null): "); a.setStreetName(convertString(scanner.nextLine(), "")); out.print("House number: "); a.setHouseNumber(convertStringToInteger(scanner.nextLine(), 0)); out.print("Flat number: "); a.setFlatNumber(convertStringToInteger(scanner.nextLine(), 0)); a.setPersons(new HashSet<Person>()); return a; }
private static String checkGpuAnalyzerVersion(){ try { ProcessBuilder pb = new ProcessBuilder("GPUShaderAnalyzer", "-ListModules"); Process p = pb.start(); Scanner scan = new Scanner(p.getInputStream()); String ln = scan.nextLine(); scan.close(); p.destroy(); return ln; } catch (IOException ex) { logger.log(Level.SEVERE, "IOEx", ex); } return null; }