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 5 of 5

Thread: Loop Breaking Not Working

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Loop Breaking Not Working

    Ok, I'm ripping my hair out over this.

    I have a huge 50,000 line tsv file that I am reading through (cannot provide file for copyright reasons). I am looking for only the relevant data for the restrictions provided.

    In this snippet of code:
    boolean start = false;
    			while(line!=null)
    			{
    				StringTokenizer tokenizer = new StringTokenizer(line);
    				int year = Integer.parseInt(tokenizer.nextToken());
    				int month = Integer.parseInt(tokenizer.nextToken());
    				//System.out.println("sy: "+sy+" sm: "+sm+"\n     Year: "+year+" Month: "+month);
    				if(!start && sy.equals(year+"") && sm.equals(month+""))
    				{
    					start = true;
    					System.out.println("Found Start YEAR: "+year+" MONTH: "+month);
    					System.out.println(months[month-1]);
    				}
    				if(month==3)
    				{
    					System.out.println("March Found: "+months[month-1]);
    				}
    				if(months[month-1].equalsIgnoreCase(cutMonth))
    					break;
    				if(start)
    				{
    I am reading through the lines. The first two tokens are the Year and then the Month for the proceding tokens data.

    Now, for this example, I have varified that cutMonth is equal to "Mar". And I have varified that it runs into times when it finds Mar for the line Month.

    So, when the Month is March, month will equal 3. When month equals 3, months[month-1] equals "Mar".

    So, how is it possible for something to enter this if statement:
    if(month==3)
    				{
    					System.out.println("March Found: "+months[month-1]);
    				}
    But not this if statement:
    if(months[month-1].equalsIgnoreCase(cutMonth))
    					break;

    If it enters the second if statement, it should break out of the while loop, correct?

    Tell me if I need to explain something in more detail. Any help is appreciated.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Loop Breaking Not Working

    In the code snippet you posted, cutMonth isn't assigned any value. To get better help sooner, post a SSCCE : Java Glossary that members here can copy, compile and debug.

    db

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,068
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Loop Breaking Not Working

    Try debugging your code by adding print outs to see what you are comparing:

    System.out.println("months[month-1]= " + months[month-1] + ", cutMonth=" + cutMonth + "<");

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Loop Breaking Not Working

    Quote Originally Posted by Darryl.Burke View Post
    In the code snippet you posted, cutMonth isn't assigned any value. To get better help sooner, post a SSCCE : Java Glossary that members here can copy, compile and debug.

    db
    cutMonth is assigned a 3 letter representation of a month that is 3 months ahead of the indicated start month. I know for a fact that cutMonth is equal to "Mar" when I run this data and scenario through, but if you are interested in seeing the actual code for cutMonth:

    public ArrayList<MarketDetails> findChangeData(ArrayList<MarketDetails> details,String sy,String sm)
        {
    ...
     
    			String lastDate = getNextDate(getNextDate(getNextDate(sm+"-"+sy)));
    			String cutYear = lastDate.substring(6);
    			String cutMonth = lastDate.substring(0,3);
    System.out.println("Cut Month: "+cutMonth+" Cut Year: "+cutYear);
    			for(int i=0;i<months.length;i++)
        		{
        			if(months[i].equals(cutMonth))
        			{
        				cutMonth = (i+1)+"";
        				break;
        			}
        		}
    ...
    }
    public String getNextDate(String curr)
        {
        	int monthN = -1;
        	int yearN = Integer.parseInt(curr.substring(4));
        	for(int i=0;i<months.length;i++)
        	{
        		if(curr.substring(0,3).equals(months[i]))
        		{
        			monthN = i;
        			break;
        		}
        	}
        	if(monthN == 11)
        	{
        		monthN=0;
        		yearN++;
        	}
        	else
        	{
        		monthN++;
        	}
        	return months[monthN]+"-"+yearN;
        }

    As you can see, a good amount of code to just determine a variable, so I didnt think it was necessary to include it.


    Try debugging your code by adding print outs to see what you are comparing:

    System.out.println("months[month-1]= " + months[month-1] + ", cutMonth=" + cutMonth + "<");
    I've attempted to do this, but it is very difficult for me to pinpoint exactly where and when it should correctly leave the loop. This is because of several reasons:
    1) it goes through 30,000 or so lines beforehand, which means it would have to print all of those out, which usually causes my IDE to crash after a few minutes from excessively printing to the console
    2) this part is the in the middle of a 2,000 line program that prints out thousands of lines to the console for me to track its process while debugging.

    However, after adding a print statement here:
    if(month==3)
    				{
    					System.out.println("March Found: "+months[month-1]);
    					System.out.println(cutMonth);
    					break;
    				}
    				if(months[month-1].equalsIgnoreCase(cutMonth))
    					break;

    I have determined that cutMonth is "Mar" and that months[month-1] is "Mar" but it still fails to enter my if statement that breaks the while loop. The break in the first if statement (with the month==3 condition) works right now, but when the program is run next week (or perhaps the week after, not sure which one), that line will have to be modified to run correctly (which is why I need the second if statement fixed).

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,068
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Loop Breaking Not Working

    I have determined that cutMonth is "Mar" and that months[month-1] is "Mar"
    What did the print out show for the values of those variables?
    I find it hard to believe that the if you show would not be true if those were the values.
    The conclusion is either the if was not executed or the values are not what you think they are.
    A print out just before the if would show which one it was.

Similar Threads

  1. Breaking If/Else Statements
    By aussiemcgr in forum Loops & Control Statements
    Replies: 9
    Last Post: August 27th, 2010, 03:28 PM
  2. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 09:05 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  4. Breaking out of multiple loops
    By helloworld922 in forum Loops & Control Statements
    Replies: 4
    Last Post: July 1st, 2009, 02:52 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM