Stop reading input in java without EOF

╄→гoц情女王★ 提交于 2019-12-06 13:17:18

Like this,

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String src;

while ((src = br.readLine()) != null) {
    StringTokenizer st=new StringTokenizer(src);
    if(!st.hasMoreTokens()) break;
    //YOUR LOGIC GOES HERE 
    }
}

Look for a special string for instance END. When trgt equals to "END" then you can break the loop.

As said @ihsan kocak you can look for a string like EXIT or QUIT.When you find the string just break the loop.

Set a specific word as a reading breaker. For example stop. If the user gives the input stop then the reading process will end. Here is an implementation example of this process:

    public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String src;

    while ((src = br.readLine()) != null && !(src.equals("stop"))) {
        String trgt = src;
       //do something
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!