简体   繁体   English

Java不会停止从输入中读取

[英]Java won't stop reading from input

Java won't stop reading from input. Java不会停止从输入中读取。 I understand that maybe this while loop might have something to do with it: 我知道也许while循环可能与此有关:

while(input.hasMoreTokens());
                {
                    array1[counter] = input.nextToken();
                    counter++;
                }

But I don't see why the loop should be a problem because I am already calling .nextToken() which should advance the token. 但是我不明白为什么循环应该是个问题,因为我已经在调用.nextToken()来推进令牌了。

Here's the full source code: 这是完整的源代码:

import java.io.*;
import java.util.*;

class HelloWorld
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

    public static void main (String args[])  // entry point from OS
    {
        HelloWorld myWork = new HelloWorld();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin()
    {
        String idata;
        StringTokenizer input;

        while ((idata = HelloWorld.ReadLn (255)) != null)
        {
            input = new StringTokenizer (idata);

            String[] array1 = {};
            int counter = 0;
            while(input.hasMoreTokens());
            {
                array1[counter] = input.nextToken();
                counter++;
            }


            int[] array2 = {};
            for(int a = 0; a < array1.length; a++)
            {
                array2[a] = Integer.parseInt(array1[a]);
            }
            int[] array3 = {};
            for(int b = 0; b < array2.length; b++)
            {
                if ( array2[b] != 42)
                {
                    array3[b] = array2[b];
                }
                else
                {
                    break;
                }
            }
            String string = "";
            for( int c = 0; c < array3.length; c++)
            {
                if( c < array3.length - 1)
                {
                    string += array3[c] + "\n";
                }
                else
                {
                    string += array3[c];
                }
            }
            System.out.println(string);
        }
    }
}

You have a stray semicolon at the end of the while : while结束时,您会有一个流浪的分号:

while(input.hasMoreTokens());
                            ^ REMOVE THIS

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

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