zoukankan      html  css  js  c++  java
  • Java: Best Way to read a file

    经常在各种平台的online test里面不熟悉STDIN, STOUT,下面举个例子:

    Input Format

    There are three lines of input:

    1. The first line contains an integer.
    2. The second line contains a double.
    3. The third line contains a String.

    Output Format

    There are three lines of output:

    1. String

    2. Double

    3. Int

    Sample Input

    42
    3.1415
    Welcome to HackerRank's Java tutorials!
    

    Sample Output

    String: Welcome to HackerRank's Java tutorials!
    Double: 3.1415
    Int: 42

    Hi, I don't understand why we have to do a sc.nextLine(), then do it again sc.nextLine()...
    Sometimes you have to clear the buffer to print the strings by command sc.nextLine();

    Answer: After supplying data for int, we would hit the enter key. What nextInt() and nextDouble() does is it assigns integer to appropriate variable and keeps the
    enter key unread in thekeyboard buffer. so when its time to supply String the nextLine() will read the enter key from the user thinking that the user has entered the enter key.
    (that's we get empty output) . Unlike C, there is no fflush() to clean buffer, so we have to flush by not taking it in variable.


     1 import java.util.*;
     2 
     3 class Solution {
     4     
     5     public static void main(String[] args) {
     6         Scanner sc = new Scanner(System.in);
     7         int n = sc.nextInt();
     8         double x = sc.nextDouble();
     9         sc.nextLine();
    10         String str = sc.nextLine();
    11 
    12 
    13         System.out.println(str);
    14         System.out.println(x);
    15         System.out.println(n);
    16     }
    17 }
  • 相关阅读:
    iOS书写高质量代码之耦合的处理
    下载历史版本App超详细教程
    iOS-申请邓白氏编码的超详细流程介绍
    AFNetworking之于https认证
    iOS时间问题
    AFNetworking到底做了什么?(二)
    AFNetworking到底做了什么
    iOS7中的ViewController切换
    iOS 视图控制器转场详解
    关于如何写UI及屏幕适配的一些技巧
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4200279.html
Copyright © 2011-2022 走看看