Incrementing ArrayElement Parameters based on previous ELEMENTS












0















I am new to java but have been playing with array-lists and am now stuck.



I have an array list created off a class called Car with three parameters one of which is called times moved.



Main Class



public class GarageTester {

/**
* @param args the command line arguments
*/
public static void main(String args) throws IOException{
// create Bank object
Garage bashimGarage = new Garage() ;


// create Scanner object associated with input file
Scanner fileScan = new Scanner(new
File("C:\Users\jamison\Desktop\GarageData.txt")) ;

// read BankAccount data from file, create objects, and add to list

while ( fileScan.hasNextLine()) // while not eof
{

String fullText = fileScan.nextLine();
// Split the acquired string into 2 based on the whitespace
String splitText = fullText.split("\s+");
// String before whitespace split
String licensePlate = splitText[0];
// String after whitespace split
String status = splitText[1];
// create Car object
Car newCar = new Car(licensePlate, status , 0) ;
// add to list
bashimGarage.addCar( newCar ) ;

}


/*
*Calculates the number of times car was temporary moved before departure
*/
bashimGarage.carDepart();


/*
*Prints list of car license plates
* Admits or declines a car to the garage
* Prints if a car departs the Garage
* When a car departs also prints the number of times it was moved
*/
bashimGarage.moveCarInGarage();


Car Class



public class Car {

private String licensePlate; // License Plate Number
private String status ; // Status: Arivved or Departed
private int moved; /* How many times the car
got moved out of the garage
*/


public Car ( String licenseNum, String carStatus , int timesMoved)
{
licensePlate = licenseNum ;
status = carStatus ;
moved = timesMoved;
}


public String getLicenseNum()
{
return licensePlate;
}


public String getStatus()
{
return status;
}



public int getTimesMoved()
{
return moved;
}



public int setTimesMoved(int times){
moved = moved + times;
return moved;
}

}


Garage Class



public class Garage {
private ArrayList<Car> list ; // a list of BankAccount objects
public int maxCars = 10; // max number of cars allowed in garage
public int currentCars = 0; // current number of cars in garage


public Garage()
{
list = new ArrayList<Car>() ;
}


public void addCar(Car newCar)
{
list.add(newCar) ; // calls "add" method of ArrayList class
}


public void carDepart() {
for (int i = 0; i < list.size(); i++) {
Car current = list.get(i); // get next car
if (current.getStatus().equals("DEPART")) {
int pos = list.indexOf(current);

for (int j = 0; j < pos; j++) {
list.get(j).setTimesMoved(1 + current.getTimesMoved());
}

list.remove(i);
return;
}

}
}



public void moveCarInGarage()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("ARRIVE"))
{
currentCars++; //Increments current # of cars in garage

if (currentCars <= 10) // Checks if there is space in garage
{
//Prints license plate and arrival status to screen
System.out.println("Car with license plate" +
current.getLicenseNum() + " has arrived "
+ "and been moved into the garage");
}
else
{
// Prints garage is full to screen
System.out.println("The garage is full at this " +
"time so come back later");
}
}
else
{

currentCars--; // Decrements current # of cars in garage
/*
Prints license plate, departure status,
and number of times moved to screen
*/
System.out.println("Car with license plate" +
current.getLicenseNum() + " is departing and has been moved "
+ current.getTimesMoved() + " times" );

}
}
}


I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"



I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"



The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.



I came up with this but it does not seem to work as when I call the 2nd method it always says 0 for times moved.



public void carDepart()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("DEPART"))
{
int pos = list.indexOf(i);

for ( int j = 0 ; pos < j ; j++)
{
current.setTimesMoved(1 + current.getTimesMoved());
}

list.remove(i);
return;
}

}
}


Second method



    public void moveCarInGarage()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("ARRIVE"))
{
currentCars++;

if (currentCars <= 10)
{
System.out.println("Car with license plate" +
current.getLicenseNum() + " has been moved into the garage");
}
else
{
System.out.println("The garage is full at this " +
"time so come back later");
}
}
else
{
currentCars--;
System.out.println("Car with license plate" +
current.getLicenseNum() + " is departing and has been moved "
+ current.getTimesMoved() + " times" );
}
}


}










share|improve this question





























    0















    I am new to java but have been playing with array-lists and am now stuck.



    I have an array list created off a class called Car with three parameters one of which is called times moved.



    Main Class



    public class GarageTester {

    /**
    * @param args the command line arguments
    */
    public static void main(String args) throws IOException{
    // create Bank object
    Garage bashimGarage = new Garage() ;


    // create Scanner object associated with input file
    Scanner fileScan = new Scanner(new
    File("C:\Users\jamison\Desktop\GarageData.txt")) ;

    // read BankAccount data from file, create objects, and add to list

    while ( fileScan.hasNextLine()) // while not eof
    {

    String fullText = fileScan.nextLine();
    // Split the acquired string into 2 based on the whitespace
    String splitText = fullText.split("\s+");
    // String before whitespace split
    String licensePlate = splitText[0];
    // String after whitespace split
    String status = splitText[1];
    // create Car object
    Car newCar = new Car(licensePlate, status , 0) ;
    // add to list
    bashimGarage.addCar( newCar ) ;

    }


    /*
    *Calculates the number of times car was temporary moved before departure
    */
    bashimGarage.carDepart();


    /*
    *Prints list of car license plates
    * Admits or declines a car to the garage
    * Prints if a car departs the Garage
    * When a car departs also prints the number of times it was moved
    */
    bashimGarage.moveCarInGarage();


    Car Class



    public class Car {

    private String licensePlate; // License Plate Number
    private String status ; // Status: Arivved or Departed
    private int moved; /* How many times the car
    got moved out of the garage
    */


    public Car ( String licenseNum, String carStatus , int timesMoved)
    {
    licensePlate = licenseNum ;
    status = carStatus ;
    moved = timesMoved;
    }


    public String getLicenseNum()
    {
    return licensePlate;
    }


    public String getStatus()
    {
    return status;
    }



    public int getTimesMoved()
    {
    return moved;
    }



    public int setTimesMoved(int times){
    moved = moved + times;
    return moved;
    }

    }


    Garage Class



    public class Garage {
    private ArrayList<Car> list ; // a list of BankAccount objects
    public int maxCars = 10; // max number of cars allowed in garage
    public int currentCars = 0; // current number of cars in garage


    public Garage()
    {
    list = new ArrayList<Car>() ;
    }


    public void addCar(Car newCar)
    {
    list.add(newCar) ; // calls "add" method of ArrayList class
    }


    public void carDepart() {
    for (int i = 0; i < list.size(); i++) {
    Car current = list.get(i); // get next car
    if (current.getStatus().equals("DEPART")) {
    int pos = list.indexOf(current);

    for (int j = 0; j < pos; j++) {
    list.get(j).setTimesMoved(1 + current.getTimesMoved());
    }

    list.remove(i);
    return;
    }

    }
    }



    public void moveCarInGarage()
    {
    for ( int i = 0 ; i < list.size() ; i++ )
    {
    Car current = list.get( i ) ; // get next car
    if (current.getStatus().equals("ARRIVE"))
    {
    currentCars++; //Increments current # of cars in garage

    if (currentCars <= 10) // Checks if there is space in garage
    {
    //Prints license plate and arrival status to screen
    System.out.println("Car with license plate" +
    current.getLicenseNum() + " has arrived "
    + "and been moved into the garage");
    }
    else
    {
    // Prints garage is full to screen
    System.out.println("The garage is full at this " +
    "time so come back later");
    }
    }
    else
    {

    currentCars--; // Decrements current # of cars in garage
    /*
    Prints license plate, departure status,
    and number of times moved to screen
    */
    System.out.println("Car with license plate" +
    current.getLicenseNum() + " is departing and has been moved "
    + current.getTimesMoved() + " times" );

    }
    }
    }


    I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"



    I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"



    The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.



    I came up with this but it does not seem to work as when I call the 2nd method it always says 0 for times moved.



    public void carDepart()
    {
    for ( int i = 0 ; i < list.size() ; i++ )
    {
    Car current = list.get( i ) ; // get next car
    if (current.getStatus().equals("DEPART"))
    {
    int pos = list.indexOf(i);

    for ( int j = 0 ; pos < j ; j++)
    {
    current.setTimesMoved(1 + current.getTimesMoved());
    }

    list.remove(i);
    return;
    }

    }
    }


    Second method



        public void moveCarInGarage()
    {
    for ( int i = 0 ; i < list.size() ; i++ )
    {
    Car current = list.get( i ) ; // get next car
    if (current.getStatus().equals("ARRIVE"))
    {
    currentCars++;

    if (currentCars <= 10)
    {
    System.out.println("Car with license plate" +
    current.getLicenseNum() + " has been moved into the garage");
    }
    else
    {
    System.out.println("The garage is full at this " +
    "time so come back later");
    }
    }
    else
    {
    currentCars--;
    System.out.println("Car with license plate" +
    current.getLicenseNum() + " is departing and has been moved "
    + current.getTimesMoved() + " times" );
    }
    }


    }










    share|improve this question



























      0












      0








      0


      2






      I am new to java but have been playing with array-lists and am now stuck.



      I have an array list created off a class called Car with three parameters one of which is called times moved.



      Main Class



      public class GarageTester {

      /**
      * @param args the command line arguments
      */
      public static void main(String args) throws IOException{
      // create Bank object
      Garage bashimGarage = new Garage() ;


      // create Scanner object associated with input file
      Scanner fileScan = new Scanner(new
      File("C:\Users\jamison\Desktop\GarageData.txt")) ;

      // read BankAccount data from file, create objects, and add to list

      while ( fileScan.hasNextLine()) // while not eof
      {

      String fullText = fileScan.nextLine();
      // Split the acquired string into 2 based on the whitespace
      String splitText = fullText.split("\s+");
      // String before whitespace split
      String licensePlate = splitText[0];
      // String after whitespace split
      String status = splitText[1];
      // create Car object
      Car newCar = new Car(licensePlate, status , 0) ;
      // add to list
      bashimGarage.addCar( newCar ) ;

      }


      /*
      *Calculates the number of times car was temporary moved before departure
      */
      bashimGarage.carDepart();


      /*
      *Prints list of car license plates
      * Admits or declines a car to the garage
      * Prints if a car departs the Garage
      * When a car departs also prints the number of times it was moved
      */
      bashimGarage.moveCarInGarage();


      Car Class



      public class Car {

      private String licensePlate; // License Plate Number
      private String status ; // Status: Arivved or Departed
      private int moved; /* How many times the car
      got moved out of the garage
      */


      public Car ( String licenseNum, String carStatus , int timesMoved)
      {
      licensePlate = licenseNum ;
      status = carStatus ;
      moved = timesMoved;
      }


      public String getLicenseNum()
      {
      return licensePlate;
      }


      public String getStatus()
      {
      return status;
      }



      public int getTimesMoved()
      {
      return moved;
      }



      public int setTimesMoved(int times){
      moved = moved + times;
      return moved;
      }

      }


      Garage Class



      public class Garage {
      private ArrayList<Car> list ; // a list of BankAccount objects
      public int maxCars = 10; // max number of cars allowed in garage
      public int currentCars = 0; // current number of cars in garage


      public Garage()
      {
      list = new ArrayList<Car>() ;
      }


      public void addCar(Car newCar)
      {
      list.add(newCar) ; // calls "add" method of ArrayList class
      }


      public void carDepart() {
      for (int i = 0; i < list.size(); i++) {
      Car current = list.get(i); // get next car
      if (current.getStatus().equals("DEPART")) {
      int pos = list.indexOf(current);

      for (int j = 0; j < pos; j++) {
      list.get(j).setTimesMoved(1 + current.getTimesMoved());
      }

      list.remove(i);
      return;
      }

      }
      }



      public void moveCarInGarage()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("ARRIVE"))
      {
      currentCars++; //Increments current # of cars in garage

      if (currentCars <= 10) // Checks if there is space in garage
      {
      //Prints license plate and arrival status to screen
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " has arrived "
      + "and been moved into the garage");
      }
      else
      {
      // Prints garage is full to screen
      System.out.println("The garage is full at this " +
      "time so come back later");
      }
      }
      else
      {

      currentCars--; // Decrements current # of cars in garage
      /*
      Prints license plate, departure status,
      and number of times moved to screen
      */
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " is departing and has been moved "
      + current.getTimesMoved() + " times" );

      }
      }
      }


      I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"



      I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"



      The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.



      I came up with this but it does not seem to work as when I call the 2nd method it always says 0 for times moved.



      public void carDepart()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("DEPART"))
      {
      int pos = list.indexOf(i);

      for ( int j = 0 ; pos < j ; j++)
      {
      current.setTimesMoved(1 + current.getTimesMoved());
      }

      list.remove(i);
      return;
      }

      }
      }


      Second method



          public void moveCarInGarage()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("ARRIVE"))
      {
      currentCars++;

      if (currentCars <= 10)
      {
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " has been moved into the garage");
      }
      else
      {
      System.out.println("The garage is full at this " +
      "time so come back later");
      }
      }
      else
      {
      currentCars--;
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " is departing and has been moved "
      + current.getTimesMoved() + " times" );
      }
      }


      }










      share|improve this question
















      I am new to java but have been playing with array-lists and am now stuck.



      I have an array list created off a class called Car with three parameters one of which is called times moved.



      Main Class



      public class GarageTester {

      /**
      * @param args the command line arguments
      */
      public static void main(String args) throws IOException{
      // create Bank object
      Garage bashimGarage = new Garage() ;


      // create Scanner object associated with input file
      Scanner fileScan = new Scanner(new
      File("C:\Users\jamison\Desktop\GarageData.txt")) ;

      // read BankAccount data from file, create objects, and add to list

      while ( fileScan.hasNextLine()) // while not eof
      {

      String fullText = fileScan.nextLine();
      // Split the acquired string into 2 based on the whitespace
      String splitText = fullText.split("\s+");
      // String before whitespace split
      String licensePlate = splitText[0];
      // String after whitespace split
      String status = splitText[1];
      // create Car object
      Car newCar = new Car(licensePlate, status , 0) ;
      // add to list
      bashimGarage.addCar( newCar ) ;

      }


      /*
      *Calculates the number of times car was temporary moved before departure
      */
      bashimGarage.carDepart();


      /*
      *Prints list of car license plates
      * Admits or declines a car to the garage
      * Prints if a car departs the Garage
      * When a car departs also prints the number of times it was moved
      */
      bashimGarage.moveCarInGarage();


      Car Class



      public class Car {

      private String licensePlate; // License Plate Number
      private String status ; // Status: Arivved or Departed
      private int moved; /* How many times the car
      got moved out of the garage
      */


      public Car ( String licenseNum, String carStatus , int timesMoved)
      {
      licensePlate = licenseNum ;
      status = carStatus ;
      moved = timesMoved;
      }


      public String getLicenseNum()
      {
      return licensePlate;
      }


      public String getStatus()
      {
      return status;
      }



      public int getTimesMoved()
      {
      return moved;
      }



      public int setTimesMoved(int times){
      moved = moved + times;
      return moved;
      }

      }


      Garage Class



      public class Garage {
      private ArrayList<Car> list ; // a list of BankAccount objects
      public int maxCars = 10; // max number of cars allowed in garage
      public int currentCars = 0; // current number of cars in garage


      public Garage()
      {
      list = new ArrayList<Car>() ;
      }


      public void addCar(Car newCar)
      {
      list.add(newCar) ; // calls "add" method of ArrayList class
      }


      public void carDepart() {
      for (int i = 0; i < list.size(); i++) {
      Car current = list.get(i); // get next car
      if (current.getStatus().equals("DEPART")) {
      int pos = list.indexOf(current);

      for (int j = 0; j < pos; j++) {
      list.get(j).setTimesMoved(1 + current.getTimesMoved());
      }

      list.remove(i);
      return;
      }

      }
      }



      public void moveCarInGarage()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("ARRIVE"))
      {
      currentCars++; //Increments current # of cars in garage

      if (currentCars <= 10) // Checks if there is space in garage
      {
      //Prints license plate and arrival status to screen
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " has arrived "
      + "and been moved into the garage");
      }
      else
      {
      // Prints garage is full to screen
      System.out.println("The garage is full at this " +
      "time so come back later");
      }
      }
      else
      {

      currentCars--; // Decrements current # of cars in garage
      /*
      Prints license plate, departure status,
      and number of times moved to screen
      */
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " is departing and has been moved "
      + current.getTimesMoved() + " times" );

      }
      }
      }


      I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"



      I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"



      The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.



      I came up with this but it does not seem to work as when I call the 2nd method it always says 0 for times moved.



      public void carDepart()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("DEPART"))
      {
      int pos = list.indexOf(i);

      for ( int j = 0 ; pos < j ; j++)
      {
      current.setTimesMoved(1 + current.getTimesMoved());
      }

      list.remove(i);
      return;
      }

      }
      }


      Second method



          public void moveCarInGarage()
      {
      for ( int i = 0 ; i < list.size() ; i++ )
      {
      Car current = list.get( i ) ; // get next car
      if (current.getStatus().equals("ARRIVE"))
      {
      currentCars++;

      if (currentCars <= 10)
      {
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " has been moved into the garage");
      }
      else
      {
      System.out.println("The garage is full at this " +
      "time so come back later");
      }
      }
      else
      {
      currentCars--;
      System.out.println("Car with license plate" +
      current.getLicenseNum() + " is departing and has been moved "
      + current.getTimesMoved() + " times" );
      }
      }


      }







      java for-loop if-statement arraylist






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 4:53







      Arthur

















      asked Jan 20 at 3:57









      ArthurArthur

      176




      176
























          2 Answers
          2






          active

          oldest

          votes


















          0














          First method, line 8.
          Why are you trying to extract pos when you already have it in i. int pos = list.indexOf(i); will give -1.



          Also, in this loop



          for ( int j = 0 ; pos < j ; j++)
          {
          current.setTimesMoved(1 + current.getTimesMoved());
          }


          You are always pointing to the same element in the arraylist.



          Instead, you may want to program it like the following:



          for ( int j = 0; j<i; j++)
          {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
          }





          share|improve this answer


























          • My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

            – Arthur
            Jan 20 at 4:25











          • I'll add an edit. Gimme 1 min

            – Aditya Gupta
            Jan 20 at 4:29











          • Oh. So you want to increment the previoys cars. I'll modify.

            – Aditya Gupta
            Jan 20 at 4:32











          • By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

            – Aditya Gupta
            Jan 20 at 4:45











          • I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

            – Arthur
            Jan 20 at 4:47



















          0














          You had below issues in your method,




          • First you were getting a index of an integer instead of the object.

          • Your inner for loop condition was wrong.

          • You were always increment the values of the current object instead of
            the previous ones.


          I have corrected them in your method. Use below one,



          public void carDepart() {
          for (int i = 0; i < list.size(); i++) {
          Car current = list.get(i); // get next car
          if (current.getStatus().equals("DEPART")) {
          /* You can remove below line and replace pos with i in your inner loop.
          Since the current object position will be same as i */
          int pos = list.indexOf(current);

          for (int j = 0; j < pos; j++) {
          list.get(j).setTimesMoved(1 + current.getTimesMoved());
          }

          list.remove(i);
          return;
          }

          }
          }





          share|improve this answer


























          • When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

            – Arthur
            Jan 20 at 4:34











          • I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

            – Arthur
            Jan 20 at 4:46











          • Can you show me your main method also?

            – Sand
            Jan 20 at 4:49











          • Main method added

            – Arthur
            Jan 20 at 4:53











          • Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

            – Sand
            Jan 20 at 5:05











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273457%2fincrementing-arrayelement-parameters-based-on-previous-elements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First method, line 8.
          Why are you trying to extract pos when you already have it in i. int pos = list.indexOf(i); will give -1.



          Also, in this loop



          for ( int j = 0 ; pos < j ; j++)
          {
          current.setTimesMoved(1 + current.getTimesMoved());
          }


          You are always pointing to the same element in the arraylist.



          Instead, you may want to program it like the following:



          for ( int j = 0; j<i; j++)
          {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
          }





          share|improve this answer


























          • My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

            – Arthur
            Jan 20 at 4:25











          • I'll add an edit. Gimme 1 min

            – Aditya Gupta
            Jan 20 at 4:29











          • Oh. So you want to increment the previoys cars. I'll modify.

            – Aditya Gupta
            Jan 20 at 4:32











          • By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

            – Aditya Gupta
            Jan 20 at 4:45











          • I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

            – Arthur
            Jan 20 at 4:47
















          0














          First method, line 8.
          Why are you trying to extract pos when you already have it in i. int pos = list.indexOf(i); will give -1.



          Also, in this loop



          for ( int j = 0 ; pos < j ; j++)
          {
          current.setTimesMoved(1 + current.getTimesMoved());
          }


          You are always pointing to the same element in the arraylist.



          Instead, you may want to program it like the following:



          for ( int j = 0; j<i; j++)
          {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
          }





          share|improve this answer


























          • My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

            – Arthur
            Jan 20 at 4:25











          • I'll add an edit. Gimme 1 min

            – Aditya Gupta
            Jan 20 at 4:29











          • Oh. So you want to increment the previoys cars. I'll modify.

            – Aditya Gupta
            Jan 20 at 4:32











          • By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

            – Aditya Gupta
            Jan 20 at 4:45











          • I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

            – Arthur
            Jan 20 at 4:47














          0












          0








          0







          First method, line 8.
          Why are you trying to extract pos when you already have it in i. int pos = list.indexOf(i); will give -1.



          Also, in this loop



          for ( int j = 0 ; pos < j ; j++)
          {
          current.setTimesMoved(1 + current.getTimesMoved());
          }


          You are always pointing to the same element in the arraylist.



          Instead, you may want to program it like the following:



          for ( int j = 0; j<i; j++)
          {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
          }





          share|improve this answer















          First method, line 8.
          Why are you trying to extract pos when you already have it in i. int pos = list.indexOf(i); will give -1.



          Also, in this loop



          for ( int j = 0 ; pos < j ; j++)
          {
          current.setTimesMoved(1 + current.getTimesMoved());
          }


          You are always pointing to the same element in the arraylist.



          Instead, you may want to program it like the following:



          for ( int j = 0; j<i; j++)
          {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 4:31

























          answered Jan 20 at 4:22









          Aditya GuptaAditya Gupta

          7851821




          7851821













          • My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

            – Arthur
            Jan 20 at 4:25











          • I'll add an edit. Gimme 1 min

            – Aditya Gupta
            Jan 20 at 4:29











          • Oh. So you want to increment the previoys cars. I'll modify.

            – Aditya Gupta
            Jan 20 at 4:32











          • By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

            – Aditya Gupta
            Jan 20 at 4:45











          • I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

            – Arthur
            Jan 20 at 4:47



















          • My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

            – Arthur
            Jan 20 at 4:25











          • I'll add an edit. Gimme 1 min

            – Aditya Gupta
            Jan 20 at 4:29











          • Oh. So you want to increment the previoys cars. I'll modify.

            – Aditya Gupta
            Jan 20 at 4:32











          • By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

            – Aditya Gupta
            Jan 20 at 4:45











          • I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

            – Arthur
            Jan 20 at 4:47

















          My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

          – Arthur
          Jan 20 at 4:25





          My logic was that when it first hits an element with the status depart it gets its index and then scans all the elements previous to that index and increments timesMoved by 1. I am not 100% sure what you mean by extract post when you already have it as I never tried to get the index before? Sorry if I sound abit lost

          – Arthur
          Jan 20 at 4:25













          I'll add an edit. Gimme 1 min

          – Aditya Gupta
          Jan 20 at 4:29





          I'll add an edit. Gimme 1 min

          – Aditya Gupta
          Jan 20 at 4:29













          Oh. So you want to increment the previoys cars. I'll modify.

          – Aditya Gupta
          Jan 20 at 4:32





          Oh. So you want to increment the previoys cars. I'll modify.

          – Aditya Gupta
          Jan 20 at 4:32













          By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

          – Aditya Gupta
          Jan 20 at 4:45





          By extracting I meant that since you are iterating using loop variable i, wont i be the current index. Ou dont need to explicitly extract position of current index.

          – Aditya Gupta
          Jan 20 at 4:45













          I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

          – Arthur
          Jan 20 at 4:47





          I understand now Sand provided a good example but even with the edited code it still gives 0 for some reason

          – Arthur
          Jan 20 at 4:47













          0














          You had below issues in your method,




          • First you were getting a index of an integer instead of the object.

          • Your inner for loop condition was wrong.

          • You were always increment the values of the current object instead of
            the previous ones.


          I have corrected them in your method. Use below one,



          public void carDepart() {
          for (int i = 0; i < list.size(); i++) {
          Car current = list.get(i); // get next car
          if (current.getStatus().equals("DEPART")) {
          /* You can remove below line and replace pos with i in your inner loop.
          Since the current object position will be same as i */
          int pos = list.indexOf(current);

          for (int j = 0; j < pos; j++) {
          list.get(j).setTimesMoved(1 + current.getTimesMoved());
          }

          list.remove(i);
          return;
          }

          }
          }





          share|improve this answer


























          • When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

            – Arthur
            Jan 20 at 4:34











          • I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

            – Arthur
            Jan 20 at 4:46











          • Can you show me your main method also?

            – Sand
            Jan 20 at 4:49











          • Main method added

            – Arthur
            Jan 20 at 4:53











          • Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

            – Sand
            Jan 20 at 5:05
















          0














          You had below issues in your method,




          • First you were getting a index of an integer instead of the object.

          • Your inner for loop condition was wrong.

          • You were always increment the values of the current object instead of
            the previous ones.


          I have corrected them in your method. Use below one,



          public void carDepart() {
          for (int i = 0; i < list.size(); i++) {
          Car current = list.get(i); // get next car
          if (current.getStatus().equals("DEPART")) {
          /* You can remove below line and replace pos with i in your inner loop.
          Since the current object position will be same as i */
          int pos = list.indexOf(current);

          for (int j = 0; j < pos; j++) {
          list.get(j).setTimesMoved(1 + current.getTimesMoved());
          }

          list.remove(i);
          return;
          }

          }
          }





          share|improve this answer


























          • When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

            – Arthur
            Jan 20 at 4:34











          • I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

            – Arthur
            Jan 20 at 4:46











          • Can you show me your main method also?

            – Sand
            Jan 20 at 4:49











          • Main method added

            – Arthur
            Jan 20 at 4:53











          • Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

            – Sand
            Jan 20 at 5:05














          0












          0








          0







          You had below issues in your method,




          • First you were getting a index of an integer instead of the object.

          • Your inner for loop condition was wrong.

          • You were always increment the values of the current object instead of
            the previous ones.


          I have corrected them in your method. Use below one,



          public void carDepart() {
          for (int i = 0; i < list.size(); i++) {
          Car current = list.get(i); // get next car
          if (current.getStatus().equals("DEPART")) {
          /* You can remove below line and replace pos with i in your inner loop.
          Since the current object position will be same as i */
          int pos = list.indexOf(current);

          for (int j = 0; j < pos; j++) {
          list.get(j).setTimesMoved(1 + current.getTimesMoved());
          }

          list.remove(i);
          return;
          }

          }
          }





          share|improve this answer















          You had below issues in your method,




          • First you were getting a index of an integer instead of the object.

          • Your inner for loop condition was wrong.

          • You were always increment the values of the current object instead of
            the previous ones.


          I have corrected them in your method. Use below one,



          public void carDepart() {
          for (int i = 0; i < list.size(); i++) {
          Car current = list.get(i); // get next car
          if (current.getStatus().equals("DEPART")) {
          /* You can remove below line and replace pos with i in your inner loop.
          Since the current object position will be same as i */
          int pos = list.indexOf(current);

          for (int j = 0; j < pos; j++) {
          list.get(j).setTimesMoved(1 + current.getTimesMoved());
          }

          list.remove(i);
          return;
          }

          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 4:38

























          answered Jan 20 at 4:31









          SandSand

          1,6522521




          1,6522521













          • When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

            – Arthur
            Jan 20 at 4:34











          • I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

            – Arthur
            Jan 20 at 4:46











          • Can you show me your main method also?

            – Sand
            Jan 20 at 4:49











          • Main method added

            – Arthur
            Jan 20 at 4:53











          • Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

            – Sand
            Jan 20 at 5:05



















          • When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

            – Arthur
            Jan 20 at 4:34











          • I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

            – Arthur
            Jan 20 at 4:46











          • Can you show me your main method also?

            – Sand
            Jan 20 at 4:49











          • Main method added

            – Arthur
            Jan 20 at 4:53











          • Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

            – Sand
            Jan 20 at 5:05

















          When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

          – Arthur
          Jan 20 at 4:34





          When running the program it still says moved 0 times after running the 2nd method. Let me edit the code to include more information

          – Arthur
          Jan 20 at 4:34













          I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

          – Arthur
          Jan 20 at 4:46





          I added more code as I am still getting 0 and am guessing it might be a bug somewhere else now

          – Arthur
          Jan 20 at 4:46













          Can you show me your main method also?

          – Sand
          Jan 20 at 4:49





          Can you show me your main method also?

          – Sand
          Jan 20 at 4:49













          Main method added

          – Arthur
          Jan 20 at 4:53





          Main method added

          – Arthur
          Jan 20 at 4:53













          Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

          – Sand
          Jan 20 at 5:05





          Well its working correctly. If you replace the first print inside moveCarInGarage method with this System.out.println("Car with license plate" + current.getLicenseNum() + " has moved " + current.getTimesMoved() + " times"); you should see its printing the times correctly. Maybe you need to think about your conditions?

          – Sand
          Jan 20 at 5:05


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273457%2fincrementing-arrayelement-parameters-based-on-previous-elements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage