Java文件结束
任务:每行将包含一个非空字符串。读到EOF。 对于每一行,打印行号,后跟一个空格和行内容 样本输入:Java文件结束,java,string,eof,Java,String,Eof,任务:每行将包含一个非空字符串。读到EOF。 对于每一行,打印行号,后跟一个空格和行内容 样本输入: import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner line = n
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner line = new Scanner(System.in);
int counter = 1;
while (line.hasNextLine()) {
String line = line.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
样本输出:
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.
说明您需要将源代码
传递给扫描仪,以便扫描仪可以从中进行扫描
若要获取用户输入,则需要使用构造函数
Scanner line = new Scanner(System.in);
int counter = 0; // Initialized out of loop.
while (line.hasNextLine()) {
String ln = line.nextLine();
System.out.println(counter +" "+ln);
counter++;
}
Scanner line = new Scanner(); // <-- YOUR ERROR - there is no constructor for the Scanner object that takes 0 arguments.
// You need to specify the environment in which you wish to 'scan'. Is it the IDE? A file? You need to specify that.
注意:确保中断while循环,否则它将进入无限循环。如果要从文件扫描,可以使用以下代码
public static void main(String[] args) {
Scanner line = new Scanner(System.in); // Added source parameter in constructor.
int counter = 1; // Initialization of counter is done outside while loop, otherwise it will always get initialized by 1 in while loop
while (line.hasNextLine()) {
String lineStr = line.nextLine(); // changed variable name to lineStr, because 2 variable can't be declared with the same name in a method.
System.out.println(counter + " " + lineStr);
counter++;
}
}
如果没有相同的名称,则不能有多个变量。您必须重命名一个行
变量
创建扫描仪时,您需要发送希望从中读取的输入流。System.in can服务器作为此流,它将从控制台读取。不过,您的问题似乎表明您希望从文件中读取。如果确实要从文件中读取,则需要创建要从中读取的文件,并将该文件发送到扫描仪,以允许扫描仪从该文件中读取
尝试:
您还有一个名为:line的重复局部变量
我建议你多读一些书来掌握变量和对象是如何工作的,而不是猜测或者被你不懂的代码欺骗。这就是你成为一名优秀程序员的原因。错误:找不到适合scannerScanner sc=new Scanner(System.in)的构造函数;另外,您正在初始化循环内的计数器变量。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File("input.txt"));
int counter = 1;
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
public class Solution {
public static void main(String[] args) {
//create the File
File file = new File(filename);
//send the file into Scanner so it can read from the file
Scanner scanner = new Scanner(file);
//initialize the counter variable
int counter = 1;
//read in the file line by line
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(counter +" "+ line);
counter++;
}
}
}
Scanner line = new Scanner(); // <-- YOUR ERROR - there is no constructor for the Scanner object that takes 0 arguments.
// You need to specify the environment in which you wish to 'scan'. Is it the IDE? A file? You need to specify that.
File readFile = new File(PATH_TO_FILE); // where PATH_TO_FILE is the String path to the location of the file
// Set Scanner to readFile
Scanner scanner = new Scanner(readFile);