从文本文件弹出双

时间:2016-01-01 18:16:27

标签: java

我想从看起来像“70.869,78.22,368.12 ......”的文本文件中弹出。我使用分隔符编写了这段代码,但它不适用于int(编译项目为空后我的控制台)。如果我将其更改为 Scanner src = new Scanner(new File("ala1.txt")); src.useDelimiter(", *"); float [] taller = new float [15]; int k = 0; // Read and sum numbers. while(src.hasNextDouble()){ //tall[i++] = scanner.nextInt(); System.out.println(src.nextDouble()); } src.close(); ,它的效果非常好。我想把这些数字放到数组中。

$table = "<table class='TestTable'><tr class='tr'>";
while($row = mysql_fetch_assoc($result)){
    $table .= "<th class='th'>";
    $table .= $row['NameOfItem'];
    $table .= "</th>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "
        <td class='td'>Minimum Bid: <b>";
        $table .= $row['MinBid'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Current Bid: <b>";
        $table .= $row['CurrentBid'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Sold By: <b>";
        $table .= $row['SoldBy'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Time Left: <b>";
        $table .= printf('%d days, %d hours, %d minutes left', $diff->d, $diff->h, $diff->i);
        $table .= "</b></td>";
}
$table .= "</tr></table>";
echo $table;

1 个答案:

答案 0 :(得分:3)

你的分隔符看起来很好,这表明问题出在其他地方。

从您的问题来看,len似乎没有将hasNextDouble视为正确的双倍。可能的问题可能是您的语言环境而非70.869期望.为双值(并且根据您的代码,您的语言环境看起来像波兰语,这证实了这一理论)。
因此,如果是这种情况,请让您的扫描程序使用其他区域设置,其中,.,而不是,,如Locale.ENGLISH

Scanner src = new Scanner("70.869, 78.22, 368.12");
src.useLocale(Locale.ENGLISH);
src.useDelimiter(", *");
while(src.hasNextDouble()){
   System.out.println(src.nextDouble());
}

输出:

70.869
78.22
368.12