使用文件的扫描仪查找所有其他整数标记
Finding every other integer token using a Scanner for the file
我正在使用 Scanner
作为文件示例,其中有 4 个男孩和 3 个女孩。每个名字后面都有一个整数(例如 Mike 24
),它以男孩开头,然后是女孩,然后是男孩,然后是女孩等等。总共有 4 个男孩和 3 个女孩,我应该数一数男孩的数量和女孩,然后将每个男孩的数字相加得出总和,然后女孩也相同。另外,当我为男孩分配 console.nextInt()
时,是否会从文件中获取数字然后分配给男孩变量?另外,console.hasNext()
是否有一个索引,如果它读取标记 #1 那么我可以说 console.hasNext() == 1;
?
示例数据:
Erik 3 Rita 7 Tanner 14 Jillyn 13 Curtis 4 Stefanie 12 Ben 6
代码:
import java.util.*;
import java.io.*;
public class Lecture07 {
public static void main(String[] args) throws FileNotFoundException{
System.out.println();
System.out.println("Hello, world!");
// EXERCISES:
// Put your answer for #1 here:
// You will need to add the method in above main(), but then call it here
Scanner console = new Scanner(new File("mydata.txt"));
boyGirl(console);
}
public static void boyGirl(Scanner console) {
int boysCount = 0;
int girlsCount = 0;
while (console.hasNext()) {
if (console.hasNextInt()) {
int boys = console.nextInt();
int girls = console.nextInt();
}
else {
console.next();
}
}
}
}
hasNext()
只会 return true
或 false
。首先你不应该在循环中执行 int boys = console.nextInt();
,因为它每次都会创建新变量并且数据将会丢失。您需要做的是将 int boys = 0;
分配给其他 2 个变量 int boysCount
和 int girlsCount
,同样适用于 int girls = 0
接下来您将需要这样的东西:
public static void boyGirl(Scanner console) {
int boysCount = 0; // here we asigning the variables that we gonna be using
int girlsCount = 0;
int boys = 0;
int girls = 0;
while (console.hasNext()) { // check if there is next element, it must be the name
console.next(); // consume the name, we do not want it. or maybe you do up to you
boys += console.nextInt(); // now get to the number and add it to boys
boysCount++; // increment the count by 1 to use later, since we found a boy
if (console.hasNext()) { // if statement to see if the boy above, is followed by a girl
console.next(); // do same thing we did to the boy and consume the name
girls += console.nextInt(); // add the number
girlsCount++; // increment girl
}
}
现在在你的 while 循环之后,你可以用变量做你想做的事,比如打印它们或其他东西。希望我能帮到你。
我正在使用 Scanner
作为文件示例,其中有 4 个男孩和 3 个女孩。每个名字后面都有一个整数(例如 Mike 24
),它以男孩开头,然后是女孩,然后是男孩,然后是女孩等等。总共有 4 个男孩和 3 个女孩,我应该数一数男孩的数量和女孩,然后将每个男孩的数字相加得出总和,然后女孩也相同。另外,当我为男孩分配 console.nextInt()
时,是否会从文件中获取数字然后分配给男孩变量?另外,console.hasNext()
是否有一个索引,如果它读取标记 #1 那么我可以说 console.hasNext() == 1;
?
示例数据:
Erik 3 Rita 7 Tanner 14 Jillyn 13 Curtis 4 Stefanie 12 Ben 6
代码:
import java.util.*;
import java.io.*;
public class Lecture07 {
public static void main(String[] args) throws FileNotFoundException{
System.out.println();
System.out.println("Hello, world!");
// EXERCISES:
// Put your answer for #1 here:
// You will need to add the method in above main(), but then call it here
Scanner console = new Scanner(new File("mydata.txt"));
boyGirl(console);
}
public static void boyGirl(Scanner console) {
int boysCount = 0;
int girlsCount = 0;
while (console.hasNext()) {
if (console.hasNextInt()) {
int boys = console.nextInt();
int girls = console.nextInt();
}
else {
console.next();
}
}
}
}
hasNext()
只会 return true
或 false
。首先你不应该在循环中执行 int boys = console.nextInt();
,因为它每次都会创建新变量并且数据将会丢失。您需要做的是将 int boys = 0;
分配给其他 2 个变量 int boysCount
和 int girlsCount
,同样适用于 int girls = 0
接下来您将需要这样的东西:
public static void boyGirl(Scanner console) {
int boysCount = 0; // here we asigning the variables that we gonna be using
int girlsCount = 0;
int boys = 0;
int girls = 0;
while (console.hasNext()) { // check if there is next element, it must be the name
console.next(); // consume the name, we do not want it. or maybe you do up to you
boys += console.nextInt(); // now get to the number and add it to boys
boysCount++; // increment the count by 1 to use later, since we found a boy
if (console.hasNext()) { // if statement to see if the boy above, is followed by a girl
console.next(); // do same thing we did to the boy and consume the name
girls += console.nextInt(); // add the number
girlsCount++; // increment girl
}
}
现在在你的 while 循环之后,你可以用变量做你想做的事,比如打印它们或其他东西。希望我能帮到你。