Results 1 to 12 of 12
  1. #1
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Errors while using parseInt

    I probably made a dumb mistake, but hey, that's why I'm posting on New To Java.

    Java Code:
    package practiceapplication;
    import static java.lang.Integer.parseInt;
    
    class Practiceapplication{
        
        int addition(String arguments[]){
            int sum = 0;
            
        if (arguments[0] == "+" ) 
            for(int x = 0; x < arguments.length; x++){
                arguments = new Integer.parseInt(arguments);
                sum += arguments[x]; 
         }
             return sum;
             
        if (arguments[0] == "*") {
            for(int x =0; x < arguments.length; x++){
                arguments = new Integer.parseInt(arguments);
            sum *= arguments[x];
            }
        } return sum;
        
        }
    }

    This is not the complete object, but you get the idea.
    What am I doing wrong?
    It says parseInt cannot be found.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Errors while using parseInt

    The new keyword is used in conjunction with a constructor call, to instantiate and object. Not with a method call, as you have used it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Errors while using parseInt

    And after you've corrected that, read the API for the Integer class and see what parameter the parseInt(...) method expects, and what it returns. Hint: Neither of those are a String array.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default Re: Errors while using parseInt

    ... and finally, you can't compare two Strings for equality with the == operator; use the String.equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: Errors while using parseInt

    You CAN use "==" operator when comparing two strings in Java 7 (java 1.7+)

    Aren't they arrays? Since in the main method (which I didn't include in this code), I have declared "String[] arguments"

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Errors while using parseInt

    The only dumb mistake I see you making is not listening to the people who are giving you advice.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: Errors while using parseInt

    I'm just questioning them, dumbshit.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Errors while using parseInt

    Gee name calling, now you're getting somewhere.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: Errors while using parseInt

    Why can't you just go bother someone else.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Errors while using parseInt

    Quote Originally Posted by bboyinmartin View Post
    You CAN use "==" operator when comparing two strings in Java 7 (java 1.7+)
    It's no different in 7 than in 6 or earlier.
    You should use equals() to compare objects (which is what Strings are) for equality, unless you want to know if they are the same object (ie the reference points to the same spot on the heap).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: Errors while using parseInt

    Okay, but how do I specify that I'm looking for an operator at arugments[0]?

    Thanks for actually help me, unlike the other guy.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Errors while using parseInt

    Quote Originally Posted by bboyinmartin View Post
    I'm just questioning them, dumbshit.
    Banned for a day. You may continue this discussion tomorrow, hopefully with a better attitude.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to use if/else with parseint
    By asai in forum New To Java
    Replies: 4
    Last Post: 06-20-2012, 06:43 PM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. parseInt Exception errors
    By TIMBERings in forum New To Java
    Replies: 5
    Last Post: 12-10-2009, 09:11 AM
  4. parseInt
    By trefoil in forum New To Java
    Replies: 4
    Last Post: 09-09-2009, 07:12 PM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •