?
Solved

Java, display a solid box

Posted on 2008-10-08
4
Medium Priority
?
276 Views
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)
       printFullLine();
    else
       printTwoStarsLine();
      }

// with the two methods being
{

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

                 
      } // end main

} // end
0
Comment
Question by:Wormnm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

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: http://java.sun.com/docs/books/tutorial/
private void printFullLine() 
}
      {
    for (int i = 0; i < iSize; i++)
       System.out.print("*");
    System.out.println("");
      }
}

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 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();

As:

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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

765 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