How to make box out of asterisks in java? nested loops?












-1















Hey I really need help with a program for my java programming class. I'll put in the instructions and the code I have thus far. Any help would be appreciated! Thanks in advance!!
Instructions:
Write a program called Box (Box.java) that will print/displays a hollow box shape
using asterisks (*). The program will read in an even number in the range 2 to 24 to
specify the number of rows/columns in the box. Display an error and re-prompt for
the number if an incorrect value was entered. The program will then display a hollow
of the appropriate size. Hint: Use loops within loops.
basically it should make a square box, so if you give it the number boxSize = 5 the output is a box with dimensions 5x5. the outline is made of asterisks but the inside is empty



heres what I have code wise so far



import java.util.Scanner;

public class Box
{

public static void main(Stringargs)
{
//numrows and numcols are equal however the spacing is differnt
Scanner input = new Scanner(System.in);
System.out.print("Enter an even number (2-24): ");
int boxSize = input.nextInt();

int numRows = boxSize;
int numCols = numRows;
// This program demonstrates compound decisions with the logical and operator &&
//asks if the number is less than or equal to 24 and greater than or equal to 2 and that the remainder is 0
//when divided by 2, checks if its an even number like it should be

if(boxSize >= 2 && boxSize <= 24 && boxSize%2 == 0)
{
//nested loops are used to print out the asterisk in the correct pattern
for(int r = 0; r<numRows; r++)
{
System.out.println("*");
for(int c = 0; c<numCols; c++)
{
System.out.print("*");
}
}
}
}

//This program demonstrates compound decisions with the logical or ( || ) operator
//checks if any of the following are true
//if one or more is true then that means that it is an incorrect number
//then reprompts the user to put in a new number then checks again
if(boxSize<2||boxSize>24||boxSize% 2 != 0)
{
System.out.println("Value must be an even number from 2-24");
}


Basically my problem is I don't know what to put in the loops and where to get the shape. I also don't know how to make it REPROMPT for a boxSize value again if the number is odd or not between 2 and 24 and it also needs to display the error message that value must be between 2 ans 24, and even ect.










share|improve this question

























  • Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

    – Dennis Meng
    Sep 11 '13 at 20:34
















-1















Hey I really need help with a program for my java programming class. I'll put in the instructions and the code I have thus far. Any help would be appreciated! Thanks in advance!!
Instructions:
Write a program called Box (Box.java) that will print/displays a hollow box shape
using asterisks (*). The program will read in an even number in the range 2 to 24 to
specify the number of rows/columns in the box. Display an error and re-prompt for
the number if an incorrect value was entered. The program will then display a hollow
of the appropriate size. Hint: Use loops within loops.
basically it should make a square box, so if you give it the number boxSize = 5 the output is a box with dimensions 5x5. the outline is made of asterisks but the inside is empty



heres what I have code wise so far



import java.util.Scanner;

public class Box
{

public static void main(Stringargs)
{
//numrows and numcols are equal however the spacing is differnt
Scanner input = new Scanner(System.in);
System.out.print("Enter an even number (2-24): ");
int boxSize = input.nextInt();

int numRows = boxSize;
int numCols = numRows;
// This program demonstrates compound decisions with the logical and operator &&
//asks if the number is less than or equal to 24 and greater than or equal to 2 and that the remainder is 0
//when divided by 2, checks if its an even number like it should be

if(boxSize >= 2 && boxSize <= 24 && boxSize%2 == 0)
{
//nested loops are used to print out the asterisk in the correct pattern
for(int r = 0; r<numRows; r++)
{
System.out.println("*");
for(int c = 0; c<numCols; c++)
{
System.out.print("*");
}
}
}
}

//This program demonstrates compound decisions with the logical or ( || ) operator
//checks if any of the following are true
//if one or more is true then that means that it is an incorrect number
//then reprompts the user to put in a new number then checks again
if(boxSize<2||boxSize>24||boxSize% 2 != 0)
{
System.out.println("Value must be an even number from 2-24");
}


Basically my problem is I don't know what to put in the loops and where to get the shape. I also don't know how to make it REPROMPT for a boxSize value again if the number is odd or not between 2 and 24 and it also needs to display the error message that value must be between 2 ans 24, and even ect.










share|improve this question

























  • Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

    – Dennis Meng
    Sep 11 '13 at 20:34














-1












-1








-1








Hey I really need help with a program for my java programming class. I'll put in the instructions and the code I have thus far. Any help would be appreciated! Thanks in advance!!
Instructions:
Write a program called Box (Box.java) that will print/displays a hollow box shape
using asterisks (*). The program will read in an even number in the range 2 to 24 to
specify the number of rows/columns in the box. Display an error and re-prompt for
the number if an incorrect value was entered. The program will then display a hollow
of the appropriate size. Hint: Use loops within loops.
basically it should make a square box, so if you give it the number boxSize = 5 the output is a box with dimensions 5x5. the outline is made of asterisks but the inside is empty



heres what I have code wise so far



import java.util.Scanner;

public class Box
{

public static void main(Stringargs)
{
//numrows and numcols are equal however the spacing is differnt
Scanner input = new Scanner(System.in);
System.out.print("Enter an even number (2-24): ");
int boxSize = input.nextInt();

int numRows = boxSize;
int numCols = numRows;
// This program demonstrates compound decisions with the logical and operator &&
//asks if the number is less than or equal to 24 and greater than or equal to 2 and that the remainder is 0
//when divided by 2, checks if its an even number like it should be

if(boxSize >= 2 && boxSize <= 24 && boxSize%2 == 0)
{
//nested loops are used to print out the asterisk in the correct pattern
for(int r = 0; r<numRows; r++)
{
System.out.println("*");
for(int c = 0; c<numCols; c++)
{
System.out.print("*");
}
}
}
}

//This program demonstrates compound decisions with the logical or ( || ) operator
//checks if any of the following are true
//if one or more is true then that means that it is an incorrect number
//then reprompts the user to put in a new number then checks again
if(boxSize<2||boxSize>24||boxSize% 2 != 0)
{
System.out.println("Value must be an even number from 2-24");
}


Basically my problem is I don't know what to put in the loops and where to get the shape. I also don't know how to make it REPROMPT for a boxSize value again if the number is odd or not between 2 and 24 and it also needs to display the error message that value must be between 2 ans 24, and even ect.










share|improve this question
















Hey I really need help with a program for my java programming class. I'll put in the instructions and the code I have thus far. Any help would be appreciated! Thanks in advance!!
Instructions:
Write a program called Box (Box.java) that will print/displays a hollow box shape
using asterisks (*). The program will read in an even number in the range 2 to 24 to
specify the number of rows/columns in the box. Display an error and re-prompt for
the number if an incorrect value was entered. The program will then display a hollow
of the appropriate size. Hint: Use loops within loops.
basically it should make a square box, so if you give it the number boxSize = 5 the output is a box with dimensions 5x5. the outline is made of asterisks but the inside is empty



heres what I have code wise so far



import java.util.Scanner;

public class Box
{

public static void main(Stringargs)
{
//numrows and numcols are equal however the spacing is differnt
Scanner input = new Scanner(System.in);
System.out.print("Enter an even number (2-24): ");
int boxSize = input.nextInt();

int numRows = boxSize;
int numCols = numRows;
// This program demonstrates compound decisions with the logical and operator &&
//asks if the number is less than or equal to 24 and greater than or equal to 2 and that the remainder is 0
//when divided by 2, checks if its an even number like it should be

if(boxSize >= 2 && boxSize <= 24 && boxSize%2 == 0)
{
//nested loops are used to print out the asterisk in the correct pattern
for(int r = 0; r<numRows; r++)
{
System.out.println("*");
for(int c = 0; c<numCols; c++)
{
System.out.print("*");
}
}
}
}

//This program demonstrates compound decisions with the logical or ( || ) operator
//checks if any of the following are true
//if one or more is true then that means that it is an incorrect number
//then reprompts the user to put in a new number then checks again
if(boxSize<2||boxSize>24||boxSize% 2 != 0)
{
System.out.println("Value must be an even number from 2-24");
}


Basically my problem is I don't know what to put in the loops and where to get the shape. I also don't know how to make it REPROMPT for a boxSize value again if the number is odd or not between 2 and 24 and it also needs to display the error message that value must be between 2 ans 24, and even ect.







java loops for-loop if-statement nested-loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 21 '18 at 3:08









eyllanesc

83.7k103562




83.7k103562










asked Sep 11 '13 at 20:11









Connie ClarkConnie Clark

4113




4113













  • Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

    – Dennis Meng
    Sep 11 '13 at 20:34



















  • Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

    – Dennis Meng
    Sep 11 '13 at 20:34

















Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

– Dennis Meng
Sep 11 '13 at 20:34





Could you add in the missing braces? I just removed some whitespace from your post and noticed they weren't lining up.

– Dennis Meng
Sep 11 '13 at 20:34












2 Answers
2






active

oldest

votes


















0














Your approach with nested loops is basically fine. Just the stuff inside is not quite what you are looking for.... What you want is more like this:



for(int r = 0; r<numRows; r++) {
for(int c = 0; c<numCols; c++) {
// print a "*" ONLY IF on border, and " " otherwise
}
// here you finished printing the "*"/" ", so print just a newline
}


for promting and reprompting I would use a do {} while(yourIfCondition) loop, to reshow the prompt.






share|improve this answer
























  • can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

    – Connie Clark
    Sep 12 '13 at 2:07











  • Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

    – Connie Clark
    Sep 12 '13 at 2:10











  • nevermind about the do while @subsub, I figured it out.

    – Connie Clark
    Sep 12 '13 at 2:21











  • spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

    – Connie Clark
    Sep 12 '13 at 2:28











  • do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

    – Connie Clark
    Sep 12 '13 at 2:28



















0














Here. I put all of it in one method. Notice there's so many ifs. You can optimize by using the ternary operator if you have reached that part in your course.



public static void main(String...args){
int boxSize = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("Enter box size [-1 to quit] >> ");
boxSize = input.nextInt();

if(boxSize == -1){
System.exit(0);
}

/* check if number is valid */
if(boxSize < 2 || boxSize > 24 || boxSize % 2 != 0){
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}

// draw the box
for (int col = 0; col < boxSize; col++) {
for (int row = 0; row < boxSize; row++) {
/* First or last row ? */
if (row == 0 || row == boxSize - 1) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == boxSize - 1 || col == 0) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == boxSize - 1) {
System.out.println();
}
}
}
}
}

}while (true);
}





share|improve this answer


























  • I misspelled height. But oh well

    – TheRealChx101
    Sep 11 '13 at 21:07











  • that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

    – Connie Clark
    Sep 12 '13 at 2:05











  • I changed it & good luck.

    – TheRealChx101
    Sep 12 '13 at 2:41











  • what are System.err and System.exit? Also what does line continue; do? Thanks!

    – Connie Clark
    Sep 12 '13 at 20:19













  • System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

    – TheRealChx101
    Sep 13 '13 at 3:00













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%2f18750340%2fhow-to-make-box-out-of-asterisks-in-java-nested-loops%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your approach with nested loops is basically fine. Just the stuff inside is not quite what you are looking for.... What you want is more like this:



for(int r = 0; r<numRows; r++) {
for(int c = 0; c<numCols; c++) {
// print a "*" ONLY IF on border, and " " otherwise
}
// here you finished printing the "*"/" ", so print just a newline
}


for promting and reprompting I would use a do {} while(yourIfCondition) loop, to reshow the prompt.






share|improve this answer
























  • can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

    – Connie Clark
    Sep 12 '13 at 2:07











  • Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

    – Connie Clark
    Sep 12 '13 at 2:10











  • nevermind about the do while @subsub, I figured it out.

    – Connie Clark
    Sep 12 '13 at 2:21











  • spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

    – Connie Clark
    Sep 12 '13 at 2:28











  • do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

    – Connie Clark
    Sep 12 '13 at 2:28
















0














Your approach with nested loops is basically fine. Just the stuff inside is not quite what you are looking for.... What you want is more like this:



for(int r = 0; r<numRows; r++) {
for(int c = 0; c<numCols; c++) {
// print a "*" ONLY IF on border, and " " otherwise
}
// here you finished printing the "*"/" ", so print just a newline
}


for promting and reprompting I would use a do {} while(yourIfCondition) loop, to reshow the prompt.






share|improve this answer
























  • can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

    – Connie Clark
    Sep 12 '13 at 2:07











  • Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

    – Connie Clark
    Sep 12 '13 at 2:10











  • nevermind about the do while @subsub, I figured it out.

    – Connie Clark
    Sep 12 '13 at 2:21











  • spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

    – Connie Clark
    Sep 12 '13 at 2:28











  • do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

    – Connie Clark
    Sep 12 '13 at 2:28














0












0








0







Your approach with nested loops is basically fine. Just the stuff inside is not quite what you are looking for.... What you want is more like this:



for(int r = 0; r<numRows; r++) {
for(int c = 0; c<numCols; c++) {
// print a "*" ONLY IF on border, and " " otherwise
}
// here you finished printing the "*"/" ", so print just a newline
}


for promting and reprompting I would use a do {} while(yourIfCondition) loop, to reshow the prompt.






share|improve this answer













Your approach with nested loops is basically fine. Just the stuff inside is not quite what you are looking for.... What you want is more like this:



for(int r = 0; r<numRows; r++) {
for(int c = 0; c<numCols; c++) {
// print a "*" ONLY IF on border, and " " otherwise
}
// here you finished printing the "*"/" ", so print just a newline
}


for promting and reprompting I would use a do {} while(yourIfCondition) loop, to reshow the prompt.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 11 '13 at 20:30









subsubsubsub

1,693820




1,693820













  • can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

    – Connie Clark
    Sep 12 '13 at 2:07











  • Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

    – Connie Clark
    Sep 12 '13 at 2:10











  • nevermind about the do while @subsub, I figured it out.

    – Connie Clark
    Sep 12 '13 at 2:21











  • spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

    – Connie Clark
    Sep 12 '13 at 2:28











  • do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

    – Connie Clark
    Sep 12 '13 at 2:28



















  • can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

    – Connie Clark
    Sep 12 '13 at 2:07











  • Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

    – Connie Clark
    Sep 12 '13 at 2:10











  • nevermind about the do while @subsub, I figured it out.

    – Connie Clark
    Sep 12 '13 at 2:21











  • spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

    – Connie Clark
    Sep 12 '13 at 2:28











  • do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

    – Connie Clark
    Sep 12 '13 at 2:28

















can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

– Connie Clark
Sep 12 '13 at 2:07





can you elaborate on how I might check if its on the border to print the ? Also, after the inner loop you said "here you finished printing the ""/" "" so does that mean just print an empty line like System.out.println();?

– Connie Clark
Sep 12 '13 at 2:07













Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

– Connie Clark
Sep 12 '13 at 2:10





Also forgot to ask when you reprompt the user I know what should go in the do part (error message and ask for scanner input again) but I have 2 questions: 1. can I still call the int boxSize like I did earlier? 2. what kind of condition should I use? Because I have no idea how many times someone might but in incorrect data. What would you recommend? Anyway, thanks for your help, I appreciate it!

– Connie Clark
Sep 12 '13 at 2:10













nevermind about the do while @subsub, I figured it out.

– Connie Clark
Sep 12 '13 at 2:21





nevermind about the do while @subsub, I figured it out.

– Connie Clark
Sep 12 '13 at 2:21













spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

– Connie Clark
Sep 12 '13 at 2:28





spoke too soon I put this for the do while and it just keeps reprompting even if I give it acceptable data

– Connie Clark
Sep 12 '13 at 2:28













do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

– Connie Clark
Sep 12 '13 at 2:28





do { System.out.println("Value must be an even number from 2-24"); System.out.print("Enter an even number (2-24): "); int boxDimension = input.nextInt(); }while(boxSize<2||boxSize>24||boxSize% 2 != 0);

– Connie Clark
Sep 12 '13 at 2:28













0














Here. I put all of it in one method. Notice there's so many ifs. You can optimize by using the ternary operator if you have reached that part in your course.



public static void main(String...args){
int boxSize = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("Enter box size [-1 to quit] >> ");
boxSize = input.nextInt();

if(boxSize == -1){
System.exit(0);
}

/* check if number is valid */
if(boxSize < 2 || boxSize > 24 || boxSize % 2 != 0){
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}

// draw the box
for (int col = 0; col < boxSize; col++) {
for (int row = 0; row < boxSize; row++) {
/* First or last row ? */
if (row == 0 || row == boxSize - 1) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == boxSize - 1 || col == 0) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == boxSize - 1) {
System.out.println();
}
}
}
}
}

}while (true);
}





share|improve this answer


























  • I misspelled height. But oh well

    – TheRealChx101
    Sep 11 '13 at 21:07











  • that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

    – Connie Clark
    Sep 12 '13 at 2:05











  • I changed it & good luck.

    – TheRealChx101
    Sep 12 '13 at 2:41











  • what are System.err and System.exit? Also what does line continue; do? Thanks!

    – Connie Clark
    Sep 12 '13 at 20:19













  • System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

    – TheRealChx101
    Sep 13 '13 at 3:00


















0














Here. I put all of it in one method. Notice there's so many ifs. You can optimize by using the ternary operator if you have reached that part in your course.



public static void main(String...args){
int boxSize = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("Enter box size [-1 to quit] >> ");
boxSize = input.nextInt();

if(boxSize == -1){
System.exit(0);
}

/* check if number is valid */
if(boxSize < 2 || boxSize > 24 || boxSize % 2 != 0){
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}

// draw the box
for (int col = 0; col < boxSize; col++) {
for (int row = 0; row < boxSize; row++) {
/* First or last row ? */
if (row == 0 || row == boxSize - 1) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == boxSize - 1 || col == 0) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == boxSize - 1) {
System.out.println();
}
}
}
}
}

}while (true);
}





share|improve this answer


























  • I misspelled height. But oh well

    – TheRealChx101
    Sep 11 '13 at 21:07











  • that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

    – Connie Clark
    Sep 12 '13 at 2:05











  • I changed it & good luck.

    – TheRealChx101
    Sep 12 '13 at 2:41











  • what are System.err and System.exit? Also what does line continue; do? Thanks!

    – Connie Clark
    Sep 12 '13 at 20:19













  • System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

    – TheRealChx101
    Sep 13 '13 at 3:00
















0












0








0







Here. I put all of it in one method. Notice there's so many ifs. You can optimize by using the ternary operator if you have reached that part in your course.



public static void main(String...args){
int boxSize = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("Enter box size [-1 to quit] >> ");
boxSize = input.nextInt();

if(boxSize == -1){
System.exit(0);
}

/* check if number is valid */
if(boxSize < 2 || boxSize > 24 || boxSize % 2 != 0){
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}

// draw the box
for (int col = 0; col < boxSize; col++) {
for (int row = 0; row < boxSize; row++) {
/* First or last row ? */
if (row == 0 || row == boxSize - 1) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == boxSize - 1 || col == 0) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == boxSize - 1) {
System.out.println();
}
}
}
}
}

}while (true);
}





share|improve this answer















Here. I put all of it in one method. Notice there's so many ifs. You can optimize by using the ternary operator if you have reached that part in your course.



public static void main(String...args){
int boxSize = 0;
Scanner input = new Scanner(System.in);

do {
System.out.print("Enter box size [-1 to quit] >> ");
boxSize = input.nextInt();

if(boxSize == -1){
System.exit(0);
}

/* check if number is valid */
if(boxSize < 2 || boxSize > 24 || boxSize % 2 != 0){
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}

// draw the box
for (int col = 0; col < boxSize; col++) {
for (int row = 0; row < boxSize; row++) {
/* First or last row ? */
if (row == 0 || row == boxSize - 1) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == boxSize - 1 || col == 0) {
System.out.print("*");
if (row == boxSize - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == boxSize - 1) {
System.out.println();
}
}
}
}
}

}while (true);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 12 '13 at 2:39

























answered Sep 11 '13 at 21:02









TheRealChx101TheRealChx101

8551130




8551130













  • I misspelled height. But oh well

    – TheRealChx101
    Sep 11 '13 at 21:07











  • that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

    – Connie Clark
    Sep 12 '13 at 2:05











  • I changed it & good luck.

    – TheRealChx101
    Sep 12 '13 at 2:41











  • what are System.err and System.exit? Also what does line continue; do? Thanks!

    – Connie Clark
    Sep 12 '13 at 20:19













  • System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

    – TheRealChx101
    Sep 13 '13 at 3:00





















  • I misspelled height. But oh well

    – TheRealChx101
    Sep 11 '13 at 21:07











  • that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

    – Connie Clark
    Sep 12 '13 at 2:05











  • I changed it & good luck.

    – TheRealChx101
    Sep 12 '13 at 2:41











  • what are System.err and System.exit? Also what does line continue; do? Thanks!

    – Connie Clark
    Sep 12 '13 at 20:19













  • System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

    – TheRealChx101
    Sep 13 '13 at 3:00



















I misspelled height. But oh well

– TheRealChx101
Sep 11 '13 at 21:07





I misspelled height. But oh well

– TheRealChx101
Sep 11 '13 at 21:07













that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

– Connie Clark
Sep 12 '13 at 2:05





that helps and I definitely would have done that approach myself, however the class I'm in is beginners java(credits didn't transfer so I'm in a way too easy class) and they wont let me use OOP, everything has to be in the main method

– Connie Clark
Sep 12 '13 at 2:05













I changed it & good luck.

– TheRealChx101
Sep 12 '13 at 2:41





I changed it & good luck.

– TheRealChx101
Sep 12 '13 at 2:41













what are System.err and System.exit? Also what does line continue; do? Thanks!

– Connie Clark
Sep 12 '13 at 20:19







what are System.err and System.exit? Also what does line continue; do? Thanks!

– Connie Clark
Sep 12 '13 at 20:19















System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

– TheRealChx101
Sep 13 '13 at 3:00







System.err is identical to System.out only that System.err is for printing errors. System.exit closes your application. Continue goes back to continue with the loop without executing any code after it (continue).

– TheRealChx101
Sep 13 '13 at 3:00




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18750340%2fhow-to-make-box-out-of-asterisks-in-java-nested-loops%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini