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: stuck on a "for" loop

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default stuck on a "for" loop

    I want the code to print 3 lines of '@' with the same length.
    In the second line it will have text inside. I have a problem when [text%2] != 0.
    Tried to fix it with a for loop, but it will print 1 less or more '@' when [text % 2] = 1.
    If something isn't clear please ask me to explain, thanks a lot to helpers.

    package sos;
    public class triangle {
    	public static void main(String[] args) {
     
     
    	int x,y,z = 5; //z*2 = first and second lines / total.length
    	char w = '@';
    for (int j=0; j<z*2; j++) { //first line
    System.out.print(w);
    	}
    System.out.println();
    	String txt = " 312 ";
    	x = txt.length();
    	y = x/2;
    	z-=y;
    for (int i = 0; i < z; i++) { // second line (first half)
    System.out.print(w);
    }
    		System.out.print(txt.toUpperCase()); //middle TEXT
    			if (z%2==0) { //second line (second half)
    for (int i = 1; i < z; i++) {
    System.out.print(w);
    }
    		}
    			else {
    for (int i = 0; i < z; i++) {
    System.out.print(w);
    }	
    		}
    System.out.println();
    		z+=y;
    for (int j=0; j<z*2; j++) { //last line
    System.out.print(w);
    }
    System.out.println();
     
    System.out.println(txt.length());
    int d = txt.length();
    d%=2;
    	System.out.println(d);
     
    	}
    }
    Last edited by TenisAfterNine; September 20th, 2019 at 04:14 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: stuck on a "for" loop

    Can you post the program's current output that shows what you are talking about?
    Also post what you want the output to look like.

    Can you fix the formatting of the source code you posted so it has proper indentations?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stuck on a "for" loop

    That is given back right now:
    @@@@@@@@@@
    @@@ 312 @@@
    @@@@@@@@@@

    Ssecond line (now) = @*3, space, '312', space, @*3. A total of 11 chars.

    That what i want to achive:
    @@@@@@@@@@
    @@@ 312 @@
    @@@@@@@@@@

    *Because the lines has to be exact in the same length by letters (including the spaces).

    Didn't quite understand your last request, Im just beginning.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: stuck on a "for" loop

    Is the problem that the second line has 3 @ after the 312 and you only want to have 2?


    Source code should have proper indentations on each line to make it easier to read and understand.
    For example only the first and last lines of the source code should start in the first column.
    The amount of indentation should show the nesting of the logic.
    Look at the posted code in other threads or anywhere. Most posted code has proper indentations.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  5. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM