This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problem with parse int method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with parse int method" Watch "problem with parse int method" New topic
Author

problem with parse int method

natasha henry
Greenhorn

Joined: Apr 12, 2007
Posts: 20
dittmer, craig and campbell, how are you? i just wanted you to know firstly that the tips did help alot and you know what, that long coding that i had, worked beautifully, as far as displaying the screen. however, i am at a standstill. i am trying to convert some string input into values by using the parseInt method but i keep getting the error message:
incompatible type found void, required int

get back to me as soon as possible please and lots of love out to you guys.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
We're very well, thank you.

Where do you get the error? I tried to compile your text (copy and paste) and got several errors about unable to find classes, which I presume are in the tasha package. But nothing about incompatible types. You are only using parseInt in one location (as far as I can tell), which doesn't have "void;" it takes String and returns int. When I uncommented your actionPerformed method, I got 4 errors, some about not being able to find classes in the .tasha package, and one about cannot find qtyval2 or something, which probably has to do with a spelling error between qtyval2 and qyval2.

You would do well to implement a synchronized closeDown() method which does all sorts of things like saving to file, and call it before your System.exit(0), so you make sure there are no loose ends when you close the app. Otherwise you are liable to turn it on again and find your database has been corrupted.
You are right to comment out your actionPerforemd method. In fact I don't think it ought to be there at all. I think you ought to have separate classes to handle your events. Have I mentioned that before?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
I have now found quite a lot of parseInts, having looked more carefully, but I don't think any of them is the sourece of your problem, As I said, I don't get any errors about incompatible type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Check carefully what the return type of your partCalc method is. And what is the type of subTotalvalInput3?
natasha henry
Greenhorn

Joined: Apr 12, 2007
Posts: 20
OK-i am going to show you the program that i am using in the tasha package to call the calculation methods. run these two together and then you will probably see my problem.


[ April 19, 2007: Message edited by: natasha henry ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Thank you. I copied and pasted it, and apart from adding my own package name, made no other changes.
It ran first time.
natasha henry
Greenhorn

Joined: Apr 12, 2007
Posts: 20
you mean that the package and the other class ran without any error and you were able to test the calculations in the textfields?
natasha henry
Greenhorn

Joined: Apr 12, 2007
Posts: 20
if that is the case, could you repost what you have please so i can see what may be so different about it bcuz i am still getting the error messages
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
No, I couldn't test the calculations. But it compiled and displayed your GUI
natasha henry
Greenhorn

Joined: Apr 12, 2007
Posts: 20
oh-i thought you included the section for the calculations. so what do you recommend me to do bcuz i need to apply calculations to some of the text fields
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
No, sorry. We are only too pleased to help people with problems with their code, not give them our code.
What you need to do is
  • Save your work in its present form, in case anything goes wrong and you have to go back and start again.
  • get a testCalculation method
  • Put something in it which reads the price from the JTextFields
  • Add an ActionListener to one of your buttons which calls the testCalculation method
  • Parse any Strings to numbers as required
  • Check for incorrect numbers (NumberFormatException)
  • Multiply "price" by "count" and add them all up.
  • Print the whole working to System.out, so you can see what is happening
  • If you are using an IDE, refactor the name of testCalculation to calculateTotalPrice or similar.


  • I shall fulminate about action listeners here.



    Lots of books, and the Java Tutorial use "addActionListener(this)" passim. A violation of the principles of object-oriented programming, because you end up with an enormous method somewhere full of nasty things like thisNow try to add button 6.

    A total mess. The only instance where addActionListener(this) is where the event directly affects the Component. A MouseListener where you display the x y coordinates of the last click, or a WindowListener which does something when the user tries to close the window, are good examples.

    I have a simple rule of thumb, which tells me that you need to decide whether there is any chance of using the same Listener more than once.
  • Are you going to use the same action or something similar more than once? A class which implements the Listener.
  • Are you going to use the same action or something similar and reuse it in different classes? A public class which implements the Listener.
  • Are you going to apply this Listener to one Component and one Component only, and not use anything similar anywhere else? An anomymous inner class.
  • There is also the option of using an Adapter class, but I don't like them; the tiniest spelling error can produce a Listener which doesn't work and you can't see why. You can also create an Action and add it to the button, but I have never tried that. It tells you about Actions in the Java Tutorial here.
    Look up the Java Tutorial about Listeners.Look up the EventListener interface, then find some of its subinterfaces; for a button you want ActionListener. Read the method description carefully.

    Decide which class type you want;
    Public class, usable by any app you program:If you think you might not use it outside your present class, put the ColourListener inside the class and change the "public class" declaration to "private class"

    If you have one Listener and one Listener only and you are not using it (or anything similar on more than one Component, use the anonymous inner class. Don't use anonymous inner classes for "exitListener" if you have two "exit" components (eg exitButton, exitMenuItem) because you don't want to use the same code twice.
    If you are using an IDE, you can probably train it to write an anonymous inner class for you. If you are doing it by hand, I suggest you do it in these stages. Note it won't compile in the earlier stages.

    1: Decide which listener you are adding to which Component. invoke its addWhatever method:2: Make space for it. Put your cursor in the () and push "enter" twice.3: Put an instantiaton statement in the gap, and "enter":4: Convert your interface to a class with a pair of braces:5: Put the method heading(s) inside the class body (you can copy them from the API for whichever listener you are using):6: Put the braces and "enters" in to make the method body:At this point you can compile the code, but it won't do anything.
    7: Put something more sensible than this inside the method:8: Remember your Javadoc comments
    If you implement a listener with severel methods and don't need them all, don't simply leave a blank method; write "/* empty method */". Then you won't think you have forgotten anything later on.
    If your methods inside the anonymous class use any local variables or parameters of the surrounding block (constructor or setUpGUI method), then you have to go and declare those variables or parameters "final".

    Yes, I know it sounds complicated, but it does work.

    CR

    [edit]Bit of text in wrong location moved, and some spelling corrections[/edit]
    [ April 20, 2007: Message edited by: Campbell Ritchie ]
    natasha henry
    Greenhorn

    Joined: Apr 12, 2007
    Posts: 20
    WOW- thank you for that little crash course on ActionListeners and it came just in time when i needed it most. i am going to get down to business and as soon as there is anything new i'll make sure to link you.

    T----H----A-----N-----K
    Y-----O-----U
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37874
        
      22
    You're welcome.

    Don't implement lots of all-singing all-dancing ActionListeners yet. Implement one which prints out "Don't click this button again." Then you can see it working. Then you can go on and add functionality.
    natasha henry
    Greenhorn

    Joined: Apr 12, 2007
    Posts: 20
    i think i took the route of all-singing-all-dancing ActionListeners mr. campbell. i decided to apply it to my calculate button and the thing runs but when i click the calculate button there is basically no response, no functionality. i'm wondering if its because i included conditions in the method (if statements) under which it should run? take a look



    this is where the action listener is





    oh please add this class
    so that the calculatory methods can be recognized



    [ April 21, 2007: Message edited by: natasha henry ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37874
        
      22
    Afraid I haven't got the time to look at your methods just now.
    Comment out everything in your ActionListener class and put a line in sayingThen put it back together one line at a time, and run the app after every single alteration.
    natasha henry
    Greenhorn

    Joined: Apr 12, 2007
    Posts: 20
    thats the thing-the only thing that the ActionListener recognizes is the JOptionPane or the System.out etc., which it successfully executes but nothing else
     
     
    subject: problem with parse int method
     
    Similar Threads
    how to accept strings and values in invoice textfields
    JTextArea problems in GridbagLayout
    GridBagLayout
    Need help with this code
    how to align components in GridBagLayout in JAVA swing