• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help with a simple GUI.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am having some problems with a simple GUI program that I am writing for school. I have read the text and examples a few times, but I can't seem to figure out what I am doing wrong.

If someone can take a look at this and give me some feedback, I would appreciate it.

Thanks,
J


[ July 20, 2005: Message edited by: jay smith ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am having some problems with a simple GUI program


Could you describe these problems? Or perhaps post a stack trace if your code is throwing an Exception?
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not able to output the results. I am not sure if I am not using toString correctly or what.

Thanks,
J

 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have any suggestions? I have just about reached the point of pulling hair out.

Is it a problem with parseInt?

I am tracing back over it again. If anyone can give me a pointer, I would appreciate it.

Thanks,
J
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I am not getting output because the input values are not getting set.

What am I doing wrong?

I am getting the following errors.

Thanks,
J

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the stack trace, it looks like you're trying to run your program with the appletviewer. But it's not an applet, so that's one part of the problem right there. You need to run your program from the command line using

java RationalTester

The only reason you're getting a GUI on screen is because you're setting everything up in the constructor, which would be a no-no for an applet, anyway.

Another problem is that you carefully display the output in the TextArea "displayArea", which isn't displayed on the screen. You've got "resultsField" on the bottom, there, but it isn't used to display anything!

Otherwise, I'm stumped. Sure looks like it should work, and I can't see what could cause those stack traces. Are you sure you're showing us exactly the same code you're running?
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made a few changes to the RationalTester class, but I have not made any changes to the Rational class. The code is posted below. I really appreciate your assistance. I am getting this stack trace:




 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only get the error when I click on the "Divide" button. Nothing happens at all when I enter numbers into the text field and press enter or when I click on the other three buttons.

Thanks,
J
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Catch the Aritmetic Exception which is caused by diving by zero.
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't learned catches yet so I'm not going to attempt it.

I would like to know why I am getting this exception to begin with.

My constructor sets the denominator to 1.

Also, why am I not able to display output?

It seems like I am doing something wrong in the set methods.

It's things like this that keep me busy for hours.


[ July 20, 2005: Message edited by: jay smith ]
[ July 20, 2005: Message edited by: jay smith ]
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Catch the Aritmetic Exception which is caused by diving by zero.
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand what you mean.

I have looked ahead in my textbook and I have seen catches, but I don't understand how they work.

Does that actually fix the problem that I am having in my code or does it just avoid the problem?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK here is something I see.



in the setDenom, it is setting it to 0.

The tough thing about debugging, is that if there is code like qoutient.setDemon(Method call in here to get value), it is difficult to know the exact value returned. Now if the code was like



It is easy to System.out.println(denom); or System.out.println(ratValueNumerator) to make sure that they return values that you expect.

Also there is a check



This might not return the results you expect. It is better to use the equals method to check the value of Strings to one another.

I hope that helps a bit.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your results aren't showing because you setText on the outputArea JTextArea that you created, but never added it to the screen. Instead you put in



resultField in the area you thought you'd put the outputArea reference.

Mark
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,

You bring up a good point. If I assign 0 to numerator and 1 to denominator, then try to multiply by the reciprocal of that, then 0 ends up as the denominator.

That is another good point about using .equals to compare strings.

I do feel like I am learning this.

I am going to go to bed. It's 2:34am here and I have been going at this way too long. I'll be back on here tomorrow if I have any more questions about this program.

Thanks everyone!!!
J
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is another good point about using .equals to compare strings.



Not that your code is failing right now, but if the left side was really null or "", then it would fail to work.

Mark
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way you've written that event handler now, you are required to hit Enter after making every entry in a text field. If you don't, the values won't be transferred, and you'll have zeroes instead. There's the error you're seeing right there.

Moral of the story: make sure you show people the exact code you're running, or they won't be able to help!
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm am not sure how to fix the problem with the ActionListener. When I try to put the set methods in the else if statements, I get a StackOverflowError.

Below is my latest code. The Rational class has not changed.

Thanks,
J
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would take these four lines:

rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );

out of the "if", put them into their own method, and call them from each one of the "else"s. There's no need to call it from the "if" but you could if you wanted.

But these lines don't handle the possibility that a text field contains not-a-number, so you're going to see NumberFormatExceptions all over the place. That little method would be a good place to put those handlers.
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I have put the ActionListeners into inner classes. I am just trying a different approach to see if I can narrow down the problem.

Thanks,
J

 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip on NumberFormatExceptions. Here is what I am getting:

When I compile the code I just posted, it compiles fine.

When I run it and try to test it, the add and multiply buttons seem be working a little, but the subtract and divide buttons give me this:


But it repeats "at Rational.getGCD(Rational.java:225)" many times.

Thanks,
J
[ July 20, 2005: Message edited by: jay smith ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because getGCD() is recursive; it calls itself, and if it never stops calling itself, then the stack will overflow. You need to make sure that the exit condition will always occur -- i.e., that numer % denom always becomes zero (hint -- it clearly doesn't!)
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense. Thank you for pointing that out.

I have fixed the recursive gcd method. Here is the code.


I have updated the RationalTester class again. The code is below. The program is now accepting input, except it is assigning the value of the second rational to the first rational also.

 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, still no luck. I have been through it a few times. I have made some changes. I would like to allow the user to specify the number of decimal points shown. Am I on the right path here?

I wonder why the value of rat2 is also getting assigned to rat1?

Thanks,
J

 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still cannot figure out what is wrong with my program. Also, I am getting this:

Any help is extremely appreciated.

Thanks,
J

I did figure out how to let a user decide the number of decimal places.

I wrote this to help me with the decimal format part:
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I feel dumb. I copied and pasted some of the code as I was writing the program and I forgot to change the names so the JTextFields were all named "second".

I still can't get the decimal format working. It looks like everything else is working fine.

Is anyone around to help?

Thanks,
J
 
jay smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any takers?

I really don't understand what I am doing wrong (or not doing at all) to allow the user to set the number of decimal places.

Thanks,
J
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't see what I am doing wrong.

J
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, here is what I am working with.

So to make sure I know what is going on, I changed

output = decimalFormat.format( Math.PI );

to

output = decimalFormat.format( decimalPlaces );

The program is displaying the number of zeros that the user inputs.
The program is also displaying the input number to the left of the decimal.

Is this just because the new decimal format has not been used to set the decimal place of any other variable yet?

I guess my question is that I do not understand how the actual number that the user inputs is being assigned to the left of the decimal.

DecimalFormat uses patterns.

In the code that you posted, the pattern is "#.".

So the # is the number that the user enters.

Is that right?

How do I use this to apply it to a sum of two fractions or something similar?
I am wanting to be able to let a user input two rational numbers (two numerators and two denominator), and enter the number of decimal places they prefer to see. Then the user can add, subtract, multiply, or divide the two fractions. I have everything working except for this part. I just want to understand how this works. Sorry for all the questions.

Thanks,
J
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic