Link to home
Create AccountLog in
Avatar of sendhelp
sendhelp

asked on

GUI to print stars rectangles with user input

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);
                                    }
                              }
ASKER CERTIFIED SOLUTION
Avatar of Siva Prasanna Kumar
Siva Prasanna Kumar
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
>> 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.
>> 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
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).
sendhelp, do you know you can split the points too?
boring :(