我正在尝试使用java扫描程序输入从键盘读取多个输入(数字作为字符串,例如12345)并发送到文本文件,我有一个扫描仪读取input.nextLine(),它将读入每一行。我只是想运行我的程序并使其保持运行,因此它将使用每个扫描仪输入填充文本文件。我使用以下代码:
import java.util.*;
import java.io.*;
public class tap1
{
public static void main(String[] args) throws IOException
{
System.out.println("Please enter number:");
File outFile = new File ("CardNumbers.txt");
FileWriter fWriter = new FileWriter (outFile, true);
PrintWriter pWriter = new PrintWriter (fWriter);
Scanner scan = new Scanner(System.in);
String num = scan.nextLine();
pWriter.println (num);
pWriter.close();
}
}
答案 0 :(得分:0)
您可以将其包装在像
这样的循环中 while(scanner.hasNext){
scanner.nextLine();
//Do some code
}
在达到EOF之前,您将继续“扫描”新输入,直到您的程序继续运行。
答案 1 :(得分:0)
你可以试试这个
import java.util.*;
import java.io.*;
public class tap1
{
public static void main(String[] args) throws IOException
{
System.out.println("Please enter number:");
File outFile = new File ("CardNumbers.txt");
FileWriter fWriter = new FileWriter (outFile, true);
PrintWriter pWriter = new PrintWriter (fWriter);
Scanner scan = new Scanner(System.in);
String num = "";
while(true){
num=scan.nextLine();
if(num.equals("exit")){
break;
}
pWriter.println (num);
}
pWriter.close();
}
}