Results 1 to 6 of 6
  1. #1
    dalcorn is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Problem with code

    Hey,

    I'm trying to print the chars on the ascii list (33-126) with ten per line. The problem is after I've done so another line is printed. Thus my program is "wrong" based on what my professor has setup for us to turn-in online. How could I delete the last line of this program? I tried using a counter for each line printed but it didn't workout well (there should only be 9 lines).

    Java Code:
    public class aa {
    
        public static void main(String[] args) {
            final int CharsPerLine = 10;
            for (int i = 33; i <= 126; i++) { //! = 33, ~ = 126
                for (int j = 0; j < CharsPerLine; i++, j++) {
                    System.out.print((char) i + " ");
                    
                    if (i == 126) {
                    	break;               	
                    }
                }
                System.out.println();
            }
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Problem with code

    The problem is that the break in the inner loop only applies to the inner loop. So the extra line gets printed. Use break with a label and place the label before the outer loop.

    e.g

    Java Code:
    loop:
    for (....)
    //   ...
       break loop;
    }
    Of course, I might add this can be done with just one loop and a counter using the mod (%) operator to determine when the number of chars is divisible by 10.

    regards,
    Jim
    Last edited by jim829; 09-24-2013 at 07:37 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    dalcorn is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with code

    If I put break; after the new line output then it stops the entire program and only produces one line.
    edit: was unsure what a label was so just tried what you said. My results appear correct but they are not when I send them (no longer produces the extra line)

    Java Code:
    /* @author Dylan Alcorn
     * Problem: Exercise 04.15
     * Class: 1301 9AM MWF Sec 001
     */
    
    public class aa {
    
        public static void main(String[] args) {
            final int CharsPerLine = 10;
    
            loop:
            for (int i = 33; i <= 126; i++) { //! = 33, ~ = 126
                for (int j = 0; j < CharsPerLine; i++, j++) {
                    System.out.print((char) i + " ");
                    
                    if (i == 126) {
                    	break loop;                      	
                    }              
                }
                System.out.println();
            }
        }
    }
    Last edited by dalcorn; 09-24-2013 at 07:40 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Problem with code

    I was talking about the break you already have.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    my128samuel is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Problem with code

    Hello,

    Try to replace your
    Java Code:
    break;
    with
    Java Code:
    return;
    it should not print a line in the execution's end.

    Regards,

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Problem with code

    Yep! Probably even better.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Having a problem with my code
    By jman8910 in forum New To Java
    Replies: 1
    Last Post: 01-15-2013, 09:33 PM
  2. Problem with code
    By koryvandell in forum New To Java
    Replies: 4
    Last Post: 04-25-2011, 04:28 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. problem in my code
    By wannabe in forum New To Java
    Replies: 5
    Last Post: 04-12-2010, 04:38 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
  •