1

So I'm having an infinite loop problem, but I simply cannot find out what/where it is. I have an arrayList called dailyPlanetStreet filled with objects of either class Goodguy Badguy Person or NormalGuy. In my removing mechanism below, if there is a normalguy/person on one side of the bad guy, i remove that person/normalguy. If there is a normalguy/person on both sides, i randomly remove either one. Also, everytime I remove one, I randomly move another person/normal guy from dailyplanetstreet to another arraylist called safety land.

Here is my current output:

normal guy
normal guy
person
badguy
normal guy
goodguy
normal guy
person
person
person
yo


normal guy
normal guy
person
badguy
goodguy
normal guy
person
person
person

Code:

import java.util.ArrayList;
public class GothamLikeAdventureTown 
    {
        static int y=5;
        static int x=3;

        static ArrayList<Person> dailyPlanetStreet=new ArrayList<Person>();
        static ArrayList<Person> safetyLand=new ArrayList<Person>();
        static ArrayList<Person> unfortunatelyFatallyWoundedPeople=new ArrayList<Person>();

        public static void rescue() {
            int goodpos=0;
            int randompos=(int)(Math.random()*dailyPlanetStreet.size());
            for (int counter1=0; counter1<dailyPlanetStreet.size(); counter1++){
                if (dailyPlanetStreet.get(counter1).name()!="Kent Clark") {counter1=goodpos;}
            }
            dailyPlanetStreet.add(randompos,dailyPlanetStreet.remove(goodpos));

        }
        public static void tosafety() {
            int outer=12;
            while (outer!=12){
            int randompos=(int)(Math.random()*dailyPlanetStreet.size());
            if ((dailyPlanetStreet.get(randompos).name()!="Clark Kent") &&  (dailyPlanetStreet.get(randompos).name()!="James Kalvin")) 
            {
                safetyLand.add(dailyPlanetStreet.remove(randompos));
                outer=12;
            } }
        }
        public static void  remove(int deletepos) {
            unfortunatelyFatallyWoundedPeople.add(dailyPlanetStreet.remove(deletepos)); 
        }

        public static void display(){
            System.out.println();
            System.out.println();
            for (int z=0; z<dailyPlanetStreet.size(); z++)
            {
                if (dailyPlanetStreet.get(z) instanceof BadGuy) System.out.println("badguy");
                else if (dailyPlanetStreet.get(z) instanceof GoodGuy) System.out.println("goodguy");
                else if (dailyPlanetStreet.get(z) instanceof NormalGuy) System.out.println("normal guy");
                else if (dailyPlanetStreet.get(z) instanceof Person) System.out.println("person");
            } 
        }

        public static void main(String args[])
            //code for putting in value of arraylist....

            display();


            while (dailyPlanetStreet.size()>2){
            for (int counter=0; counter<dailyPlanetStreet.size(); counter++){
                if (dailyPlanetStreet.get(counter).name()=="James Kalvin") 
                {
                    if ((counter==0) && (dailyPlanetStreet.get(1).name()=="Clark Kent")) {}
                    else if ((counter==0) && (dailyPlanetStreet.get(1).name()!="Clark Kent")) {System.out.println("yo"); remove(counter+1);display();rescue();} 
                    else if ((counter==dailyPlanetStreet.size()-1) && (dailyPlanetStreet.get(dailyPlanetStreet.size()-2).name()=="Clark Kent")) {System.out.println("yo");display(); tosafety();rescue();}
                    else if ((counter==dailyPlanetStreet.size()-1) && (dailyPlanetStreet.get(dailyPlanetStreet.size()-2).name()!="Clark Kent")) {System.out.println("yo");remove(counter-1);display();tosafety();rescue();}
                    else if  (dailyPlanetStreet.get(counter+1).name()=="Clark Kent") {System.out.println("yo");remove(counter-1);display();tosafety();rescue();}
                    else if (dailyPlanetStreet.get(counter-1).name()=="Clark Kent") {System.out.println("yo");remove(counter+1);display(); tosafety();rescue();}
                    else if ((dailyPlanetStreet.get(counter-1).name()!="Clark Kent")&& (dailyPlanetStreet.get(counter+1).name()!="Clark Kent")) 
                    {
                        if (((int)(Math.random()*2))==1) {System.out.println("yo");remove(counter+1); display();tosafety();rescue();}
                        else {System.out.println("yo");remove(counter-1); display(); tosafety();rescue();}
                    } 

                    }
                }
            System.out.println(dailyPlanetStreet.size());
            }


            System.out.println("hi");

            }   

    } 
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • In the future, you should tag the language 1) for more exposure 2) So others know what language you're working with and 3) syntax highlighting. I'll do it for you this time. – Paul Samsotha Dec 06 '13 at 05:21
  • I understand sir; this is my first post, so I will be sure to address those things in future posts. – user3072997 Dec 06 '13 at 05:28

1 Answers1

1

One problem I can see is that you have the following method:

public static void rescue() {
  int goodpos=0;
  int randompos=(int)(Math.random()*dailyPlanetStreet.size());

  for (int counter1=0; counter1<dailyPlanetStreet.size(); counter1++){
    if (dailyPlanetStreet.get(counter1).name()!="Kent Clark") {counter1=goodpos;}
  }

  dailyPlanetStreet.add(randompos,dailyPlanetStreet.remove(goodpos));
}

In the above you're setting counter1 back to zero every time the name isn't "Kent Clark". You probably intended to use goodpos = counter1; inside the if clause. Also, don't use == or != to compare String values, rather use .equals().

Thus the above should be:

public static void rescue() {
  int goodPos = 0;
  int randomPos = (int)(Math.random() * dailyPlanetStreet.size());

  for (int counter1 = 0; counter1 < dailyPlanetStreet.size(); counter1++) {
    if (!"Kent Clark".equals(dailyPlanetStreet.get(counter1).name())) {
      goodPos = counter1;
    }
  }

  dailyPlanetStreet.add(randomPos, dailyPlanetStreet.remove(goodPos));
}

This finds the last non-Superman person on the street and moves it to a random position. If you wanted to find the first, you should use break to get out of the loop.

Also, since you're constantly checking whether an object's name is Clark Kent, use a helper method to DRY (Don't Repeat Yourself) up your code and improve readability:

private boolean isClarkKent(Person person) {
  return "Kent Clark".equals(person.getName());
}
t0mppa
  • 3,983
  • 5
  • 37
  • 48
  • I fixed the errors you mentioned in my program, but now there are some times when the program runs correctly and other times where the output is just like this: 8 8 8 8 8 8 8 8....(infinite loop); I don't understand when there is this infinite loop problem, and why the program doesn't even display the arraylist, as I have told it to. – user3072997 Dec 06 '13 at 06:53
  • None of the persons on the street is called James Kalvin, so the size of `dailyPlanetStreet` is printed after the `for` loop with lots of `if` clauses in `main` method. Since the size of `dailyPlanetStreet` never goes down, the `while` around the `for` is an endless loop. – t0mppa Dec 06 '13 at 07:01
  • I named the object in the arrayList that is an instance of BadGuy James Kalvin. So, it doesn't make sense to me how that could be the case. Moreover, the first thing that I tell the program to output is the display method, which displays the objects of the arrayList. The program doesn't output that, only an infinite loop of an integer (i.e 8888 9999 6666). – user3072997 Dec 06 '13 at 07:13
  • How about you make a new question (since this is a different issue and your original question doesn't describe it anymore) and add an example with [ideone.com](http://ideone.com/) into it, so we can see what exactly you're doing in the case where it breaks down? – t0mppa Dec 06 '13 at 07:25