0

In my computer science class we were asked to create a program that would prompt the user to enter how many rows, columns, and what symbol they would like printed in a "magic box" and to store each of these variables and print them their very own magic box using a nested for loop. My program compiles properly and prints the correct box based on my inputs, but it does not print two of my print statements. It will print the first three statements (the ones prompting the user to enter certain things), but does not print the statements

Here comes the magic...Here's your very own magic box

and

This magic box brought to you by Beth Tanner."

I have tried everything I can think of and still cannot get these statements to print, any help would be greatly appreciated. I am including my program below.

import java.util.Scanner;

public class MagicBox {
  public static void main(String[] args) {
    Scanner input= new Scanner(System.in);

    System.out.println("How many rows would you like in your box?");
      int rows = input.nextInt();
    System.out.println("How many columns would you like in your box?");
      int columns = input.nextInt();
    System.out.println("What symbol would you like in your box?");
      String symbol = input.next(); 

    System.out.println("Here comes the magic...\nHere's your very own magic box!");

    int count1;
    int count2;
      for(count1 = 1; count1 <= rows; count1++) 
        for (count2 = 1; count2 <= columns; count2++)
          System.out.print(symbol);
          System.out.println(); 
      System.out.println("This magic box brought to you by Beth Tanner.");   

  } // end main
} // end class
  • 19
    Use braces on your loops for clarity – David Oct 29 '13 at 14:44
  • 3
    Java is not a whitespace-sensitive language. Your indent is wrong, and is misleading you. – SLaks Oct 29 '13 at 14:45
  • 6
    I remember once I was asked to write a program to does magic with rabbit, and because I didn't use braces, the rabbit actually died :( So always remember to use braces to save the rabbit :) – Maroun Oct 29 '13 at 14:46
  • 2
    When I run your program it does print those lines. Is this the actual code that isn't working for you? – Reinstate Monica -- notmaynard Oct 29 '13 at 14:48
  • 4
    @MarounMaroun I'm sorry to hear about your rabbit. We've all lost a loved one due to improper loop-block bracing. – Reinstate Monica -- notmaynard Oct 29 '13 at 14:49
  • @ ianmnotmaynard- yes, I literally copied and pasted the code from dr java onto this post. –  Oct 29 '13 at 14:50
  • is that the \n inside println causing the prob??? – sam Oct 29 '13 at 14:51
  • 1
    @iamnotmaynard That's Ok.. My rabbits were happy when I moved to Python. – Maroun Oct 29 '13 at 14:52
  • Any suggestions as to where to place the brackets? I figured that was the problem, but no matter where I place them, it still does not print. I'm still really new to this stuff. –  Oct 29 '13 at 14:52
  • Does the program end or do you have to cancel it? – Ingo Oct 29 '13 at 14:54
  • @Ingo it ends on it's own. –  Oct 29 '13 at 14:56
  • Does it give any sort of error? What inputs are you using (i.e. what do you enter for rows, columns, symbol)? – Reinstate Monica -- notmaynard Oct 29 '13 at 14:56
  • @iamnotmaynard When I compile I do not receive any error messages. I've used a variety of inputs, normally 3 for rows, 4 columns, and @ for the symbol. –  Oct 29 '13 at 14:57
  • *Magic Box* always make me think to something with [a clay brick inside](http://www.dailytech.com/Scam+Artists+Replace+iPad+2s+with+Blocks+of+Clay+in+Canada+Return+Them+for+Full+Refunds/article23809.htm) :D – Andrea Ligios Oct 29 '13 at 14:58
  • What version (`java -version`) are you using? Does it print out your box, or just stop after asking you for the symbol? It _should_ be working, and the fact it's not confuses me. – Reinstate Monica -- notmaynard Oct 29 '13 at 15:04
  • Dr. Java is apparently an IDE (I've never heard of it). Does it have an error console, or log window or something? – Reinstate Monica -- notmaynard Oct 29 '13 at 15:07
  • @iamnotmaynard I am using DrJava Version: drjava-20130901-r5756. I checked for an update but it says none are available. And it does print the box. It asks how many rows and allows me to answer, then how many columns, I answer, then what symbol, after I answer it prints the box. –  Oct 29 '13 at 15:10
  • Ok, I just downloaded Dr. Java and it still works as expected. In the "Compiler Output" tab, what is listed in the "Compiler" drop-down? – Reinstate Monica -- notmaynard Oct 29 '13 at 15:25

3 Answers3

2

Using correct blocks, everything works.

Note that the outer loop must enclose the newline produced by System.out.println();. In your code this newline is only printed afer all row * columns symbols have been printed on one line.

int rows = 5;
int columns = 3;
String symbol = "@";

System.out.println("Here comes the magic...\nHere's your very own magic box!");

for (int count1 = 1; count1 <= rows; count1++) {
    for (int count2 = 1; count2 <= columns; count2++) {
        System.out.print(symbol);
    }
    System.out.println();
}

System.out.println("This magic box brought to you by Beth Tanner.");

Output:

Here comes the magic...
Here's your very own magic box!
@@@
@@@
@@@
@@@
@@@
This magic box brought to you by Beth Tanner.
0

I have no idea what Magic box is, but I assume you want something like this:

for(count1 = 1; count1 <= rows; count1++) {
    for (count2 = 1; count2 <= columns; count2++) {
      System.out.print(symbol);
    }
    System.out.println();
  }

There are a couple of problems with your initial code:

  • There is no "collumns" variable declared - you have a typo there and it's actually columns
  • Always use curly braces in loops. Without those you only one expression will be executed in each loop, which means that System.out.println() will only be invoked once as opposed to being added after every row.
dimoniy
  • 5,820
  • 2
  • 24
  • 22
0

I think you want this:

for(count1 = 1; count1 <= rows; count1++){ 
  for (count2 = 1; count2 <= columns; count2++)
    System.out.print(symbol);

  System.out.println(); 
}

or even clearer

for(count1 = 1; count1 <= rows; count1++){ 
  for (count2 = 1; count2 <= columns; count2++){
    System.out.print(symbol);
  }

  System.out.println(); 
}

This will give you the magic box you desire.

This link may help you see the difference: stackoverflow question on omitting braces

Community
  • 1
  • 1
Bruce
  • 2,406
  • 5
  • 29
  • 35