Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: help me terminate the infinite loop.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me terminate the infinite loop.

    import java.util.*;
    import java.io.*;
     
    class close {
    	public static void main(String[] args) throws IOException {
    		int p, q;
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("enter values");
    		String s1 = br.readLine();
    		p = Integer.parseInt(s1);
    		String s2 = br.readLine();
    		q = Integer.parseInt(s2);
    		close c1 = new close();
    		c1.find(p, q);
    	}
     
    	void find(int p, int q) {
    		int s[] = new int[18];
    		int lol, j, count = 0, f = 0, a;
    		boolean b;
    		for (lol = p; lol <= q; lol++) {
    			j = 0;
    			System.out.print(j);
    			while (lol > 0) {
     
    				s[j] = lol % 10;
    				j++;
    				lol = lol / 10;
    				System.out.println(lol);
     
    			}
    		}
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: help me terminate the infinite loop.

    In future, please use highlight tags around your code (see my sig), and proper formatting is encouraged.
    I have done it for you this time.

    This infinite loop is caused by your while loop.

    You say, "while lol is greater than 0"..
    If you set the q variable as anything other than 0, it will loop indefinitely.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: help me terminate the infinite loop.

    lol will become 0 when your code exits from while loop, so it will never greater than q. Then your for loop will not reach the end.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me terminate the infinite loop.

    please first tell what type of output you need..

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  2. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 06:45 AM
  3. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  4. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM
  5. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 12:26 AM