while-loop

Thread.sleep() in a while loop

不羁岁月 提交于 2019-12-04 23:56:35
I notice that NetBeans is warning me about using Thread.sleep() in a while loop in my Java code, so I've done some research on the subject. It seems primarily the issue is one of performance, where your while condition may become true while the counter is still sleeping, thus wasting wall-clock time as you wait for the next iteration. This all makes perfect sense. My application has a need to contact a remote system and periodically poll for the state of an operation, waiting until the operation is complete before sending the next request. At the moment the code logically does this: String

Python syntax for an empty while loop

笑着哭i 提交于 2019-12-04 22:16:29
I have written this: while file.readline().startswith("#"): continue But I suspect the continue is unnecessary? What is the correct syntax for what i'm trying to achieve? while file.readline().startswith("#"): pass This uses the pass statement : The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. http://www.network-theory.co.uk/docs/pytut/passStatements.html 来源: https://stackoverflow.com/questions/14797046/python-syntax-for-an-empty-while-loop

Validating input with while loop and scanner

强颜欢笑 提交于 2019-12-04 21:59:21
What's the best way about getting a valid integer from a user that is in a specified range (0,20) and is an int . If they enter an invalid integer print out error. I am thinking something like: int choice = -1; while(!scanner.hasNextInt() || choice < 0 || choice > 20) { System.out.println("Error"); scanner.next(); //clear the buffer } choice = scanner.nextInt(); Is this correct or is there a better way? Where do you change choice inside of your while loop? If it's not changed you can't expect to use it in your if block's boolean condition. You'll have to check that the Scanner doesn't have an

iterate a html table of two row and 3 column using one php query

只愿长相守 提交于 2019-12-04 21:38:49
I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table. I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row. this is what i have tried but didn't work <div id="maincontent"> <!-- class one --> <?php $getSection = getSection(); $i=0; while($allSection = mysql_fetch_array($getSection)){ ?> <div class="subconent"> <table width="937" border="0"> <tr> <td> <div class="sub_image"> <a href="section.php?id=<?php echo

Comparing char in a while loop

浪子不回头ぞ 提交于 2019-12-04 19:47:31
As long as the input is not x, the loop will continue to ask for the input and it prints out either A or B. int main (void){ char input; while( input != 'x'){ printf("Enter Input:"); scanf("%c", &input); if (input == 'a'){ printf("A \n"); } else{ printf("B\n"); } } return (0); } The problem is that everytime after i entered the input, it prints the output and it also prints out "Enter Input:B" in a new line no matter i entered a or b or anything else as input. Can anyone tell me how can i solve this problem, Thanks! Here is what happened: Enter Input:a A Enter Input:B / after the output it

Looping through MySQL left join in php vs. 2 separate queries

萝らか妹 提交于 2019-12-04 19:29:29
I have a simple question and answer section on my website that allows comments. I currently populate the answers using a loop and a $_GET['id'] value. Here is my query <?php try{ $parent=$_GET['id']; $post_type = "2"; $stmt = $dbh->prepare( "SELECT p.post_id, p.content, p.author, p.created, pc.comment_id AS comment_id, pc.content AS comment_content FROM posts AS p LEFT JOIN posts_comments AS pc ON p.post_id = pc.parent_id WHERE p.post_type = :post_type AND p.parent = :parent "); $stmt->bindParam(':parent', $parent, PDO::PARAM_INT); $stmt->bindParam(':post_type', $post_type, PDO::PARAM_INT);

How to count occurrence of each character in java string using Pattern Class(Regex)

末鹿安然 提交于 2019-12-04 19:21:22
I am trying to find a number of Occurrence of each character on the given string. Expected output: t=2 e=1 s=1 i=1 n=1 g=1 Current output: T=0 e=0 s=0 t=0 i=0 n=0 g=0 Code: String str = "Testing"; int count = 0; Pattern p = Pattern.compile("[a-zA-Z]", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str); while (m.find()) { if (m.group().equals(str)) { count++; } System.out.println(m.group() + "=" + count); } There are many ways of doing this but I am looking for Regex only, so how can we achieve that by using Regex. Any Help would be Appreciated. Thanks in advance. No need for a regex to

php echo table with while loop

与世无争的帅哥 提交于 2019-12-04 19:04:33
I am creating a table and want it laid out a certain way and have to retrieve the data from the DB. I am trying to get it set up where it has the usernames across the top example... Jim Chris Allen Rick 7 8 4 5 my code looks like this and I have been messing around with it for hours and cant figure out why I cant get it set up how I want. Any help would be appreciated. Its a while loop. while ($pickresults= mysql_fetch_assoc($picksquery)) { //first row echo '<th> '.$pickresults['username'].' </th> '; echo ' <td> '.$pickresults['firstgame'].' </td> '; } First off, you should learn the HTML code

A try-catch method in while loop?

孤街浪徒 提交于 2019-12-04 18:46:17
I have this code, and I want to put the try-catch inside a while loop. The logic would be, "while there is an input error, the program would keep on asking for a correct input". How will I do that? Thanks in advance. public class Random1 { public static void main(String[] args) { int g; Scanner input = new Scanner(System.in); Random r = new Random(); int a = r.nextInt(10) + 1; try { System.out.print("Enter your guess: "); g = input.nextInt(); if (g == a) { System.out.println("**************"); System.out.println("* YOU WON! *"); System.out.println("**************"); System.out.println("Thank

C++, Real-Time User Input, during While Loop

∥☆過路亽.° 提交于 2019-12-04 18:41:24
Is there was any way for the User to give a Real-Time input, while something is constantly being updated in the background. Basically, making the program not stop, when asking for user input. For example, It will ask for user input, while a number is constantly being calculated. There are two ways of this problem, as I see it. One is, as xebo commented, using multi threading. Use one thread for the constant calculation of the number or whatever, and another thread to look for user input constantly. The second method is a simpler on and works only if you are using cin( from the std namespace)