在C ++中如果我想读取输入直到EOF,我可以按照以下方式进行操作
while(scanf("%d",&n))
{
A[i]=n;
i++;
}
然后我可以将此代码作为./a.out< input.txt中。这段代码的java等价物是什么?
答案 0 :(得分:14)
你可以这样做:
Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
A[i] = s.nextInt();
i++;
}
答案 1 :(得分:9)
// assuming that reader is an instance of java.io.BufferedReader
String line = null;
while ((line = reader.readLine()) != null) {
// do something with every line, one at a time
}
如果您遇到困难,请告诉我。
答案 2 :(得分:2)
import java.io.BufferedReader;
import java.io.FileReader;
BufferedReader br = null;
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
}
//using Scanner class
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
答案 3 :(得分:2)
这是使用BufferedReader和FileReader类的Java等效代码。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SmallFileReader {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
String line=nul;
while( (line=br.readLine()) != null) {
System.out.println(line);
}
}
}
答案 4 :(得分:1)
这是使用BufferedReader和FileReader类的Java等效代码。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SmallFileReader {
public static void main(String[] args) throws IOException {
选项1:
String fileName = args[0];
BufferedReader br = new BufferedReader(new FileReader(fileName));
选项2:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a file name: ");
String fileName = br.readLine();
//BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
String line=null;
while( (line=br.readLine()) != null) {
System.out.println(line);
}
}
}
我对@Vallabh Code做了一些修改。
@tom如果要通过命令行输入文件名,可以使用第一个选项
java SmallFileReader Hello.txt
选项2将在您运行文件时询问您的文件名。
答案 5 :(得分:1)
唯一对我有用的东西(你甚至不需要创建文件)
Scanner read = new Scanner(System.in);
String cadena;
boolean cond = true;
int i =0;
while (cond){
cadena = read.nextLine();
if(cadena.isEmpty())
cond = false;
}
答案 6 :(得分:0)
一个简单的解决方案是使用Scanner
类。
请参见下面的摘录:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNextLine())
{
String line = s.nextLine();
System.out.println(line);
}
}
}
答案 7 :(得分:0)
这是我的Java等效代码,用于读取输入,直到文件结束:
import java.util.Scanner;
public class EndOfFileSolutions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i = 1; sc.hasNext()== true; i++){
System.out.println(i + " " + sc.nextLine());
}
}
}
此代码将产生如下所示的输出-
Hello world
I am a file
Read me until end-of-file.
1 Hello world
2 I am a file
3 Read me until end-of-file.
此答案也可以很好地解决Hackerrank EOF problem
答案 8 :(得分:0)
在Java中,读到EOF的一种方法是:-
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) //read until condition is true
{
String line = scan.nextLine();
System.out.println(line);
i++;
}
答案 9 :(得分:0)
Scanner scanner = new Scanner(System.in);
int c = 0;
while(scanner.hasNext()){
System.out.println(++c + " " + scanner.nextLine());
}
scanner.close();
// use while instead of normal for loop.
// If you need to read a file than BufferReader is the best way to do it, as explained above.
答案 10 :(得分:-1)
EOF 或行尾..!可以在单个 while 循环条件中实现.. !!,直到条件有效循环运行并从用户那里获取输入。“.hasNextLine”,正在检查是否有另一个输入行。例如,我正在使用 Hackerrank 练习。 希望这对你有帮助。
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int i=1;
Scanner sc = new Scanner(System.in); // Making Scanner object
while(sc.hasNextLine()){ // This is the main condition for EOF
String line = sc.nextLine();
System.out.format("%d %s\n",i,line);
i++;
}
}
}