Java, display a solid box

Posted on 2008-10-08
Last Modified: 2013-11-23
I am trying to learn JAVA code. I been working on a problem that i cant get to work. could some one please help me out. I dont know why I keep getting an error on line 27. code is attached for review.

import javax.swing.JOptionPane; // import class JOptionPane

public class Asterisks
   public static void main( String args[] )

         // prompt user to enter size of box
         String size =
            JOptionPane.showInputDialog( "Enter the size of box 1 - 12." );
         int iSize = Integer.parseInt(size);
    for (int i = 0; i < iSize; i++) {
    if (i==0 || i==iSize-1)

// with the two methods being

private void printFullLine()
    for (int i = 0; i < iSize; i++)
private void printTwoStarsLine()
    for (int i = 0; i < iSize; i++)
       System.out.print( (i==0 || i==iSize-1) ? "*" : " ");

      } // end main

} // end
Question by:Wormnm
  • 2
LVL 59

Expert Comment

by:Kevin Cross
ID: 22674687
Firstly, take a look at the syntax of your method declaration.

They should be of form:

modifier returntype methodname(params) {
    // some code

Instead of that you have som extra } in code...

Think that should clear up a bit if fixed.

Since you indicated you are learning, you can take a look here for more syntax assistance:
private void printFullLine() 



    for (int i = 0; i < iSize; i++)





Open in new window

LVL 59

Accepted Solution

Kevin Cross earned 125 total points
ID: 22674736
Stepping through the rest of the code, here are my observations:
+You main method should end before declaration of other methods, they should not be nested in the main method.
+For sharing of the variables which is what it appears you wanted to do by nesting, you either need to declare as global or pass through parameters.  Since you are calling from a loop, I would probably go with a parameter here -- but each programmer has their own style.
+Static methods cannot access non-static members of same class unless an instance of that class is instantiated.  Other fix is make non-static members, static.
+(IMHO) while learning and even after, I would stick with using { and }.  Get in the habit of coding those in even before you know the logic that goes between.  Put those on page and a // TODO: with some information on what task you are trying to accomplish.

Use of stylish shortcuts like the inline if statement you have in printTwoStarsLine is great, but (again me personally) will tend to write:

if (i == 0) doSomething();


if (i == 0) { doSomething(); }

Still get the nice one liner, but clear what I am doing if text wraps, etc.

Hope all that helps and is clear.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now