Solved

Java GUI program- VERY URGENT

Posted on 2006-10-31
23
295 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
Securing Jmx Console and web console 2 48
Java and GPO 11 47
thymeleaf natural templating vs JSP 2 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now