尝试从具有不同内容的文件读取双精度数据。例如,如果它是一个double,那么该消息应该是“Double number is 23.5”。如果不是双数,则该消息应该是“六十三不是双数”。文件内容是打开文件并读取双精度数据
97.9
100.1
三
63个
12.4
3002.4
酷
34.6
这是它
............
我写的代码打开文件和扫描下一个行但似乎没有正常工作。
class ReadDouble
{
Scanner scan = new Scanner(System.in);
try
{
File textFile = new File ("doubleData.txt");
Scanner scanFile = new Scanner (textFile);
String str = scan.nextLine();
while(scanFile.hasNextLine())
{
double num = Double.parseDouble(str);
if(str == num)
{
System.out.println("Double number is" + str);
}
}//end while
}//end try
catch (NumberFormatException nfe)
{
System.out.println(str + "Is not a Double number");
}
}
} //结束类
'scan.nextLine()'应该在你的while循环中,否则你不会经过第一行。 – Charlie