尝试仅在文件包含数字和单词的情况下读取双数字。到目前为止我所做的代码可以正确地从文件中读取并打印带有数字和单词的完整列表。不确定如何排除单词并打印只读双精度数。
public void doubleFromFile()
{
Scanner scan = new Scanner(System.in);
Scanner fileScan;
boolean validName = false;
double num = 0.0;
do
{
System.out.print("Enter file name: ");
String str = scan.nextLine();
try
{
fileScan = new Scanner(new File(str));
validName = true;
while(fileScan.hasNextLine())
{
try
{
str = fileScan.nextLine();
num = Double.parseDouble(str);
System.out.println(str);
}
catch (NumberFormatException nfe)
{
System.out.println("is not a Double number");
}
}//end while
}// end try
catch (FileNotFoundException fnfe)
{
System.out.println("Invalid File name: enter again");
}
}while(!validName);
}//end doubleFromFile method