?
Solved

Java GUI program- VERY URGENT

Posted on 2006-10-31
23
Medium Priority
?
315 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 750 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 9 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