Java pattern using for loop, stuck in the last part











up vote
-2
down vote

favorite












Here is my code, I am trying to get a specific pattern but I ended up with an unexpected pattern.



for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i * 2; j++)
{
if (i != 3)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}

for(int k = 13; k > i * 2; k--)
{
System.out.print("*");
}

System.out.println();
}


I am getting this:



*************
***********
*********
*************
*****


When I should be getting this:



*************
***********
*********
***********
*************


Anyone can help me with this one? Thanks!










share|improve this question




















  • 1




    Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
    – Krease
    Nov 20 at 18:07















up vote
-2
down vote

favorite












Here is my code, I am trying to get a specific pattern but I ended up with an unexpected pattern.



for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i * 2; j++)
{
if (i != 3)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}

for(int k = 13; k > i * 2; k--)
{
System.out.print("*");
}

System.out.println();
}


I am getting this:



*************
***********
*********
*************
*****


When I should be getting this:



*************
***********
*********
***********
*************


Anyone can help me with this one? Thanks!










share|improve this question




















  • 1




    Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
    – Krease
    Nov 20 at 18:07













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Here is my code, I am trying to get a specific pattern but I ended up with an unexpected pattern.



for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i * 2; j++)
{
if (i != 3)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}

for(int k = 13; k > i * 2; k--)
{
System.out.print("*");
}

System.out.println();
}


I am getting this:



*************
***********
*********
*************
*****


When I should be getting this:



*************
***********
*********
***********
*************


Anyone can help me with this one? Thanks!










share|improve this question















Here is my code, I am trying to get a specific pattern but I ended up with an unexpected pattern.



for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i * 2; j++)
{
if (i != 3)
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}

for(int k = 13; k > i * 2; k--)
{
System.out.print("*");
}

System.out.println();
}


I am getting this:



*************
***********
*********
*************
*****


When I should be getting this:



*************
***********
*********
***********
*************


Anyone can help me with this one? Thanks!







java loops for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 at 16:57









jaco0646

4,64052446




4,64052446










asked Nov 20 at 18:04









Hugelevin

31




31








  • 1




    Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
    – Krease
    Nov 20 at 18:07














  • 1




    Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
    – Krease
    Nov 20 at 18:07








1




1




Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
– Krease
Nov 20 at 18:07




Have you tried debugging through it to see if it's going through the loops as you expect it to? Once you do that it should be clear to you what should change
– Krease
Nov 20 at 18:07












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










The line that you print is a sequence of space '' and asterisks '*' characters. Its length is always 13. So the sum of of spaces and stars must be 13.



Whenever a line is printed 2 spaces are added to the line and 2 stars are removed, until you reach a point where the reverse operation is happened (the point is 9 stars).



This code prints that pattern.



public class Pattern{

private static final int MAX_LINE_LENGTH = 13;
private static final int MIN_LINE_LENGTH = 9;
private static final int DIFFERENCE = 2;
private static final int LINES = 5;

private static final String SPACE = " ";
private static final String STAR = "*";

public static void main(String args) {
printPattern();
}

private static void printPattern(){
int spaces = 0;
int stars = MAX_LINE_LENGTH;
boolean reverse = false;

for (int i=0; i<LINES; i++) {
printLine(spaces,stars);

if (stars == MIN_LINE_LENGTH) {
reverse = true;
}

if (reverse == false) {
spaces+=DIFFERENCE;
stars-=DIFFERENCE;
}else{
spaces-=DIFFERENCE;
stars+=DIFFERENCE;
}
}
}

private static void printLine(int spaces, int stars){
StringBuilder builder = new StringBuilder();
for (int i=0; i<spaces+stars; i++) {
if (i<spaces) {
builder.append(SPACE);
}else{
builder.append(STAR);
}
}
System.out.println(builder.toString());
}
}





share|improve this answer



















  • 1




    Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
    – Hugelevin
    Nov 21 at 19:17










  • No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
    – Skemelio
    Nov 21 at 19:21










  • By the way if you have any questions about the above code snippet just ask here in the comment section.
    – Skemelio
    Nov 21 at 19:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398939%2fjava-pattern-using-for-loop-stuck-in-the-last-part%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










The line that you print is a sequence of space '' and asterisks '*' characters. Its length is always 13. So the sum of of spaces and stars must be 13.



Whenever a line is printed 2 spaces are added to the line and 2 stars are removed, until you reach a point where the reverse operation is happened (the point is 9 stars).



This code prints that pattern.



public class Pattern{

private static final int MAX_LINE_LENGTH = 13;
private static final int MIN_LINE_LENGTH = 9;
private static final int DIFFERENCE = 2;
private static final int LINES = 5;

private static final String SPACE = " ";
private static final String STAR = "*";

public static void main(String args) {
printPattern();
}

private static void printPattern(){
int spaces = 0;
int stars = MAX_LINE_LENGTH;
boolean reverse = false;

for (int i=0; i<LINES; i++) {
printLine(spaces,stars);

if (stars == MIN_LINE_LENGTH) {
reverse = true;
}

if (reverse == false) {
spaces+=DIFFERENCE;
stars-=DIFFERENCE;
}else{
spaces-=DIFFERENCE;
stars+=DIFFERENCE;
}
}
}

private static void printLine(int spaces, int stars){
StringBuilder builder = new StringBuilder();
for (int i=0; i<spaces+stars; i++) {
if (i<spaces) {
builder.append(SPACE);
}else{
builder.append(STAR);
}
}
System.out.println(builder.toString());
}
}





share|improve this answer



















  • 1




    Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
    – Hugelevin
    Nov 21 at 19:17










  • No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
    – Skemelio
    Nov 21 at 19:21










  • By the way if you have any questions about the above code snippet just ask here in the comment section.
    – Skemelio
    Nov 21 at 19:22















up vote
3
down vote



accepted










The line that you print is a sequence of space '' and asterisks '*' characters. Its length is always 13. So the sum of of spaces and stars must be 13.



Whenever a line is printed 2 spaces are added to the line and 2 stars are removed, until you reach a point where the reverse operation is happened (the point is 9 stars).



This code prints that pattern.



public class Pattern{

private static final int MAX_LINE_LENGTH = 13;
private static final int MIN_LINE_LENGTH = 9;
private static final int DIFFERENCE = 2;
private static final int LINES = 5;

private static final String SPACE = " ";
private static final String STAR = "*";

public static void main(String args) {
printPattern();
}

private static void printPattern(){
int spaces = 0;
int stars = MAX_LINE_LENGTH;
boolean reverse = false;

for (int i=0; i<LINES; i++) {
printLine(spaces,stars);

if (stars == MIN_LINE_LENGTH) {
reverse = true;
}

if (reverse == false) {
spaces+=DIFFERENCE;
stars-=DIFFERENCE;
}else{
spaces-=DIFFERENCE;
stars+=DIFFERENCE;
}
}
}

private static void printLine(int spaces, int stars){
StringBuilder builder = new StringBuilder();
for (int i=0; i<spaces+stars; i++) {
if (i<spaces) {
builder.append(SPACE);
}else{
builder.append(STAR);
}
}
System.out.println(builder.toString());
}
}





share|improve this answer



















  • 1




    Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
    – Hugelevin
    Nov 21 at 19:17










  • No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
    – Skemelio
    Nov 21 at 19:21










  • By the way if you have any questions about the above code snippet just ask here in the comment section.
    – Skemelio
    Nov 21 at 19:22













up vote
3
down vote



accepted







up vote
3
down vote



accepted






The line that you print is a sequence of space '' and asterisks '*' characters. Its length is always 13. So the sum of of spaces and stars must be 13.



Whenever a line is printed 2 spaces are added to the line and 2 stars are removed, until you reach a point where the reverse operation is happened (the point is 9 stars).



This code prints that pattern.



public class Pattern{

private static final int MAX_LINE_LENGTH = 13;
private static final int MIN_LINE_LENGTH = 9;
private static final int DIFFERENCE = 2;
private static final int LINES = 5;

private static final String SPACE = " ";
private static final String STAR = "*";

public static void main(String args) {
printPattern();
}

private static void printPattern(){
int spaces = 0;
int stars = MAX_LINE_LENGTH;
boolean reverse = false;

for (int i=0; i<LINES; i++) {
printLine(spaces,stars);

if (stars == MIN_LINE_LENGTH) {
reverse = true;
}

if (reverse == false) {
spaces+=DIFFERENCE;
stars-=DIFFERENCE;
}else{
spaces-=DIFFERENCE;
stars+=DIFFERENCE;
}
}
}

private static void printLine(int spaces, int stars){
StringBuilder builder = new StringBuilder();
for (int i=0; i<spaces+stars; i++) {
if (i<spaces) {
builder.append(SPACE);
}else{
builder.append(STAR);
}
}
System.out.println(builder.toString());
}
}





share|improve this answer














The line that you print is a sequence of space '' and asterisks '*' characters. Its length is always 13. So the sum of of spaces and stars must be 13.



Whenever a line is printed 2 spaces are added to the line and 2 stars are removed, until you reach a point where the reverse operation is happened (the point is 9 stars).



This code prints that pattern.



public class Pattern{

private static final int MAX_LINE_LENGTH = 13;
private static final int MIN_LINE_LENGTH = 9;
private static final int DIFFERENCE = 2;
private static final int LINES = 5;

private static final String SPACE = " ";
private static final String STAR = "*";

public static void main(String args) {
printPattern();
}

private static void printPattern(){
int spaces = 0;
int stars = MAX_LINE_LENGTH;
boolean reverse = false;

for (int i=0; i<LINES; i++) {
printLine(spaces,stars);

if (stars == MIN_LINE_LENGTH) {
reverse = true;
}

if (reverse == false) {
spaces+=DIFFERENCE;
stars-=DIFFERENCE;
}else{
spaces-=DIFFERENCE;
stars+=DIFFERENCE;
}
}
}

private static void printLine(int spaces, int stars){
StringBuilder builder = new StringBuilder();
for (int i=0; i<spaces+stars; i++) {
if (i<spaces) {
builder.append(SPACE);
}else{
builder.append(STAR);
}
}
System.out.println(builder.toString());
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 10:08

























answered Nov 20 at 20:16









Skemelio

751214




751214








  • 1




    Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
    – Hugelevin
    Nov 21 at 19:17










  • No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
    – Skemelio
    Nov 21 at 19:21










  • By the way if you have any questions about the above code snippet just ask here in the comment section.
    – Skemelio
    Nov 21 at 19:22














  • 1




    Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
    – Hugelevin
    Nov 21 at 19:17










  • No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
    – Skemelio
    Nov 21 at 19:21










  • By the way if you have any questions about the above code snippet just ask here in the comment section.
    – Skemelio
    Nov 21 at 19:22








1




1




Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
– Hugelevin
Nov 21 at 19:17




Sure that will work, but I wanted to use the least code possible without having to create methods for it. I am a beginner so most of the logic you used is new for me and I don't quite seem to understand everything. I still appreciate your time to help me. Thanks.
– Hugelevin
Nov 21 at 19:17












No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
– Skemelio
Nov 21 at 19:21




No problem @Hugelevin. By the way, I've split the code in small methods just to be cleared and more understandable. On the way you will learn to break problems into small ones and eventually write your code somehow like this above, so people will understand it. Never forget that you don't write code just for yourself, you write code to be read by others, just like I did (tried at least). Cheers!
– Skemelio
Nov 21 at 19:21












By the way if you have any questions about the above code snippet just ask here in the comment section.
– Skemelio
Nov 21 at 19:22




By the way if you have any questions about the above code snippet just ask here in the comment section.
– Skemelio
Nov 21 at 19:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398939%2fjava-pattern-using-for-loop-stuck-in-the-last-part%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'