简体   繁体   中英

How can I exit a Scanner Class to perform the rest of the loop?

I am accessing a Scanner that helps retrieve RFID Tags from an RFID Reader. I tried using a loop around my Scanner.next(); but that wouldn't do anything. I think that once my Scanner accesses the RFID Reader's method, that it just permanently stays in there. Is there any way to exit the method for the RFID and then perform the rest of my loop?

//The beginning of the loop
while (!doneYet) { 
        //Just a set boolean variable that I try to use later
        Inter = false;
      //A question class that I created, obtains and displays a random question
        Test = Work.randomQuestion();
        Test.display();

      //This is where my problems seem to start, this is the RFID Readers Method.
        rfid.addTagGainListener(new TagGainListener() {
            public void tagGained(TagGainEvent oe) {
                Temp = oe.getValue();
                if (Test.getRFIDTag().equals(Temp)) {
                    System.out.println("You are Correct");
                    count++;
                    System.out.println(count);
                    Inter = true;
                    System.out.println("out");
                    /*
                     * try { System.in.close(); } catch (IOException e) {
                     * TODO Auto-generated catch block e.printStackTrace();
                     * }
                     */
                } else if (!Test.getRFIDTag().equals(Temp)) {
                    System.out.println("Sorry, you are wrong");
                }
                return;
            }
        });

        // Before the RFID Opens though, this code is initiated
        rfid.openAny();
        rfid.waitForAttachment(1000);
        sc = new Scanner(System.in);

         //This is where I attempted to control the RFID Reader, but this doesn't work
        if(!Inter){
            sc.next();
        }

         //How I attempt to end the initial while loop
        if(count>10){
            doneYet = true;
        }
    }

Thanks in advance for all of the help.

Your scanner doesn't actually do anything. sc.next() just skips initial whitespace, and returns the first string of non-whitespace characters; but it just discards that string, never (for example) passing it to rfid . As far as I can see, nothing ever triggers rfid 's TagGainListener . (And why do you add a new TagGainListener for each pass through the loop, anyway? Surely you only need one listener?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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