Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39

Thread: Nested Loops

  1. #21
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    Sorry wasn't aware of how to do that, but I saw the link on your signature at the bottom
    I will remember that for future reference

  2. #22
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all you should collect number of rows and columns from the user. Without doing that how did you find number of rows and columns ?

    Java Code:
    Scanner scan = new Scanner(System.in);
    
            System.out.println("Enter rows: ");
            int row = scan.nextInt();
    
            System.out.println("Enter columns: ");
            int columns = scan.nextInt();

  3. #23
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    Hmm.. the question on my assignment requires only one number from the user which is the number of stars.

    So if the number is 7, then there would be 7 stars going diagonally in one direction and then 7 stars the other direction.

    I am still working on it and I am trying to figure it out, I just have a quick question with this assignmetn and another assignment, I'm recieving this error
    Java Code:
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at Cross1.main(Cross1.java:10)
    any idea what that means, or how i can solve this problem?

  4. #24
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just looking at your assignment I thought he first thing that you've to sort out is square one.

  5. #25
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    Nope, the only required thing to do is to make the X out of the stars.

    Those other things were hints as to how do to them.

  6. #26
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, then it involve some logics. Anyway, try to write the square pattern first of all. Show it here, then you can edit the code appropriately.

  7. #27
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Hi ,

    just try to analyze this ..

    Java Code:
     for (row = 0; row < number; row++) {
                for (col = 0; col < number; col++) {
                       System.out.print("*");
                }
                System.out.println();
            }


    Now you can build your logic around it :)
    Well jus think wat this if (col == row || col == number-row-1) will do also :p

    I think these are more than enough tips to design your solution any doubt feel free to contact :)

    warm regards
    Vinod M
    ____________________________
    give me beans .........
    Last edited by Vinod Mukundan; 11-26-2010 at 11:35 AM. Reason: Gave direct solution first edited it to tips :)

  8. #28
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    Hi ,

    just try to analyze this ..

    Java Code:
     for (row = 0; row < number; row++) {
                for (col = 0; col < number; col++) {
                       System.out.print("*");
                }
                System.out.println();
            }


    Now you can build your logic around it :)
    Well jus think wat this if (col == row || col == number-row-1) will do also :p

    I think these are more than enough tips to design your solution any doubt feel free to contact :)

    warm regards
    Vinod M
    ____________________________
    give me beans .........


    It looks so easy now... Yesterday I spent a couple of hours on this with no viable solution.
    Last edited by luke; 11-26-2010 at 09:30 PM.

  9. #29
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    I agree, it looks really easy now. Thank you!
    I'm just having troubles with the other cross. Haha.

    Java Code:
    for (row = 0; row < number; row++) 
    				{
    					for (column = number - row - 1; column < number; column++)
    					{
    						for	(column = row - number; column < number; column++)
    						{
    							System.out.print(" ");
    						}
    							System.out.println("*");
    				}
    				}
    	}
    }
    When I run it, I get this
    Java Code:
    Enter a number: 
    5
              *
             *
            *
           *
          *
    anyone know what i''m doing wrong? am i missing something, I need the other cross in the other direction to complete the X

  10. #30
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by candygirl198827 View Post
    I agree, it looks really easy now. Thank you!
    I'm just having troubles with the other cross. Haha.

    Java Code:
    for (row = 0; row < number; row++) 
    				{
    					for (column = number - row - 1; column < number; column++)
    					{
    						for	(column = row - number; column < number; column++)
    						{
    							System.out.print(" ");
    						}
    							System.out.println("*");
    				}
    				}
    	}
    }
    When I run it, I get this
    Java Code:
    Enter a number: 
    5
              *
             *
            *
           *
          *
    anyone know what i''m doing wrong? am i missing something, I need the other cross in the other direction to complete the X

    Java Code:
    for(int row = 0; row < number; row++){
    			for(int col = 0; col < number; col++){
    				if(col == row || col == number - row - 1)
                                    ...
    You should be able to continue it.

    P.S. Don't forget to click on the Rep button of Vinod Mukundan.
    Last edited by luke; 11-26-2010 at 09:54 PM.

  11. #31
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
    for (row = 0; row < number; row++) 
    				{
    					for (column = 0; column < number; column++)
    						if(column == row || column == number - row - 1)
    						{
    							for	(column = row - number; column < number; column++)		
    						
    								{
    							System.out.print(" ");
    						}
    							System.out.println("*");	
    						}
    			
    					{
    						System.out.print(" ");
    					}
    						System.out.println("*");	
    						}
    					}
    				}
    this is what i get
    Java Code:
    Enter a number: 
    
    5
              *
     *
             *
     *
            *
     *
           *
     *
          *
     *
    i am soooo close!!!
    can't figure out where i am going wrong.

  12. #32
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by candygirl198827 View Post
    Java Code:
    for (row = 0; row < number; row++) 
    				{
    					for (column = 0; column < number; column++)
    						if(column == row || column == number - row - 1)
    						{
    							for	(column = row - number; column < number; column++)		
    						
    								{
    							System.out.print(" ");
    						}
    							System.out.println("*");	
    						}
    			
    					{
    						System.out.print(" ");
    					}
    						System.out.println("*");	
    						}
    					}
    				}
    this is what i get
    Java Code:
    Enter a number: 
    
    5
              *
     *
             *
     *
            *
     *
           *
     *
          *
     *
    i am soooo close!!!
    can't figure out where i am going wrong.

    Why are you using the most inside loop?

  13. #33
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    i don't know.

    the if statement isn't making a difference in my code.
    with or without it, i still have one line going diagonal.
    Java Code:
    for (row = 0; row < number; row++)	
    				{
    					for (column = 0; column < number; column++)
    						if(column == row || column == number - row - 1)
    						{
    						for	(column = row - number; column < number; column++)		
    							{
    							System.out.print(" ");
    							}
    							System.out.println("*");	// straight line
    							}
    							System.out.println("*");	// cross 
    				}
    				
    					}
    	}
    results with
    Java Code:
    Enter a number: 
    5
              *
    *
             *
    *
            *
    *
           *
    *
          *
    *
    i've been moving codes around, trying to figure out if my brackets are not placed in the right spots.. but i'm still having trouble

  14. #34
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Try the following and see what happen when you have odd/even number of rows/columns.

    Java Code:
    public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Enter rows: ");
            int row = scan.nextInt();
    
            System.out.println("Enter columns: ");
            int columns = scan.nextInt();
    
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < columns; j++) {
                    if (i == j) {
                        System.out.print("* ");
                    }
                    else if(i == ((columns - 1) - j)) {
                        System.out.print("* ");
                    }
                    else {
                        System.out.print("  ");
                    }
                }
                System.out.println("");
            }
        }

  15. #35
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  16. #36
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Java Code:
    public class Test{
    
        public static void main(String[] args) {
    
            int number = 0;
            int row = 0;
            int col = 0;
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Please Enter the Value:-");
                number = Integer.parseInt(br.readLine());
    
                for (row = 0; row < number; row++) {
                    for (col = 0; col < number; col++) {
                        if (col == row || col == number - row - 1) {
                            System.out.print("*");
                        } else {
                            System.out.print(" ");
                        }
                    }
                    System.out.println();
                }
            } catch (NumberFormatException e) {
                System.out.println("You have entered a Invalid Value");
            } catch (Exception er) {
                er.printStackTrace();
            }
        }
    }

    Try this one out .... I guess only odd numbers will give you a proper cross in any case.... unless u adjust the value.

    Try understanding the logic as suggested by Eranga.

    Nice to know it helped you Luke ..:)

    warm regards
    Vinod M
    Last edited by Vinod Mukundan; 11-29-2010 at 06:16 AM.
    _______________________________________________
    give me beans .........

  17. #37
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    I totally understand now!
    My if-else statements were not working together, plus I had some extra code that was unnecessary and confusing me.
    Thank you so much for all of your help and time. I honestly would be pulling my hair out right now trying to figure this out.
    Honestly, thank you to all of you.

  18. #38
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Great !!!!! So I consider the thread as solved.Thnx to all
    _______________________________________________
    give me beans .........

  19. #39
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by candygirl198827 View Post
    I totally understand now!
    My if-else statements were not working together, plus I had some extra code that was unnecessary and confusing me.
    Thank you so much for all of your help and time. I honestly would be pulling my hair out right now trying to figure this out.
    Honestly, thank you to all of you.
    Whenever you some with an issue, don't try to code it at once. Take a peace of paper and a pencil, and write-down all the logic comes to your mind. Then compare them with each other and you can see a better solution at a certain time, that's the indication for start coding. Good luck :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 03:49 AM
  2. Nested Loops for Java
    By soccer_kid_6 in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 06:27 AM
  3. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 09:47 AM
  4. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 03:48 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 05:51 PM

Tags for this Thread

Posting Permissions

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