Java: Best Way to read a file
经常在各种平台的online test里面不熟悉STDIN, STOUT,下面举个例子:
Input Format
There are three lines of input:
- The first line contains an integer.
- The second line contains a double.
- 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!Hi, I don't understand why we have to do a sc.nextLine(), then do it again sc.nextLine()...
Double: 3.1415
Int: 42
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.
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double x = sc.nextDouble();
sc.nextLine();
String str = sc.nextLine();
System.out.println(str);
System.out.println(x);
System.out.println(n);
}
}
Java: Best Way to read a file的更多相关文章
- Android(java)学习笔记87:File类使用
package cn.itcast_01; import java.io.File; /* * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式. * 而Java就提供了一个类File供我们使用 ...
- Java基础-IO流对象之File类
Java基础-IO流对象之File类 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.IO技术概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下 ...
- Android(java)学习笔记26:File类的使用
1. File类的使用 package cn.itcast_01; import java.io.File; /* * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式. * 而Java就提供 ...
- Java基础之I/O和file
五.IO流1.IO流概述 (1)用来处理设备(硬盘,控制台,内存)间的数据. (2)java中对数据的操作都是通过流的方式. (3)java用于操作流的类都在io包中. (4)按照流操作的数据的类型不 ...
- Linux下修改默认字符集--->解决Linux下Java程序种中文文件夹file.isDirectory()判断失败的问题
一.问题描述: 一个项目中为了生成树状目录,调用了file.listFiles()方法,然后利用file.isDirectory()方法判断是否为目录,该程序在windows下运行无问题,在Linux ...
- JAVA基础学习day21--IO流三-File、Properties、PrintWriter与合并、分割流
一.File 1.1.File概述 文件和目录路径名的抽象表示形式. 用户界面和操作系统使用与系统相关的路径名字符串 来命名文件和目录.此类呈现分层路径名的一个抽象的.与系统无关的视图.抽象路径名 有 ...
- Java文件IO操作应该抛弃File拥抱Paths和Files
Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...
- Java基础——常用类(Date、File)以及包装类
本文要点: 基本数据类型的包装类 字符串相关类: 不可变字符序列:String 可变字符序列:StringBuffer.StringBuilder 时间处理相关类: Date DateFormat.S ...
- Java Bad version number in .class file
错误信息: java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLo ...
- java移动/赋值文件 copy/move file
public class FileAccess { public static boolean Move(File srcFile, String destPath) { // Destination ...
随机推荐
- tar解压包的时候出现错误 gzip: stdin: not in gzip format
在Linux环境下,通过tar -zxvf 命令解压文件时遇到”gzip: stdin: not in gzip format“等错误:如图所示 root@cmfchina:/usr/java# ta ...
- mac下用户用户组命令行操作
使用mac的时候需要像linux一样对用户和群组进行操作,但是linux使用的gpasswd和usermod在mac上都不可以使用,mac使用dscl来对group和user操作. 查看用户组: ds ...
- Node.j中path模块对路径的操作
一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...
- Unity3D Shader描边效果
Shader "Custom/RimColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whit ...
- demo:使用数字证书进行数字签名和加密,解密
下边是一个使用数字证书来进行数字签名(以及验证签名信息),以及非对称加密的一个demo,代码中使用PKCS12类型的keystore(包含私钥)使用JKS或者其他类型的keystore也是可以的,就是 ...
- 【CF819C】Mister B and Beacons on Field 数学
[CF819C]Mister B and Beacons on Field 题意:外星人盯上了Farmer Jack的农场!我们假设FJ的农场是一个二维直角坐标系,FJ的家在原点.外星人向FJ的农场上 ...
- [工具] Snipaste
https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...
- 9.17 Django ORM分组
2018-9-17 19:53:22 预习:http://www.cnblogs.com/liwenzhou/p/8343243.html 新买个蓝牙挂耳耳机,感觉不错! 放上代码 笔记什么的明天继 ...
- 模拟线程安全的售票案例(java)
package try51.thread.safe; import java.util.ArrayList; import java.util.Random; import java.util.con ...
- 对Yii2中 yii\web\User的理解,和自建的app\models\User(基础版),frontend\models\User的应用原理
yii\web\User 是一个统称,为用户,没有具体实例,只能管理: 此处以app\models\User为基准: app\models\User 是映射数据表user的model类,同时也实现接口 ...