List of usage examples for java.util Scanner Scanner
public Scanner(ReadableByteChannel source)
From source file:flashcrawler.FlashCrawler.java
/** * @param args the command line arguments *///from w ww.ja va 2 s . co m public static void main(String[] args) throws FileNotFoundException { Scanner scn = new Scanner(new File("input.txt")); ArrayList<String> ins = new ArrayList(); while (scn.hasNextLine()) { String input = scn.nextLine(); ins.add(input); } String URL; PrintWriter writer = null; writer = new PrintWriter(new FileOutputStream(new File("error-log.txt"), true)); String File; for (String name : ins) { File offlinePath = new File("/games/" + name + ".swf"); String onlinePath = "http://wsh.gamib.com/x/" + name + "/" + name + ".swf"; System.out.println("Downloading " + onlinePath + " into " + offlinePath); URL url = null; try { System.out.println("..."); url = new URL(onlinePath); } catch (MalformedURLException ex) { System.out.println("Failed to create url object"); writer.println("Error when creating url: " + onlinePath + "\tname"); } try { System.out.println("..."); FileUtils.copyURLToFile(url, offlinePath); System.out.println("Success."); } catch (IOException ex) { System.out.println("Error when downloading game: " + offlinePath); writer.println("Error when downloading game: " + offlinePath); } } writer.close(); System.out.println("Process complete!"); }
From source file:math2605.gn_log.java
/** * @param args the command line arguments *///from w ww. j a va 2s.c om public static void main(String[] args) { //get file name System.out.println("Please enter a file name:"); Scanner scanner = new Scanner(System.in); String fileName = scanner.nextLine(); List<String[]> pairs = new ArrayList<>(); //get coordinate pairs and add to arraylist try { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { String[] pair = line.split(","); pairs.add(pair); } br.close(); } catch (Exception e) { } System.out.println("Please enter the value of a:"); double a = scanner.nextInt(); System.out.println("Please enter the value of b:"); double b = scanner.nextInt(); System.out.println("Please enter the value of c:"); double c = scanner.nextInt(); //init B, vector with 3 coordinates RealMatrix B = new Array2DRowRealMatrix(3, 1); B.setEntry(0, 0, a); B.setEntry(1, 0, b); B.setEntry(2, 0, c); System.out.println("Please enter the number of iteration for the Gauss-newton:"); //init N, number of iterations int N = scanner.nextInt(); //init r, vector of residuals RealMatrix r = new Array2DRowRealMatrix(); setR(pairs, a, b, c, r); //init J, Jacobian of r RealMatrix J = new Array2DRowRealMatrix(); setJ(pairs, a, b, c, r, J); System.out.println("J"); System.out.println(J); System.out.println("r"); System.out.println(r); RealMatrix sub = findQR(J, r); for (int i = N; i > 0; i--) { B = B.subtract(sub); double B0 = B.getEntry(0, 0); double B1 = B.getEntry(1, 0); double B2 = B.getEntry(2, 0); //CHANGE ABC TO USE B0, B1, B2 setR(pairs, B0, B1, B2, r); setJ(pairs, B0, B1, B2, r, J); } System.out.println("B"); System.out.println(B.toString()); }
From source file:math2605.gn_exp.java
/** * @param args the command line arguments *//* ww w . j a va 2 s . c o m*/ public static void main(String[] args) { //get file name System.out.println("Please enter a file name:"); Scanner scanner = new Scanner(System.in); String fileName = scanner.nextLine(); List<String[]> pairs = new ArrayList<>(); //get coordinate pairs and add to arraylist try { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { String[] pair = line.split(","); pairs.add(pair); } br.close(); } catch (Exception e) { } System.out.println("Please enter the value of a:"); double a = scanner.nextInt(); System.out.println("Please enter the value of b:"); double b = scanner.nextInt(); System.out.println("Please enter the value of c:"); double c = scanner.nextInt(); //init B, vector with 3 coordinates RealMatrix B = new Array2DRowRealMatrix(3, 1); B.setEntry(0, 0, a); B.setEntry(1, 0, b); B.setEntry(2, 0, c); System.out.println("Please enter the number of iteration for the Gauss-newton:"); //init N, number of iterations int N = scanner.nextInt(); //init r, vector of residuals RealMatrix r = new Array2DRowRealMatrix(); setR(pairs, a, b, c, r); //init J, Jacobian of r RealMatrix J = new Array2DRowRealMatrix(); setJ(pairs, a, b, c, r, J); System.out.println("J"); System.out.println(J); System.out.println("r"); System.out.println(r); RealMatrix sub = findQR(J, r); for (int i = N; i > 0; i--) { B = B.subtract(sub); double B0 = B.getEntry(0, 0); double B1 = B.getEntry(1, 0); double B2 = B.getEntry(2, 0); //CHANGE ABC TO USE B0, B1, B2 setR(pairs, B0, B1, B2, r); setJ(pairs, B0, B1, B2, r, J); } System.out.println("B"); System.out.println(B.toString()); }
From source file:divconq.hub.Daemon.java
public static void main(String[] args) { try {/*from w w w . j ava2s. c o m*/ Daemon.startService(args); try (Scanner scan = new Scanner(System.in)) { System.out.println("Press enter to end Daemon"); scan.nextLine(); } } catch (Exception x) { } Daemon.stopService(args); }
From source file:Retirement.java
public static void main(String[] args) { // read inputs Scanner in = new Scanner(System.in); System.out.print("How much money do you need to retire? "); double goal = in.nextDouble(); System.out.print("How much money will you contribute every year? "); double payment = in.nextDouble(); System.out.print("Interest rate in %: "); double interestRate = in.nextDouble(); double balance = 0; int years = 0; // update account balance while goal isn't reached while (balance < goal) { // add this year's payment and interest balance += payment;/* w ww. jav a 2s. c om*/ double interest = balance * interestRate / 100; balance += interest; years++; } System.out.println("You can retire in " + years + " years."); }
From source file:LotteryDrawing.java
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); // fill an array with numbers 1 2 3 . . . n int[] numbers = new int[n]; for (int i = 0; i < numbers.length; i++) numbers[i] = i + 1;//from ww w .j ava 2 s .com // draw k numbers and put them into a second array int[] result = new int[k]; for (int i = 0; i < result.length; i++) { // make a random index between 0 and n - 1 int r = (int) (Math.random() * n); // pick the element at the random location result[i] = numbers[r]; // move the last element into the random location numbers[r] = numbers[n - 1]; n--; } // print the sorted array Arrays.sort(result); System.out.println("Bet the following combination. It'll make you rich!"); for (int r : result) System.out.println(r); }
From source file:BigIntegerTest.java
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); /*/* w w w. ja v a2 s .c o m*/ * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k) */ BigInteger lotteryOdds = BigInteger.valueOf(1); for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i)); System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!"); }
From source file:fitmon.Client.java
public static void main(String[] args) throws IOException, ClientProtocolException, NoSuchAlgorithmException, InvalidKeyException, SAXException, ParserConfigurationException { Food food = null;//from w w w . j a v a2 s . c o m DietAPI dApi = new DietAPI(); JDBCConnection jdbcCon = new JDBCConnection(); ArrayList<Food> foodItems = dApi.getFood(); Scanner scan = new Scanner(System.in); Calendar currentDate = Calendar.getInstance(); //Get the current date SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); //format it as per your requirement String dateNow = formatter.format(currentDate.getTime()); System.out.println("Now the date is :=> " + dateNow); for (int i = 0; i < foodItems.size(); i++) { food = foodItems.get(i); System.out.println("ID : " + food.getFoodID()); System.out.println("servingID : " + food.getServingID()); System.out.println("Name : " + food.getItemName()); System.out.println("category : " + food.getCategory()); System.out.println("Quantity : " + food.getQuantity()); System.out.println("calories : " + food.getCalories()); System.out.println("fat : " + food.getFat()); System.out.println("carbs : " + food.getCarbs()); System.out.println("protein : " + food.getProtein()); System.out.println("fiber : " + food.getFiber()); System.out.println("sodium : " + food.getSodium()); System.out.println("sugar : " + food.getSugar()); System.out.println("cholesterol : " + food.getCholesterol()); System.out.println( "------------------------------------------------------------------------------------------------"); } System.out.println("Choose a meal......"); String mealType = scan.next(); System.out.println("Choose an item......"); String servingID = scan.next(); for (int j = 0; j < foodItems.size(); j++) { if (foodItems.get(j).getServingID() == null ? servingID == null : foodItems.get(j).getServingID().equals(servingID)) { food = foodItems.get(j); break; } } Diet diet = new CustomizedDiet(); diet.createDiet(food, mealType, dateNow); }
From source file:aula1.Aula1.java
/** * @param args the command line arguments *//*from ww w . ja va 2 s . c o m*/ public static void main(String[] args) { Scanner dados = new Scanner(System.in); int escolha; double ini; double fim; String exp; String info; int sens = 0; boolean valida = true; do { System.out.println( "Decida a operao: 0 para montar grfico, 1 para calcular limite, 2 para calcular qualquer expresso"); escolha = dados.nextInt(); switch (escolha) { case 0: System.out.println("Informe o inicio do dominio"); ini = dados.nextDouble(); System.out.println("Informe o fim do dominio"); fim = dados.nextDouble(); System.out.println("Informe a quantidade de casas decimais desejadas: "); sens = dados.nextInt(); System.out.println("Informe a expresso: "); exp = dados.next(); double[] x = montaDominio(ini, fim, sens); calcula(x, exp); valida = false; break; case 1: System.out.println("Informe a expresso: "); exp = dados.next(); System.out.println("Informe o ponto limite: "); info = dados.next(); try { calculaLimite(exp, info); } catch (Exception ex) { System.out .println("Erro: " + ex.getMessage() + "Tentativade aplcao do teorema do confronto"); } valida = false; break; case 2: System.out.println("Informe a expresso:"); info = dados.next(); try { System.out.println(conversor(info, "0")); } catch (Exception ex) { System.out.println("Erro: " + ex.getMessage()); } valida = false; break; default: System.out.println("Escolha invlida!"); break; } } while (valida); // Double[] vet = new Double[3]; // vet = lerDados(); // montaDominio(vet[0], vet[1], vet[2].intValue()); // calculaLimite(3); }
From source file:SetTest.java
public static void main(String[] args) { Set<String> words = new HashSet<String>(); // HashSet implements Set long totalTime = 0; Scanner in = new Scanner(System.in); while (in.hasNext()) { String word = in.next();//from w w w. jav a 2 s. c om long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); }