简体   繁体   English

如何在不吃下一个整数的情况下检查下一行?

[英]How can I check the next line without eating the next integer?

I'm grappling with a specific problem, but seeing as this is technically homework and I want to learn what I'm doing wrong, I would love a more general solution. 我正在解决一个特定的问题,但是由于这在技术上是家庭作业,并且我想了解自己做错了什么,所以我希望有一个更通用的解决方案。 Some caveats: I have to use the scanner class, and my data is not in an array or anything. 注意事项:我必须使用扫描程序类,并且我的数据不在数组或任何其他内容中。 I know from reading around on the site that BufferedReading is preferred. 通过在站点上阅读,我知道BufferedReading是首选。 From what I've read of it, I think I would prefer it too. 从我读到的内容来看,我想我也喜欢它。 But that's not what I'm allowed to work with here. 但这不是我允许在这里使用的。

I'm trying to read from a data file and then do some stuff with that file. 我正在尝试从数据文件中读取内容,然后对该文件进行一些处理。 The data goes like this: 数据如下:

1234 55 64 75
1235 44 32 12
...
nnnn xx yy zz
0
2234 47 57 67
2235 67 67 67
...
nnnn xx yy zz
0

Each line is an ID followed by three grades. 每行是一个ID,后跟三个等级。 Each class is terminated by a zero line and then the while loop starts from the top: 每个类都以零线终止,然后while循环从顶部开始:

while (classContinues == true) {                   
//get the student's data and assign it in the program
studentID = inputFile.nextInt();
programGrade = inputFile.nextInt();
midtermGrade = inputFile.nextInt();
finalGrade = inputFile.nextInt();

// in here I'm doing other stuff but I don't need help with that

// check if the class has more students
if (inputFile.nextInt() == 0) {
    classContinues = false;
} else {
    classContinues = true;
    inputFile.nextLine(); // eat the rest of the line
}
}

Now, when you run the code like this, it manages to print the output I want, but it skips every other row of data. 现在,当您像这样运行代码时,它可以打印我想要的输出,但是会跳过每隔一行数据。 Remove the inputFile.nextLine(); 删除inputFile.nextLine(); and it skips the second student ID and then messes up all the other output. 并跳过第二个学生ID,然后将所有其他输出弄乱。 So I guess what I'd like to know is what I'm doing wrong--how should I be checking for the next integer to be zero without eating the next student ID? 所以我想我想知道我在做什么错-我应该如何在不吃下一个学生证的情况下检查下一个整数是否为零?

Below piece of code will jump out of the while loop when it comes to the first '0' from the input. 当涉及到输入中的第一个“ 0”时,下面的代码将跳出while循环。 That is why it cannot catch all the records. 这就是为什么它无法捕获所有记录的原因。

if (inputFile.nextInt() == 0) {
    classContinues = false;
} else {
    classContinues = true;
    inputFile.nextLine(); // eat the rest of the line
}

And for nextInt() method, when it is called, it will return the current int value and point to the next one. 对于nextInt()方法,在调用该方法时,它将返回当前的int值并指向下一个值。

Try below while codes, it can get each line of the grade records. 在下面的代码中尝试一下,它可以获得成绩记录的每一行。 And I create an entity Named StudentGrade to store the record. 我创建了一个名为StudentGrade的实体来存储记录。 And the For each loop will print out the records stored in the list. 并且For Each循环将打印出存储在列表中的记录。

    while (classContinues == true) {
        StudentGrade stu = new StudentGrade();
        // get the student's data and assign it in the program
        int id = 0;

        if ((id = inputFile.nextInt()) != 0) {
            stu.studentID = id;
        stu.programGrade = inputFile.nextInt();
        stu.midtermGrade = inputFile.nextInt();
        stu.finalGrade = inputFile.nextInt();
        studentGrades.add(stu);
        // in here I'm doing other stuff but I don't need help with that
        // check if the class has more students
        }
        else if (!inputFile.hasNext()) {
            classContinues = false;
        }
    }

    for (StudentGrade s : studentGrades) {
        System.out.println(s);
    }

input data: 输入数据:

1234 55 64 75
1235 44 32 12
1236 23 32 32
0
2234 47 57 67
2235 67 67 67
2236 23 23 2
0

output: 输出:

1234 55 64 75
1235 44 32 12
1236 23 32 32
2234 47 57 67
2235 67 67 67
2236 23 23 2

By the way, it'd better to use Mehmet's method to get the records, it's much easier and understandable. 顺便说一句,最好使用Mehmet的方法来获取记录,这更容易理解。

PS This is my first answer in StackOverflow. PS:这是我在StackOverflow中的第一个答案。 Hope that it can help. 希望对您有所帮助。

Store every line into a String variable, parse integers from that line and then assign them by reading it from that string, not from the line itself. 将每一行存储到String变量中,解析该行中的整数,然后通过从该字符串(而不是该行本身)中读取它来分配它们。 So: 所以:

String nextLine;

while (classContinues)
{             
nextLine = inputFile.nextLine();

String[] tokens = nextLine.split(" ");

if(tokens.length == 1) //this means line has '0' character
    classContinues = false;
else
    {
    classContinues = true;

    studentID = tokens[0];
    programGrade = tokens[1];
    midtermGrade = tokens[2];
    finalGrade = tokens[3];

    // your stuff
    }
}

If there will be any kind of error that will show misleading results with this code, it is probably my bad because I don't know the rest of the project. 如果使用此代码会出现任何会显示误导性结果的错误,那可能是我的错,因为我不了解项目的其余部分。 So I posted a code similar to yours. 所以我发布了与您相似的代码。

Also you have to do a null check to that String that you got from nextLine method. 另外,您还必须对从nextLine方法获得的String进行空检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何在不转到下一行的情况下检查下一个整数(扫描仪) - Java: How to check for next ints without going to the next line (Scanner) 如何在 Java 中同时检查下一个输入是否为 &gt; 和 &lt; 时检查下一个输入是否为整数? - How can I check that the next input is an integer while checking if it's > and < at the same time in Java? 如何从 2 个整数中获得下一个最接近的 integer? - How can I get the next closest integer out of 2 integers? 如何检查textpad中的下一个空行并附加下一个输入 - How to check the next empty line in textpad and append the next input 如何检查文本文件中的下一行是否为空? - How to check if next line in text file is empty? 如何让我的扫描仪读取文件中的下一行? - How can I get my scanner to read the next line in the file? 如何转到 Thymleaf 中 th:text 的下一行? - How can i go to next line inside the th:text in Thymleaf? 如何跳过在同一行中插入的下一个变量? - How can I skip the next Variable that is inserted In the same line? 如何让JToolBars包装到下一行(FlowLayout)而不将它们隐藏在它们下方的JPanel中? - How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them? 如何读取下一行 BufferedReader? - How do I read the next line BufferedReader?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM