+ -
当前位置:首页 → 问答吧 → [已解决][java]有个关於next line既bug唔识搞

[已解决][java]有个关於next line既bug唔识搞

时间:2013-12-22

来源:互联网

RaceRecord.txt
引用:Sugus : 15.33, 11.32, 11.97
Angus : 9.31, 13.32, 10.70
Mandy : 12.18
Sandy : 14.35, 10.15
Arma : 9.86, 10.49
Golden : 12.35, 11.41
Amiee : 11.38
Athenae : 12.37
Issac : 8.96, 12.36
code:
引用:import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class RecordTest {

public static void main(String[] args) throws IOException {
FileReader reader = new FileReader ("d:/RaceRecord.txt");
Scanner scanner = new Scanner (reader);
scanner.useDelimiter(" : |, |\n");

String name = null;
Double key;

while (scanner.hasNext())
{
if (!scanner.hasNextDouble()) {
name = scanner.next();
System.out.println(name);
} else {
key = Double.parseDouble(scanner.next());
System.out.printf("%.02f %s\n", key, name);
}
}
}
}
expected result:
引用:Sugus
15.33 Sugus
11.32 Sugus
11.97 Sugus
Angus
9.31 Angus
13.32 Angus
10.70 Angus
Mandy
12.18 Mandy
Sandy
14.35 Sandy
10.15 Sandy
Arma
9.86 Arma
10.49 Arma
Golden
12.35 Golden
11.41 Golden
Amiee
11.38 Amiee
Athenae
12.37 Athenae
Issac
8.96 Issac
12.36 Issac
Wrong result:
引用:Sugus
15.33 Sugus
11.32 Sugus
11.97

Angus
9.31 Angus
13.32 Angus
10.70

Mandy
12.18

Sandy
14.35 Sandy
10.15

Arma
9.86 Arma
10.49

Golden
12.35 Golden
11.41

Amiee
11.38

Athenae
12.37

Issac
8.96 Issac
12.36 Issac
一有next line就食左个name
如果将所有row摆晒同一行就出到expected result
请指教衰左系边
大感谢

[ 本帖最后由 Qoo记 於 2013-11-28 12:54 AM 编辑 ]

作者: Qoo记   发布时间: 2013-12-22

use scanner.nextLine() when you think there is no more double to read.

作者: a8d7e8   发布时间: 2013-12-22

ching
i don't quite understand what you mean

the standard form of the record is:
name : double1, double2, ..., doublen
name2...
my approach is divide data into [name] [double1] [double2] ...
for delimiter [ : ] [, ], they work
but for [\n] or [\\n], it doesn't work

hope to see more detail
thanks_____________________________________________
for reference:
input :
引用:Sugus : 15.33, 11.32, 11.97, Angus : 9.31, 13.32, 10.70, Mandy : 12.18, Sandy : 14.35, 10.15, Arma : 9.86, 10.49, Golden : 12.35, 11.41, Amiee : 11.38, Athenae : 12.37, Issac : 8.96, 12.36
it gives out expected result

[ 本帖最后由 Qoo记 於 2013-11-28 12:42 AM 编辑 ]

作者: Qoo记   发布时间: 2013-12-22

已解决
notepad既nextline并非单纯既\n
而系\r\n

作者: Qoo记   发布时间: 2013-12-22

Good, I hope you could learn to use cross-platform technique as in #2.
引用:原帖由 Qoo记 於 2013-11-28 00:53 发表
已解决
notepad既nextline并非单纯既\n
而系\r\n

作者: a8d7e8   发布时间: 2013-12-22

我噚晚谂应该同视窗新行字元系 "\r\n" 有关,但就唔明白点解输出会唔见咗 name。
啱啱做咗个实验,原来 scanner.hasNextDouble() 系好严格,字串内唔容许空白字元(空格、\t、\r、等等),一有就传回假。
同 Double.parseDouble() 好唔同,内含空白字元系无问题。
因此,显示记录最后一个数值,其实系变数 name。

作者: fitcat07   发布时间: 2013-12-22

oh, 原来如此.

我错了.
引用:原帖由 fitcat07 於 2013-11-28 12:26 发表
我噚晚谂应该同视窗新行字元系 "\r\n" 有关,但就唔明白点解输出会唔见咗 name。
啱啱做咗个实验,原来 scanner.hasNextDouble() 系好严格,字串内唔容许空白字元(空格、\t、\r、等等), ...

作者: a8d7e8   发布时间: 2013-12-22

睇完真系明晒thx

___________________
今次真系冇活用网上resource
系stackoverflow打算问个阵有关键字即prompt我去相关post
一睇就知问题
平白晒左几日
___________________
also thanks a8d7e8 ching for replying the topic

作者: Qoo记   发布时间: 2013-12-22

热门下载

更多