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: logic error in for loops

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default logic error in for loops

    I'm trying to get a program that displays all the numbers from 10 to 800 , 10 per line, that's divisible by 5 and 6 and having the numbers seperated by one space.

    Can someone tell me exactly what my for loops are really doing? When i run each loop alone, one iterates from 10 to 800 downward in a column and the other just goes in a row from 1 to 10.
    however when i run them together the program is terminated. Why is it doing this?

     
    import java.util.Scanner;
    public class a1 {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Scanner input = new Scanner(System.in);
     
    		for (int i = 10; i <= 800; i++) {
    			for (int j = 1; j <= 10; j++) {
    				if (i % 5 == 0 && i % 6 == 0) {
    					System.out.print(i + " ");
    				}
    			}
    			System.out.println();
    		}
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: logic error in for loops

    the program is terminated.
    Are there error messages? Please copy the full text and paste it here.

    Or if there are no errors, please explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: logic error in for loops

    Your problem lies in your if statement. You need an OR instead of an AND. That will get you past your first problem. You then need to rethink how you are counting your 10 per line issue. Hope this helps to get you started.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: logic error in for loops

    Rather than using a nested for loop, which tends to confuse, how about using a counter for the size of the rows.

Similar Threads

  1. Not sure what the Logic Error is? [help]
    By Mitsuwa in forum Object Oriented Programming
    Replies: 2
    Last Post: January 27th, 2013, 11:55 PM
  2. Can you help me find my logic error
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2012, 01:51 AM
  3. Java for loops to count vowels/consonants usinf the logic of the main
    By willie lee in forum Loops & Control Statements
    Replies: 41
    Last Post: June 18th, 2012, 05:01 AM
  4. Lotto Problem logic error
    By ippo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 11:18 AM
  5. Serial Programming Logic Error???
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 27th, 2011, 04:28 AM