BufferedReader.nextLine() 不会前进到下一行,所以我陷入了循环
我正在尝试重用 Scanner(System.in)
和 BufferedReader(FileReader)
的输入方法。当用户输入“readfile”作为命令时,程序将文件中的第一个人(即文件中的第一行)添加到列表中,但 BufferedReader 被卡在同一行上,并且因此陷入了无限循环。
键盘输入的 Scanner
方法就像一个魅力。
class Menu {
public static final int ARRAYINDEXFOREMAIL = 3;
private static final String NEWPERSON = "newperson";
private static final String FROMFILE = "readfile";
public static final String READFILE = "saved.txt";
private Scanner keyboardInput = new Scanner( System.in );
private String name;
private String tlf;
private String[] email;
private String [] stringTmp;
private String in;
private boolean readFromFile = false;
Menu( boolean fileRead ) {
if( fileRead ) {
readFromFile = true;
}
else {
readFromFile = false;
}
}
void run() {
while(true) {
System.out.print("\n" + "ordre> ");
if ( !readFromFile ) {
in = keyboardInput.nextLine();
}
else {
try {
BufferedReader fileOut = new BufferedReader(
new FileReader( READFILE ) );
if( ( in = fileOut.readLine() ) == null ) {
readFromFile = false;
fileOut.close();
break;
}
else {
System.out.println("in stringTmp is: " + in); // For debugging purposes
}
}
catch (FileNotFoundException filenotfoundexception) {
System.out.println("Cannot find file: " + READFILE + " !");
}
catch (IOException ioexception) {
ioexception.printStackTrace();
}
}
stringTmp = in.split("\\s+");
if( stringTmp[0].equalsIgnoreCase(NEWPERSON) ) {
if( stringTmp.length <= 1 ) {
System.out.println("ERROR! Enter one or more parameters..");
continue;
}
else {
name = stringTmp[1];
tlf = stringTmp[2];
if( stringTmp.length > ARRAYINDEXFOREMAIL ) {
email = new String[stringTmp.length - ARRAYINDEXFOREMAIL];
for( int i = ARRAYINDEXFOREMAIL, j = i - ARRAYINDEXFOREMAIL ; i < stringTmp.length; i++, j++ ) {
email[j] = stringTmp[i];
}
}
else {
email = new String[0];
}
mainlist.addPerson( name, tlf, email );
}
}
else if ( stringTmp[0].equalsIgnoreCase(FROMFILE) ) {
Meny m = new Menu( true );
m.run();
}
}
}
}
I am trying to reuse input method for both Scanner(System.in)
and BufferedReader(FileReader)
. When the user types "readfile" as a command, the program adds the first person from the file (ie the first line from the file) to a list, but the BufferedReader
is stuck on the same line and therefore is stuck in an infinite loop.
The Scanner
method from keyboard input works like a charm.
class Menu {
public static final int ARRAYINDEXFOREMAIL = 3;
private static final String NEWPERSON = "newperson";
private static final String FROMFILE = "readfile";
public static final String READFILE = "saved.txt";
private Scanner keyboardInput = new Scanner( System.in );
private String name;
private String tlf;
private String[] email;
private String [] stringTmp;
private String in;
private boolean readFromFile = false;
Menu( boolean fileRead ) {
if( fileRead ) {
readFromFile = true;
}
else {
readFromFile = false;
}
}
void run() {
while(true) {
System.out.print("\n" + "ordre> ");
if ( !readFromFile ) {
in = keyboardInput.nextLine();
}
else {
try {
BufferedReader fileOut = new BufferedReader(
new FileReader( READFILE ) );
if( ( in = fileOut.readLine() ) == null ) {
readFromFile = false;
fileOut.close();
break;
}
else {
System.out.println("in stringTmp is: " + in); // For debugging purposes
}
}
catch (FileNotFoundException filenotfoundexception) {
System.out.println("Cannot find file: " + READFILE + " !");
}
catch (IOException ioexception) {
ioexception.printStackTrace();
}
}
stringTmp = in.split("\\s+");
if( stringTmp[0].equalsIgnoreCase(NEWPERSON) ) {
if( stringTmp.length <= 1 ) {
System.out.println("ERROR! Enter one or more parameters..");
continue;
}
else {
name = stringTmp[1];
tlf = stringTmp[2];
if( stringTmp.length > ARRAYINDEXFOREMAIL ) {
email = new String[stringTmp.length - ARRAYINDEXFOREMAIL];
for( int i = ARRAYINDEXFOREMAIL, j = i - ARRAYINDEXFOREMAIL ; i < stringTmp.length; i++, j++ ) {
email[j] = stringTmp[i];
}
}
else {
email = new String[0];
}
mainlist.addPerson( name, tlf, email );
}
}
else if ( stringTmp[0].equalsIgnoreCase(FROMFILE) ) {
Meny m = new Menu( true );
m.run();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,在循环本身中,您设置
这将导致创建一个新的 BufferedReader!
在循环的下一次迭代中 - 您将重复此行,重置
fileOut
作为结果 - 当您调用fileOut.readLine()
时 - 它将读取第一个再次排队。您应该在循环之前而不是在循环中初始化
fileOut
来解决这个问题。Note that in the loop itself you set
This will cause a new
BufferedReader
to be created!In the loop's next iteration - you will repeat this line, reset
fileOut
as a result - and when you invokefileOut.readLine()
- it will read the first line again.You should initialize
fileOut
before the loop and not in it to solve it.