Results 1 to 3 of 3

Thread: a small method

  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Posts
    36
    Rep Power
    0

    Default a small method

    hey,
    I've written a method that needs to return an object "Wagon".
    The method needs to pick out the wagon with the biggest amount of weight and return this wagon.
    But the problem is that its saying that "kar" is already defined.
    I know I defined it in the beginning of the method but each time when I run through the forloop and there is a bigger amount af wait in the wagon "i", I want kar to be this wagon so that in the end the wagon with the most weight is going to be returned.

    This is the code:

    Java Code:
    public Wagon zwaarsteWagon(){
        Wagon kar = new Wagon();
        double gewicht= 0.0;
        for(int i=0; i<=getAantalWagons(); i++)
            if(wagons[i] != null){
                if(wagons[i].getGewichtGoederen()>gewicht){
                    Wagon kar = wagons[i];
                    gewicht = wagons[i].getGewichtGoederen();
                    gewicht = wagons[i].getGewichtGoederen();}}
                    
        return kar;}

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Java Code:
    public Wagon zwaarsteWagon(){
        Wagon kar = new Wagon();
        double gewicht= 0.0;
        for(int i=0; i<=getAantalWagons(); i++)
            if(wagons[i] != null){
                if(wagons[i].getGewichtGoederen()>gewicht){
                    kar = wagons[i];        //             <----------- just assign
                    gewicht = wagons[i].getGewichtGoederen();
                    gewicht = wagons[i].getGewichtGoederen();}}
                    
        return kar;}
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's because of these two lines.

    Java Code:
    public Wagon zwaarsteWagon(){
        Wagon kar = new Wagon();
        ......
        for(......)
            if(....){
                if(...){
                    Wagon kar = wagons[i];
                    ....
                    .......}}
                    
        return kar;}
    You cannot use the same variable name in the same scope of it. It's a fundamental thing actually.

Similar Threads

  1. small method isn't supposed to go under 0
    By senca in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 02:23 PM
  2. small problem
    By rawan in forum Java Applets
    Replies: 2
    Last Post: 04-07-2010, 04:32 AM
  3. small code, need help
    By p0rnstar in forum New To Java
    Replies: 22
    Last Post: 01-22-2010, 02:57 PM
  4. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 07:19 AM
  5. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 01:27 PM

Posting Permissions

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