简体   繁体   English

虽然hasNext()不会结束

[英]While hasNext() won't end

I have a program for an assignment that I cannot puzzle out the final tidbit. 我有一个分配任务的程序,我无法弄清楚最后的花絮。 It needs to be able to accept a large snippet of "DNA" code. 它需要能够接受较大的“ DNA”代码段。 The samples given are in the 100,000+ range. 给出的样本在100,000+范围内。 I wrote at first what wold work for a small sample, one line, wonderfully. 首先,我写了一篇精彩的论文,写了一小段样本是如何工作的。 The TA told me that I should be able to add a while (input.hasNext()) and it will complete more than just the first line of the sample file I am copying and pasting into the console. TA告诉我,我应该可以添加while (input.hasNext()) ,它不仅可以完成要复制并粘贴到控制台的示例文件的第一行,而且还可以完成更多工作。 It certainly does that! 当然可以! It just won't end. 只是不会结束。 I tried using a break; 我试着break; where I thought would be appropriate but end up back to where I was with just a single line being counted. 我认为合适的位置,但最后只返回一行,回到原来的位置。

Scanner scan = new Scanner(System.in); //Scanner

System.out.println("Enter a DNA sequence consisting of A, T, G, and C, on one line: "); //Instructions for user.
dnaSequence = scan.nextLine();                                                          //Scan for next line of string.
dnaSequence = dnaSequence.toUpperCase();                                                //Converts all letters entered upper case to avoid issues.

while(scan.hasNext()){
    for (int i = 0; i < dnaSequence.length(); i++) //Make i = 0, i has to be less than the length of the entered sequence, will excute count.
    {
        validCount = dnaSequence.charAt(i);    //[FILL IN!]
        switch (validCount)                    //Switch for all valid counts
        {
        case 'A' :    //For any case A.
            countA++; //Count all As.
            break;
        case 'T' :    //For any case T.
            countT++; //Count all Ts.
            break;
        case 'C' :    //For any case C.
            countC++; //Count all Cs.
            break;
        case 'G' :    //For any case G.
            countG++; //Count all Gs.
            break;
        }
    }

    totalCountGC = countG + countC;                         //Math for G and C, together.
    totalCountSequence = countA + countT + countG + countC; //Math for total count of all other counts in switch.

You never consume any of the input inside the loop. 您永远不会消耗循环内的任何输入。 The only time you actually read new data is before you enter the while loop, on this line: 实际读取新数据的唯一时间是在进入while循环之前的这一行:

dnaSequence = scan.nextLine();

So basically all you're doing is reading a single line from your input, and then proceeding to do your calculations on that same line over and over. 因此,基本上您要做的就是从输入中读取一行,然后一遍又一遍地在同一行上进行计算。

Move that, and the toUpperCase inside the loop, and it will continue to read new lines in, and eventually consume all the input. 将其移动,并将toUpperCase放入循环内,它将继续读取新行,并最终消耗所有输入。 So your code would look something like this: 因此,您的代码将如下所示:

while(scan.hasNextLine()){
    String dnaSequence = scan.nextLine().toUpperCase();

    for (int i = 0; i < dnaSequence.length(); i++){
        validCount = dnaSequence.charAt(i);
        switch (validCount){
        case 'A' :
            countA++;
            break;
        case 'T' :
            countT++;
            break;
        case 'C' :
            countC++;
            break;
        case 'G' :
            countG++;
            break;
        }
    }
}

In this, I'm assuming that you're using something like input redirection from a file for your input, and not typing in the lines manually. 在此,我假设您正在使用类似从文件中进行输入重定向之类的内容,而不是手动输入行。 If you do actually type them in at run time, then this won't work, since there's no way for the program to know when you're done. 如果您确实在运行时输入了它们,那么它将无法正常工作,因为程序无法知道何时完成操作。

System.in is a input stream which usually System.in是一个输入流,通常

  • reads data from console 从控制台读取数据
  • and is opened entire time application is working. 并在整个应用程序正常工作时打开。

Now since it is possible that user is in the middle of creating data which will be send to that stream (he can be writing it in console but didn't press enter yet) hasNext needs to wait with decision until 现在,由于用户可能正在创建要发送到该流的数据(他可以在控制台中编写数据,但尚未按Enter键),因此hasNext需要等待决策直到

  • user will decide to sent his data, 用户将决定发送其数据,
  • or until stream will be closed (like in case of end of file) 或直到流将被关闭(如文件结尾)

To avoid this problem you can let user provide some sort of special value which will end loop like 为避免此问题,您可以让用户提供某种特殊值,该特殊值将终止循环,例如

while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    if ("EXIT".equals(line))
        break;
    //if we are here it means loop should continue 
    handle(line);//or other code which will handle your data
}

To avoid infinite waiting on opened stream (like System.in ), you can choose other option. 为了避免无限等待打开的流(例如System.in ),可以选择其他选项。 Simply make Scanner read from source which has end , like File. 只需使Scanner从具有end的源文件(如File)中读取。

So your code can look like: 因此您的代码如下所示:

//input.txt should contain data you want to read
File data = new File("path/to/your/input.txt");
Scanner scanner = new Scanner(data);
while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    //handle this line
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM