Results 1 to 2 of 2
  1. #1
    fred is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default help with basic example

    I have this code:
    Java Code:
    for(int i=0; i<100; i++)
    {
    str = br.readLine();
    System.out.println("String"+ i + str);
    if(str.equals("stop"))
    break;
    }
    If I remove the System.out.println, it is going for an infinite loop.
    why?

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    fred, i suggest if ur going to ask bout some code later, try to put the full code in the post

    Ur post, since it's only half the code, lost some of the important information involving the program, so it's hard to analyze what's wrong with it actually

    The missing points
    - we don't know what br are
    just using assumptions, from the name i presume it may be a BufferedReader instance
    - we also don't know what str are or what it may consists
    another assumptions, it should be a String right?

    So, based on a rough assumption, in which im not as good of a Sherlock Holmes in assuming things, so if im wrong please correct me :D
    The code should be like this or some sort
    Java Code:
    import java.io.*;
    
    public class Fred
    {
    	public static void main(String[] args)throws IOException
    	{
    		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    		String str;
    		for(int i=0; i<100; i++)
    		{
    			str = br.readLine();
    			System.out.println("String"+ i + str);
    			if(str.equals("stop"))
    				break;
    		}
    	}
    }
    If this is the code, which i doubt it is, i already tried deleting the System.out.print inside the for loop, it work just okay, no infinite loop

    Besides, an infinite loop in a for loop with that kinda parameter is really rare, might be possible if u insert a decrement operation for i (i--) inside the brackets of the loop, which u didn't
    But usually, infinite loop doesnt occur in for loop, while loop maybe, but not for loop

    This is the best i can do to help, i suggest u post the full source code

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 07:17 PM
  2. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  3. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 01:36 PM
  4. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 04:35 PM
  5. Basic Graphic
    By jkswebsite in forum Java 2D
    Replies: 6
    Last Post: 11-26-2007, 03:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •