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 ...
随机推荐
- angularjs实现选项卡实例
注意:事件.循环.赋值在一起就出错 错误实例: <!DOCTYPE html> <html ng-app="tab_switch"> <head> ...
- maven用变量的方法统一管理jar包版本
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 前端开发利器 Emmet 介绍与基础语法教程
在前端开发的过程中,编写 HTML.CSS 代码始终占据了很大的工作比例.特别是手动编写 HTML 代码,效率特别低下,因为需要敲打各种“尖括号”.闭合标签等.而现在 Emmet 就是为了提高代码编写 ...
- 安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605错误 解决方案
重装数据库服务器上的SQL SERVER 2008 上遇到了以下问题 标题: SQL Server 安装程序失败. SQL Server 安装程序遇到以下错误: MsiGetProductInfo 无 ...
- Mongoose学习参考资料
我们学习node操作mongodb数据库时,可能经常要用到MongoDB 数据库的模型工具Mongoose,下面是我整理的一些学习资料: 番外篇之——使用 Mongoose:https://githu ...
- C++类继承示例
C++的子类与孙子类都实现了虚函数时,孙子类的实现会覆盖掉子类的实现. 继承的最主要的应用就是把不同的类放到一个数组中,然后遍历调用同名函数. 实例如下: #include <iostream& ...
- SQL已存在则更新不存在则插入
不废话,下代码. replace into T_Life_UMessage(message_id,account,isread,isdelete)values(?,?,1,1) 意思是若不存在则插入要 ...
- poj 2125 Destroying The Graph 最小割+方案输出
构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), 连边 (a1,b2),容量为正无穷大 则该 ...
- Asp.net MVC]Asp.net MVC5系列——Routing特性
目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...
- ELK之nginx日志使用json格式输出
json Nginx默认日志输出格式为文本非json格式,修改配置文件即可输出json格式便于收集以及绘图 修改nginx配置文件添加配置,增加一个json输出格式的日志格式 log_format a ...