We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Java GUI program- VERY URGENT

alligarbage
alligarbage asked
on
Medium Priority
334 Views
Last Modified: 2010-03-31
Here is my problem:

Write a complete 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 do the following:

A. Print a rectangle of stars.
B. Error check for bad input: alphabetic characters or negative numbers.  Do not end the program but remind the user of the proper input.
C. Allow the user to use the program until the user decides to terminate the program.

List any functionality that is NOT in your completed program.

Here is the code I have so far:

import javax.swing.JOptionPane;
public class RectangleOfStars
{
      public static void main(String[] args)
      {
            int rows, columns;

            String rowsStr, columnsStr, outputStr;

            rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
            rows = Integer.parseInt(rowsStr);

            columnsStr = JOptionPane.showInputDialog("Enter the of columns: ");
            columns = Integer.parseInt(columnsStr);

            while(true) // endless loop.
            {
                  try
                  {
                        rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
                        rows = Integer.parseInt(rowsStr);

                        break;
                  }
                  catch(NumberFormatException)
                  {
                        // tell them the input was wrong
                        System.out.println("Please enter a positive integer.");
                  }
                  JOptionPane.showMessageDialog(null, outputStr, "Rectangle of Stars", JOptionPane.INFORMATION_MESSAGE);
                  System.exit(0);
                        
            //Print output
            for(int i = 0; i < rows; ++i)
            {
                  for(int j = 0; j < columns; ++j)
                  {
                  // print a star
                  System.out.print("*"); // System.out is a default PrintWriter given by JAVA
                   }
            // go to the next line
            System.out.println();
            }
      }
}
}

Here are the error messages I'm getting, so I can't even compile it.  Please help:

RectangleOfStars.java:25: <identifier> expected
                  catch(NumberFormatException)
                                                   ^
RectangleOfStars.java:30: ')' expected
                  JOptionPane.showMessageDialog(null, outputStr, "Rectangle of Stars", JOptionPane.INFORMATION_MESSAGE);

Thank you for your help
Comment
Watch Question

>>"catch(NumberFormatException)"
You should have the identifier as

catch(NumberFormatException nfe)
Why do you ask the same question again?
Okay,

I just saw it as well.

sciuriware is right. Why do you ask the same question again? I believe that this is the continuation of http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22043462.html
... and I fear that alligarbage doesn't understand any of our answers ...

;JOOP!

Author

Commented:
I asked the same question because I needed an answer asap and I thought that posting a new question would generate an answer sooner.  

Here is my updated code.  Still one error message.

import javax.swing.JOptionPane;
public class RectangleOfStars
{
      public static void main(String[] args)
      {
            int rows, columns;

            String rowsStr, columnsStr, outputStr;

            rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
            rows = Integer.parseInt(rowsStr);

            columnsStr = JOptionPane.showInputDialog("Enter the of columns: ");
            columns = Integer.parseInt(columnsStr);

            while(true) // endless loop.
            {
                  try
                  {
                        rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
                        rows = Integer.parseInt(rowsStr);

                        break;
                  }
                  catch(NumberFormatException nfe)
                  {
                        // tell them the input was wrong
                        System.out.println("Please enter a positive integer.");
                  }
                  JOptionPane.showMessageDialog(null, outputStr, "Rectangle of Stars", JOptionPane.INFORMATION_MESSAGE);
                  System.exit(0);
                        
            //Print output
            for(int i = 0; i < rows; ++i)
            {
                  for(int j = 0; j < columns; ++j)
                  {
                  // print a star
                  System.out.print("*"); // System.out is a default PrintWriter given by JAVA
                   }
            // go to the next line
            System.out.println();
            }
      }
}
}

Error message:

RectangleOfStars.java:30: variable outputStr might not have been initialized
                  JOptionPane.showMessageDialog(null, outputStr, "Rectangle of Stars", JOptionPane.INFORMATION_MESSAGE);
                                                            ^
1 error

Delete those 2 lines. Do you want to stop your program in the middle?

;JOOP!
And please learn the basics of programming first!
What use is it to complete this program without understanding it?

And: WE MAY NOT WRITE THIS PROGRAM FOR YOU!!!!

even if we wanted to do so,
besides, would you learn anything from that?

;JOOP!

Author

Commented:
At this point, I'm not trying to learn as much as I'm trying to get it turned in.  I have had a lot of family emergencies this term and haven't had much time to study this.  I realize that you cannot just give me the answer, but I just need as much help as I can get.

Thank you

I have deleted those two lines and asked another question in the other post for this.
Do you now understand what each line does?

If so tell me what is the use of lines 10, 11, 12, 13 & 14.

;JOOP!
alligarbage,

I know that it may be that you are frustated on this but we are not going to write the working, nice, tidy codes for you. We want you to understand so you have to attempt to do this no matter what happens. That is why sciuriware and I are here for. Additionally, I personally did NOT see any improvement of you since yesterday. You just keep asking the same thing again and again.

Go and search the basic Java programming URL. There are many that explains these codes.

Do not give up.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Are you referring to these lines:

                                rowsStr = JOptionPane.showInputDialog("Enter the number of rows: ");
            rows = Integer.parseInt(rowsStr);

            columnsStr = JOptionPane.showInputDialog("Enter the of columns: ");
            columns = Integer.parseInt(columnsStr);

If so, I'm not entirely sure why these lines are here, if I am asking them again in the loop.  I am thinking that these should probably be deleted and just asked in the loop.
I'm not suprised I've made no progress since yesterday.  I have gotten 2 hours of sleep and I'm stressing cause everything is due tomorrow.
Yes
alligarbage, this is no use!

You can't learn to swim by just diving from a bridge into a wide river.
It took us years.
;JOOP!

Author

Commented:
Make each question worth 250 points.
.. and say "please".

Author

Commented:
please and thank you :)

sorry if I was rude before.
I'm stressed, but that's no excuse.

You are stressed because you try to do something you can't do:
you must learn to program from the basics up, and NOT start
with a program you don't understand and in a language you don't master.

My advice:
 it's like making a long jump:
if you jump without a run, you don't loose time but you will fall short,
if you turn and make a run first, you'll loose time but you'll make it.
So loose some time you invest to be good in the end.

;JOOP!

Author

Commented:
I understand that, but I can't learn Java in two days and my assignments are due tonight.
I was just trying to get everything done.
Hopefully next time I won't have any family emergencies throughout the term that deters me from studying.
I wish I could help you but now I am extremely busy with my works.

Apologized but can't help you now.

Author

Commented:
Yes, I REALIZE that NO ONE will do the work for me.  lol
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.