Java: why is this code not working? Infinite loop?

让人想犯罪 __ 提交于 2019-12-20 02:53:01

问题


So as you may be able to tell from my attempt, I'm trying to figure out how I'd make a program which gives the user 5 seconds to enter some lines of text, then the Scanner will count how many lines were entered. I've just started learning Java as my 2nd language, so please try to explain everything as simply as possible :)

I've got two theories as to why it's not working. The first is that nextLine() will return the whole line, regardless of whether it's empty or not meaning rather than NL equaling "", it will actually equal the whole line (ie " "). And my second theory is that I've got no idea what I'm doing and the program flow is all over the place. Anyway, here's my code:

class OrigClass{
    public static void main(String args[]){
        Scanner ScanObj = new Scanner(System.in);
        int Count = 0;
        String NL = ScanObj.nextLine();
        try{
            Thread.sleep(5000);}
        catch (InterruptedException e){
            e.printStackTrace();
        }
        while (!NL.equals("")){
            Count++;
            NL = ScanObj.nextLine();
        }
        System.out.print("You Entered " + Count + " Lines.");
        ScanObj.close();
    }
}

Oh, I forgot to mention hasNext() was what I originally tried:

       import java.util.Scanner;

    class OrigClass{
public static void main(String args[]){
    Scanner ScanObj = new Scanner(System.in);
    int Count = 0;
    try{
    Thread.sleep(5000);}
    catch (InterruptedException e){
        e.printStackTrace();
    }
    while (ScanObj.hasNext() == true){
    Count++;
    ScanObj.nextLine();
    }
    System.out.print("You Entered " + Count + " Lines.");
    ScanObj.close();
}
    }

回答1:


From the looks of it, this code should work. My only guess is that you are manually entering the input and are forgetting to signal the end of input with CTRL+D. However, doing this, you'll get a NoSuchElementException if you do not use ScanObj.hasNext().

You could also run your code using input redirection. java OrigClass < data

A better way to do this would be the following:

import java.util.Scanner;

public class Sc {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int count = 0;
        String nl; // = scan.nextLine();
        //while (!NL.equals(""))
        while(scan.hasNext())
        {
          count++;
          nl = scan.nextLine();
        }
        System.out.println("Done!  Count: " + count);
        scan.close();
    }
}

The difference here is that we save the first nextLine() until we're in the while loop. This will give an accurate count of how many lines are in the input.

Just don't forget to signal end of input with CTRL+D.




回答2:


Well this solution is not really good. but works.

public class FiveSecond {
  public static void main(String args[]){
    new Thread(new Count(new Reader())).start();
  }
}
class Count implements Runnable{
  Reader r;Thread t;
  Robot ro;
  public Count(Reader t){this.r=t;
  try {
    ro=new Robot();
  } catch (AWTException e) {e.printStackTrace();}
  }
  @Override
  public void run() {
    t=new Thread(r);
    //t.setDaemon(true); //[S2]
    t.start();
    try{
    Thread.sleep(5000);
    }catch(Exception e){}
    t.interrupt();
    //Implicitly press the enter key in order to release the readLine() method :D
    //not recommended, and it's not a good idea, but works
    ro.keyPress(KeyEvent.VK_ENTER);
    ro.keyRelease(KeyEvent.VK_ENTER);
    /*
     * this is possible to save the strings lines in somewhere in order to access from invoker application
     * or send back the strings by socket, etc . . .
     */
    System.out.println("number of entered lines "+r.getCount()+"\n");
    //you would run this main as a process and get the number of counts 
    //System.exit(r.getCount()); //[S2]
  }
}

class Reader implements Runnable{
  private List<String> lines;
  private volatile int count;
  private BufferedReader br;
  public Reader(){
    lines=new ArrayList<String>();
    br=new BufferedReader(new InputStreamReader(System.in));
  }
  @Override
  public void run() {
    try{String line;
      System.out.println("you have 5 second to detect a 2048 length character, then your system will broken");
      while((line=br.readLine())!=null){
        if(!Thread.currentThread().isInterrupted()){
      count++;lines.add(line);}else{break;}
      }
      //for showing the lines entered
      //System.out.println(lines.toString());
    }catch(Exception ex){}
  }
  public int getCount(){return this.count;}  
}

but the best approach is about running a separated process to count the lines, and you would just remove the [S2] comments to achieve it.



来源:https://stackoverflow.com/questions/17577908/java-why-is-this-code-not-working-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!