Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that Java while (scanner.hasNext ()) can't jump out?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the problem that Java while (scanner.hasNext ()) cannot jump out of the problem". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to solve the problem that Java while (scanner.hasNext ()) can't jump out of"!

Java while (scanner.hasNext ()) cannot jump out of the situation description:

This problem is encountered in doing PAT Class B 1010, I need to read a limited number on the keyboard, and then calculate in pairs. What I wanted at first was to declare an array, save all the numbers I read, and then calculate them separately, but I found that he would not jump out of the loop I had set, but had been executing it.

When reading the last piece of data, instead of jumping out, it is blocked in the while and executed all the time.

Reason:

HasNext (), which returns true if there is another tag in the input of this scanner. This method may block while waiting for input to scan. The scanner will not perform any input. So the cycle will go on forever.

Solution:

At this point, you can set a Terminator to call the overloaded method hasNext (String patten) of hasNext (): return true if the next input matches the slave Terminator. The scanner does not perform any input.

/ example: take "0" as the Terminator Scanner in = new Scanner (System.in); while (! in.hasNext ("0")) {System.out.println (in.next ());}

But this kind of operation can not meet my requirements for problem solving. I ran the answer written by java online and found that it was the same question, but it could be calculated in while. Output all the results before you get stuck.

Another solution is to read in the form of an input string, then disassemble it and convert it into int data for calculation.

Java's while+hasNext () / * * @ method 1: encounter special symbol ending * / public static void main (String args []) {/ / System.in represents standard input, that is, keyboard input Scanner sc = new Scanner (System.in); / / adding the following line will only use carriage return as the delimiter / / sc.useDelimiter ("\ n"). / / useDelimiter can change the delimiter to "enter", or other characters. System.out.println ("Please input a number"); / / determines whether there is a next entry while (! sc.hasNext ("0")) / / A Terminator can be set in this case, calling the overloaded method hasNext (String patten) of hasNext (): / / returns true if the next tag matches the pattern constructed from the specified string. The scanner does not perform any input. {/ / output input item System.out.println ("keyboard input is:" + sc.next ()); System.out.println ("Please input a number");} System.out.println ("not executed");}

/ * * @ method 2 ends the output of * / public static void main (String [] args) {System.out.print ("Please enter a value:"); Scanner s = new Scanner (System.in); while (s.hasNext ()) {String a = s.next () / / assign s.next () to variable an if ("quit" .equals (a)) {break;} else {System.out.println (a);}

At this point, I believe you have a deeper understanding of "how to solve Java while (scanner.hasNext ()) can not jump out of the problem". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report