?
Solved

GUI to print stars rectangles with user input

Posted on 2006-05-04
6
Medium Priority
?
870 Views
Last Modified: 2008-03-10
I am writing an interactive Java GUI program that requests two numbers from the user. The first number is the row dimension and the second number is the column dimension. Print to the user a rectangle of stars based on the input. The program must, print the rectangle of stars, error check for bad input: alphabetic characters or negative numbers. Do not end the program but remind the user of the proper input, and allow the user to use the program until the user decides to terminate the program.

I am stuck, this is as far as I can get, what else do I need.


import javax.swing.JOptionPane;
public class RectangleOfStars
{
      public static void main(String[] args)
      {
            double rows, columns;
            
            String rowsStr, columnsStr, outputStr;
            
            rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
            rows = Double.parseDouble(rowsStr);
            
            columnsStr = JOptionPane.showInputDialog("Enter the of columns: ");
            columns = Double.parseDouble(columnsStr);
            
            stars =
            
            outputStr = stars ;
                                                
            JOptionPane.showMessageDialog(null, outputStr, "Rectangle of Stars", JOptionPane.INFORMATION_MESSAGE);
                                    System.exit(0);
                                    }
                              }
0
Comment
Question by:sendhelp
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 2000 total points
ID: 16611845
http://lab.privacy.cs.cmu.edu/courses/java2/library/unit3/lecture.html

check out shapetest1 qill suit u in the above link
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16612272
>> stars =
>> outputStr = stars ;

'stars' is not defined here, so you will get a compilation error.

You are not really creating a rectangle anywhere, are you? You need loops to do that. I'll give you an algo:

for i = 0 to rows - 1
  begin
  for j = 0 to columns - 1
    begin
      print *
    end
  print new line
  end

>> allow the user to use the program until the user decides to terminate the program

You will another loop, like a do-while loop enclosing everything else for doing that. Ask the user his choice (Y/ N). If he enters Y, loop again otherwise exit the loop.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16612457
>> allow the user to use the program until the user decides to terminate the program

interact with user by JOptionPane and ask him whether or not he/she wants to continue
also this is redundant, because a GUI based application can be terminated whenever the user decides by clicking on the close button, or any other option you have provided for them

I think you have to define the row,col variables as integer. printing a fractional number of stars does not have any meening.
also use Integer.parseInt() and it will check bad inputs (alphabet) for you
to check for negative numbers use an if else statement
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16612529
Yes, ints can be used, though your program will run if you use doubles too. If you want to use the JOptionPane only for taking user-input but want to display the output on the console, it should be ok (at least run-able).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16613993
sendhelp, do you know you can split the points too?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16614112
boring :(
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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