List of usage examples for java.util Scanner Scanner
public Scanner(ReadableByteChannel source)
From source file:com.dalelotts.rpn.ScannerTest.java
@Test(expected = NoSuchElementException.class) // Then public void nextThrowsExceptionForEmptyString() throws Exception { // Given//from ww w. java 2 s . c o m try (final InputStream inputStream = IOUtils.toInputStream("", "UTF-8")) { scanner = new Scanner(inputStream); // When scanner.next(); // Expect this to throw an exception // Then // Will not reach this point. // (expected = NoSuchElementException.class) above // tells JUnit that this test should throw a NoSuchElementException. } }
From source file:com.mycompany.asyncreq.GetThread.java
@Override public void run() { try {// ww w . j a v a 2 s . c om Future<HttpResponse> future = client.execute(request, context, null); HttpResponse response = future.get(); MatcherAssert.assertThat(response.getStatusLine().getReasonPhrase(), equals(200)); Boolean isDone = true; Scanner scan = new Scanner(System.in); File f = new File("my.txt"); FileWriter fr = new FileWriter(f); BufferedWriter bwr = new BufferedWriter(fr); BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); while ((br.readLine()) != null) { bwr.write(new Scanner(System.in).nextLine()); } } catch (Exception ex) { System.out.println(ex.getLocalizedMessage()); } }
From source file:com.attivio.releng.scmsync.GithubEnterprise.Payload.java
/** * Create from file containing json - mostly to unit tests * @param jsonFile file containing json//from w w w .j a va 2s. c o m * @return Payload object * @throws IOException on missing or inaccessible file */ public static Payload createFromFile(File jsonFile) throws IOException { String content = new Scanner(jsonFile).useDelimiter("\\Z").next(); return Payload.createFromString(content); }
From source file:app.sunstreak.yourpisd.net.LoginDebugger.java
static void debug(String[] args) throws MalformedURLException, IOException, InterruptedException, ExecutionException, JSONException { if (args.length == 2) { mEmail = args[0];/* ww w.ja v a 2s .co m*/ mPassword = args[1]; System.out.println("|" + mEmail + "|" + mPassword + "|"); } else { Scanner sc = new Scanner(System.in); System.out.print("Please enter username:\t"); mEmail = sc.next(); System.out.print("Please enter password:\t"); mPassword = sc.next(); sc.close(); } session = Session.createSession(mEmail, mPassword); int loginSuccess = session.login(); if (loginSuccess == 1) System.out.println("Successful login!"); else { System.out.println("Login failed."); System.exit(-1); } session.tryLoginGradebook(); for (Student st : session.getStudents()) { st.loadGradeSummary(); for (int i = 0; i < 8; i++) System.out.println(st.getClassesForTerm(i)); for (int i = 0; i < st.getClassMatch().length; i++) { System.out.printf("%20s", st.getClassList().optJSONObject(st.getClassMatch()[i]).getString("title")); System.out.print(" "); JSONArray classGrade = st.getClassList().optJSONObject(st.getClassMatch()[i]).optJSONArray("terms"); if (classGrade.optJSONObject(0).getString("description").equals("4th Six Weeks")) System.out.printf("%20s", ""); for (int j = 0; j < classGrade.length(); j++) { int score = classGrade.optJSONObject(j).optInt("average", -1); System.out.printf("%5s", displayScore(score)); } if (classGrade.length() == 4 && classGrade.optJSONObject(0).getString("description").equals("1st Six Weeks")) System.out.printf("%20s", ""); System.out.printf(" S1:%5s", displayScore( st.getClassList().optJSONObject(st.getClassMatch()[i]).optInt("firstSemesterAverage", -1))); System.out.printf(" S2:%5s\n", displayScore(st.getClassList().optJSONObject(st.getClassMatch()[i]) .optInt("secondSemesterAverage", -1))); } boolean attendanceLoaded = false; while (!attendanceLoaded) { try { st.loadAttendanceSummary(); attendanceLoaded = true; } catch (Exception e) { e.printStackTrace(); } } /* String[] classNames = st.getAttendanceSummaryClassNames(); int[][] attendanceSummary = st.getAttendanceSummary(); for (int j = 0; j < Parser.AttendanceData.COLS.length; j++) { System.out.printf("%20s", ""); System.out.printf("%5s", Parser.AttendanceData.COLS[j]); System.out.println(); } for (int i = 0; i < classNames.length; i++) { System.out.printf("%20s", classNames[i]); for (int j = 0; j < attendanceSummary[i].length; j++) { System.out.printf("%5d", attendanceSummary[i][j]); } System.out.println(); } */ } }
From source file:RdfDataGenerationApplication.java
public RdfDataGenerationApplication(String web_sites_config_file, String database_config_file, PrintWriter out) throws Exception { this.out = out; this.database_config_file = database_config_file; // process web sites: List<String> lines = (List<String>) FileUtils.readLines(new File(web_sites_config_file)); for (String line : lines) { Scanner scanner = new Scanner(line); scanner.useDelimiter(" "); try {/*from ww w.ja va2s. c om*/ String starting_url = scanner.next(); int spider_depth = Integer.parseInt(scanner.next()); spider(starting_url, spider_depth); } catch (Exception ex) { ex.printStackTrace(); } } // after processing all 4 data sources, add more RDF statements for inter-source properties: process_interpage_shared_properties(); out.close(); }
From source file:com.it.j2ee.cache.ehcache.cluster.client2.Client2.java
@SuppressWarnings("resource") @Test/*from w w w. jav a 2 s . c o m*/ public void demo() { cache = ehcacheManager.getCache(CACHE_NAME); System.out.println("Cluster2: (q?)"); int i = 0; do { // cluster1?? System.out.println("?cluster1-key:"); String key = new Scanner(System.in).nextLine(); if ("q".equals(key)) { System.exit(0); } Object result = get(key); System.out.println("?cluster1-value:" + result); // cluster2cluster1?? System.out.println("cluster2-key:"); key = new Scanner(System.in).nextLine(); System.out.println("cluster2-value:"); Object value = new Scanner(System.in).nextLine(); put(key, value); i++; } while (i < 10); // ??? new Scanner(System.in).nextLine(); }
From source file:hu.petabyte.redflags.web.svc.OrganizationsSvc.java
@PostConstruct public void init() { try {/*from w w w. j a v a 2s . c om*/ countSql = new Scanner(countRes.getInputStream()).useDelimiter("\\Z").next(); } catch (Exception e) { e.printStackTrace(); countSql = "SELECT COUNT(*) FROM rfwl_organizations"; } try { querySql = new Scanner(queryRes.getInputStream()).useDelimiter("\\Z").next(); } catch (Exception e) { e.printStackTrace(); querySql = "SELECT * FROM rfwl_organizations LIMIT 0"; } }
From source file:instastats.InstaStats.java
private String getAccessCode() { String codePath = this.getClass().getResource("").getPath() + localCodePath; File codeFile = new File(codePath); String code = null;//from w w w .j av a 2 s.c o m try { Scanner scanner = new Scanner(codeFile); code = scanner.next(); scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return code; }
From source file:com.std.Index.java
public void find_historical_data(int month, int day, int year) { try {//w w w .j a v a 2s . co m String url = "http://real-chart.finance.yahoo.com/table.csv?s=" + this.ticker + "&d=" + month + "&e=" + day + "&f=" + year + "&g=d&a=0&b=1&c=1970&ignore=.csv"; InputStream input; input = new URL(url).openStream(); Scanner s = new Scanner(input); s.useDelimiter("\\A"); String csv = s.hasNext() ? s.next() : ""; s.close(); input.close(); csv = csv.replace("\"", ""); historical_data = new ArrayList<String>(Arrays.asList(csv.split("\n"))); } catch (Exception ex) { System.out.println("Could not retrieve historical data"); System.out.println("Error with connection"); } }
From source file:br.com.poc.navigation.NavigationLoader.java
private void startNavigation() { Scanner scanner = new Scanner(System.in); this.printLogo(); boolean continueExecution = true; do {/*from w ww.j a va2s. c om*/ this.printMenu(); final String option = scanner.next(); switch (option) { case "1": try { this.printCommandList(); String commands = scanner.next(); System.out.println("\n > Aguarde o processamento...."); System.out.println("\n > Comandos inputados: " + commands); final String distinateCoordinate = navigationComponent.traceRoute(commands).toString(); System.out.println("\n > A rota final ser: " + distinateCoordinate); } catch (ParseInputCommandException | InvalidCommandException exception) { System.out.println("\n > Ocorreram alguns erros ao processar a sequncia de comandos:\n"); final List<String> messages = ExceptionMessageCollector.getStackMessages(exception); messages.stream().forEach((message) -> { System.out.println(message); }); } break; case "2": System.out.println("\n > Obrigado por navegar conosco! \n"); scanner.close(); continueExecution = false; break; default: System.out.println("\n > Opo de menu invlida."); break; } } while (continueExecution); }