Solved

Java GUI program- VERY URGENT

Posted on 2006-10-31
23
309 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
0
Comment
Question by:alligarbage
[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
  • 9
  • 7
  • 4
23 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17847999
>>"catch(NumberFormatException)"
You should have the identifier as

catch(NumberFormatException nfe)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848362
Why do you ask the same question again?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17848393
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:sciuriware
ID: 17848403
... and I fear that alligarbage doesn't understand any of our answers ...

;JOOP!
0
 

Author Comment

by:alligarbage
ID: 17848411
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

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848432
Delete those 2 lines. Do you want to stop your program in the middle?

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848438
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!
0
 

Author Comment

by:alligarbage
ID: 17848501
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.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848510
Do you now understand what each line does?

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

;JOOP!
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 250 total points
ID: 17848520
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.
0
 

Author Comment

by:alligarbage
ID: 17848527
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.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848538
Yes
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17848544
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!
0
 

Author Comment

by:alligarbage
ID: 17855177
Make each question worth 250 points.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17855969
.. and say "please".
0
 

Author Comment

by:alligarbage
ID: 17856079
please and thank you :)

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

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17856149
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!
0
 

Author Comment

by:alligarbage
ID: 17856337
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.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17856545
I wish I could help you but now I am extremely busy with my works.

Apologized but can't help you now.
0
 

Author Comment

by:alligarbage
ID: 17863990
Yes, I REALIZE that NO ONE will do the work for me.  lol
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

717 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