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 ...
随机推荐
- excel多个sheet表拆分成独立的excel文件
一.问题: 公司做一个项目,给的用户上报文是一个包含多个sheet页的excel文件, 但是在实际处理报文数据的时候需要拆分excel文件为多个独立的报文excel文件 二.解决: (1)一个一个的复 ...
- sencha touch NavigationView 嵌套 TabPanel 的问题
在st2.1之中,在NavigationView视图之中在嵌套一个TabPanel会有以下问题 下面我们监控TabPanel的activate事件和activeitemchange事件 会发现当首页加 ...
- [BeiJing2011]元素[贪心+线性基]
2460: [BeiJing2011]元素 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1245 Solved: 652[Submit][Stat ...
- 生产环境的gitlab大版本升级思路(从7.x升级到8.x)
之前在生产环境部署的gitlab是7.x版本的,提供给公司内部的员工来使用,大概有350个用户左右,gitlab从8.x版本之后内置了CI和CD的集成,所以就考虑到升级版本的问题 通过参考和总结git ...
- Altium Designer 输出 gerber 光绘文件的详细说明
Altium Designer 输出 gerber 光绘文件的详细说明 PCB画好后,我们需要输出光绘文件交给制版厂家.由此,输出光绘文件的重要性就显出来了. 先复习一下介绍各层的定义吧,哈哈 (1) ...
- -bash: locate: command not found
部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum -y ...
- 【CF633H】Fibonacci-ish II 莫队+线段树
[CF633H]Fibonacci-ish II 题意:给你一个长度为n的序列$a_i$.m个询问,每个询问形如l,r:将[l,r]中的所有$a_i$排序并去重,设得到的新数列为$b_i$,求$b_1 ...
- 使用Autolayout xib实现动态高度的TableViewCell
http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...
- lombok 转载
http://www.blogjava.net/fancydeepin/archive/2012/07/12/lombok.html LomBok主要特性有:自动生成默认的getter/setter方 ...
- Xcode - xcode-select: error: tool 'xcodebuild' requires Xcode报错解决方案
用mac 自带的终端执行的命令,安装安装Vapor和toolbox 安装指令: macdeMacBook-Pro:~ mac$ curl -sL check.vapor.sh| bash 结果报这个错 ...