我正在使用Eclipse上的Java程序。该程序从用户获取“txt”文件名,并打印出该“txt”文件中的行数(“文件中有”x“行”)。如果“txt”文件不存在,则应打印出“该文件不存在”
我试图让程序继续循环,询问文件名,无论最后一个条目是否导致错误,直到用户输入“完成”。
这是我正在编辑的代码。共有2个班级。这是第一个(ProcessFile.java):
// ProcessFile.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* This class will ask the user for a text file
* name, open that file, and print all the lines
* of the file and how many lines there are.
*
*
*/
public class ProcessFile {
/**
* This method prompts the user to enter a
* file name and returns it.
*
* @return A String containing a file name.
*/
public static String getFileName()
{
Scanner in = new Scanner( System.in );
String fileName;
FileReader reader;
do
{
System.out.println( "Please enter a file name to open: " );
fileName = in.nextLine();
try
{
reader = new FileReader( fileName );
}
catch( FileNotFoundException e )
{
System.out.println( "That file does not exist." );
reader = null;
}
}
while ( reader == null );
in.close();
try
{
reader.close();
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
}
/**
* This method takes an ArrayList of Strings and prints each
* element of the ArrayList, one per line, as well as the
* number of items in the ArrayList.
*
* @param lines
*/
public static void printInformation( ArrayList<String> lines )
{
for ( String line : lines )
System.out.println( line );
System.out.println( "There are " + lines.size() + " lines in the file." );
}
public static void main( String[] args )
{
String fileName;
FileManager fileInfo;
ArrayList<String> lines = new ArrayList<String>();
fileName = getFileName( );
fileInfo = new FileManager( fileName );
try
{
lines = fileInfo.readLines();
}
catch( FileNotFoundException e )
{
System.out.println( e );
}
printInformation( lines );
}
}
这是第二个类(FileManager.java):
// FileManager.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class will manage the interaction with the
* file for the ProcessFile class.
*
*
*/
public class FileManager
{
private String fileName;
public FileManager( String file )
{
fileName = file;
}
/**
* This function will read the file stored in
* fileName and return an ArrayList made up of
* the lines of the file.
*
* @return An ArrayList containing the file's lines.
*/
public ArrayList<String> readLines( ) throws FileNotFoundException
{
ArrayList<String> lines = new ArrayList<String>();
FileReader fileIn = new FileReader( fileName );
Scanner in = new Scanner( fileIn );
while ( in.hasNextLine() )
{
String line = in.nextLine();
lines.add( line );
}
in.close();
return lines;
}
}
对于“如果用户键入'完成',结束程序”部分,我查找了一些内容并将其包含在下面的代码中。不确定它是否正确,但是当我输入该部分时我没有收到任何错误。这是我所做的更改(我根据我更改的部分添加了注释):
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* This class will ask the user for a text file
* name, open that file, and print all the lines
* of the file and how many lines there are.
*
*
*/
public class ProcessFile {
/**
* This method prompts the user to enter a
* file name and returns it.
*
* @return A String containing a file name.
*/
public static String getFileName() // Error: This method must return a result of type String
{
Scanner in = new Scanner( System.in );
String fileName;
FileReader reader;
int x = 1;
if (System.in.equals("Done") || System.in.equals("done")) // This is the part I wasn't sure of (the one I said I looked up)
{
System.exit(0);
}
else
{
while (x == 1)
{
System.out.println( "Please enter a file name to open: " );
fileName = in.nextLine();
try
{
reader = new FileReader( fileName );
}
catch( FileNotFoundException e )
{
System.out.println( "That file does not exist." );
}
in.close();
try
{
reader.close(); //Error: The local variable reader may not have been initialized
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
}
}
}
/**
* This method takes an ArrayList of Strings and prints each
* element of the ArrayList, one per line, as well as the
* number of items in the ArrayList.
*
* @param lines
*/
public static void printInformation( ArrayList<String> lines )
{
for ( String line : lines )
System.out.println( line );
System.out.println( "There are " + lines.size() + " lines in the file." );
}
public static void main( String[] args )
{
String fileName;
FileManager fileInfo;
ArrayList<String> lines = new ArrayList<String>();
fileName = getFileName( );
fileInfo = new FileManager( fileName );
try
{
lines = fileInfo.readLines();
}
catch( FileNotFoundException e )
{
System.out.println( e );
}
printInformation( lines );
getFileName(); // <--- Return to the top and get the user input again.
}
}
我认为我接近这个。任何一点帮助将不胜感激。非常感谢。
**编辑代码(@μTheory)
public static String getFileName()
{
Scanner in = new Scanner( System.in );
String fileName;
FileReader reader;
do
{
System.out.println( "Please enter a file name to open: " );
fileName = in.nextLine();
if ( in.equals("Done") || in.equals("done") )
{
in.close();
System.exit(0);
}
else
{
try
{
reader = new FileReader( fileName ); //Error: The local variable reader may not have been initialized.
}
catch( FileNotFoundException e )
{
System.out.println( "That file does not exist." );
}
}
in.close();
try
{
reader.close();
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
}
while ( reader == null );
}
答案 0 :(得分:3)
首先,System.in
引用InputStream
,因此您对System.in.equals("Done");
的调用会尝试比较String
和InputStream
,并且显然会返回假。而是在fileName = in.nextLine();
语句之前调用if
,然后检查是否filename.equals("Done")
。并将if语句放在while
循环中。
现在你已经创建了一个无限循环:while ( x == 1)
永远不会停止,因为你实例化x=1
并且你永远不会改变循环中的值。
我建议您在之前编程时将循环更改为while( reader == null)
。并取出while
声明并将其放在行
in.close();
try
{
reader.close();//No more error
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
为什么?因为,当reader
为null
时,您可以不关闭允许您在循环的每次迭代中调用Scanner
的{{1}}。当你想要在fileName = in.nextLine();
不为空时结束循环时,你可以不调用reader
,因为根据定义,你的reader.close();
对象将为null并抛出一个reader
。
在致电NullPointerException
之前,请先考虑关闭所有开放流,例如System.exit(0);
。
修改
Scanner
所以这是正确的代码。您仍然不明白,public static String getFileName()
{
Scanner in = new Scanner( System.in );
String fileName;
FileReader reader;
do
{
System.out.println( "Please enter a file name to open: " );
fileName = in.nextLine();
if ( fileName.equals("Done") || fileName.equals("done") )
{
in.close();
System.exit(0);
}
else
{
try
{
reader = new FileReader( fileName ); //Error: The local variable reader may not have been initialized.
}
catch( FileNotFoundException e )
{
System.out.println( "That file does not exist." );
}
}
}
while ( reader == null );
in.close();
try
{
reader.close();
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
}
您试图将in.equals("Done")
obejct(in
和Scanner
的实例进行比较,如上所述,显然是假的。因此,我将String
替换为in
代表用户输入的行。
然后我提取了块:
fileNmae
在 in.close();
try
{
reader.close();
}
catch ( IOException e )
{
System.out.println( e );
}
return fileName;
循环之外,如上所述。如果您仍在使用它们或者尚未实现它们,则无法关闭流。
答案 1 :(得分:1)
您需要从Scanner获取用户输入并将其与“完成”进行比较,而不是System.in
本身。 System.in
只是流,而非实际输入,您需要从该流读取才能进行比较。
Scanner s = new Scanner(System.in)
String inp = s.nextLine()
if(inp.equals("Done"))
{
//Code
}
而且,正如@DeiAndrei在他的回答中指出的那样,你可以使用equalsIgnoreCase
来比较它而不区分大小写。忘记了这一点,并为了完整性而添加了这个。
答案 2 :(得分:1)
您应该更换您不确定的部分:
if(in.nextLine().equalsIgnoreCase("done")){
System.exit(1);
}
您可以使用equalsIgnoreCase方法进行不区分大小写的比较。 要从控制台读取,只需使用Scanner.nextLine()或您认为适合Scanner类的任何其他方法。
如果您不想接受如下输入:
"DONE", "DoNe", etc
然后只需将输入字符串与
进行比较"Done" and "done"
和以前一样。