使用Java计算文件中的项目数量

huangapple go评论68阅读模式
英文:

Counting the number of items in a file using java

问题

我有一个文本文件:

2|BATH BENCH|19.00
20|ORANGE BELL|1.42
04|BOILER ONION|1.78

我需要使用JAVA获取这里的项目数量,即3个项目。这是我的代码:

int Flag=0;
File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    Flag=Flag+1;
}

它进入了一个无限循环。有人可以帮忙吗?谢谢。

英文:

I have a text file:

2|BATH BENCH|19.00
20|ORANGE BELL|1.42
04|BOILER ONION|1.78

I need to get the number of items which is 3 here using JAVA. This is my code:

int Flag=0;
	File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
	Scanner sc = new Scanner(file);
	while (sc.hasNextLine()) {
		Flag=Flag+1;
	}

It is going in an infinite loop.
Can someone please help? Thank you.

答案1

得分: 1

你必须获取下一行以避免无限循环。

int Flag = 0;
File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    sc.nextLine();
    Flag++;
}
英文:

You must get the next line to avoid an endless loop.

int Flag = 0;
File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    sc.nextLine();
    Flag++;
}

答案2

得分: 1

while (sc.hasNextLine()) {
  Flag=Flag+1;
  String line = sc.nextLine(); //Do whatever with line
}
英文:
while (sc.hasNextLine()) {
  Flag=Flag+1;
  String line = sc.nextLine(); //Do whatever with line
}

答案3

得分: 1

代码中您编写了以下内容:

int Flag=0;
File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) { // 这行代码是用来检查是否还有下一行。
    Flag=Flag+1;
}

当您编写 while (sc.hasNextLine()){} 时,它会检查是否还有下一行。

例如:
第1行:abcdefg
第2行:hijklmnop

在这种情况下,您的代码将始终停留在 第1行,并不断告诉您存在下一行。

然而,当您编写:

while(sc.hasNextLine()){
    sc.nextLine();
    Flag++;
}

Scanner 将会读取 第1行,然后因为 sc.nextLine(),它将跳转到 第2行,然后当检查 sc.hasNextLine() 时,将返回 false

英文:

In the code you have written

int Flag=0;
    File file = new File("/Users/a0r01ox/Documents/costl-tablet-automation/src/ItemUPC/ItemUPC.txt");
    Scanner sc = new Scanner(file);
    while (sc.hasNextLine()) { // this line is just checking whether there is next line or not.
        Flag=Flag+1;
    }

When you write while (sc.hasNextLine()){} it check whether there is nextLine or not.
eg <br>line 1 : abcdefg<br>
line 2: hijklmnop<br>

here your code will just be on line 1 and keep telling you that yes there is a nextLine.<br>

Whereas when you write

while(sc.hasNextLine()){
sc.nextLine();
Flag++;
}

Scanner will read the line 1 and then because of sc.nextLine() it will go to line 2 and then when sc.hasNextLine() is checked it gives false.

huangapple
  • 本文由 发表于 2020年9月28日 13:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定