0

Many times I run this, there is an infinite loop problem, but I simply cannot find out what/where it is. Other times, it works fine. During the infinite error runs the program is not even running the initial part where I tell the program to display the arrayList. 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, every time I remove one, I randomly move another person/normal guy from daily planet street to another arraylist called safety land.

For example, an infinite error run might look like this:

8
8
8
8
8
8

Please help me find the problem

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") {goodpos=counter1; }
        }
        dailyPlanetStreet.add(randompos,dailyPlanetStreet.remove(goodpos));

    }



    public static void tosafety() {
        System.out.println("Got here!");
        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")) 
        {
            System.out.println("Got into the loop!");
            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[])
        {
        String[] firstname= {"a ", "b", "c", "d","e","f","g","h","i","j"};
        String[] lastname= {"k ", "l", "m", "n","o","p","q","r","s","t"};
        while (y==x){
            y=(int)(Math.random()*10);
            x=(int)(Math.random()*10);

        }



        for (int z=0; z<10; z++)
        {
            if (z==y)
                {
                    GoodGuy superMan=new GoodGuy( "This looks like a job for superman!"   , "A lot of them"   ,  true , 10   , "Superman"  , "Clark Kent"  , "plumber" , 100  ,   10,10);
                    dailyPlanetStreet.add(superMan);                    }

            else if (z==x)
                {
                    BadGuy superMan=new BadGuy( "haha"   , "Darkness"   ,  true , 10   , "Batman"  , "James Kalvin"  , "farmer" , 100  ,   10,10);
                    dailyPlanetStreet.add(superMan);
                                                                }   
            else  if (((int)(Math.random()*2)+1)==1)
                 {
                    Person superMan=new Person( firstname[(int)(Math.random()*10)]+lastname[(int)(Math.random()*10)], "teacher", 100  , 10,10);
                    dailyPlanetStreet.add(superMan); 
                 }  
            else 
                {
                    NormalGuy superMan=new NormalGuy( firstname[(int)(Math.random()*10)]+lastname[(int)(Math.random()*10)], "teacher", 100  , 10,10);
                    dailyPlanetStreet.add(superMan); 
                }
        }

        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"); tosafety();rescue();display();}
                else if ((counter==dailyPlanetStreet.size()-1) && (dailyPlanetStreet.get(dailyPlanetStreet.size()-2).name()!="Clark Kent")) {System.out.println("yo");remove(counter-1);tosafety();rescue();display();}
                else if  (dailyPlanetStreet.get(counter+1).name()=="Clark Kent") {System.out.println("yo");remove(counter-1);tosafety();rescue();display();}
                else if (dailyPlanetStreet.get(counter-1).name()=="Clark Kent") {System.out.println("yo");remove(counter+1); tosafety();rescue();display();}
                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); tosafety();rescue();display();}
                    else {System.out.println("yo");remove(counter-1);  tosafety();rescue();display();}
                } 

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


        System.out.println("hi");

        }   

} 
guness
  • 6,336
  • 7
  • 59
  • 88
  • 2
    [how to compare `String` in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – zapl Dec 07 '13 at 03:15
  • It is not a duplicate! The OP has been advised to ask another question because the topic had changed. Please read carefully when flagging questionse :-/ – Octoshape Dec 07 '13 at 11:05

1 Answers1

0

You compare String with == instead of .equals, this is a common mistake and does not work.

For example in your while loop:

if (dailyPlanetStreet.get(counter).name()=="James Kalvin")

should be changed to:

if (dailyPlanetStreet.get(counter).name().equals("James Kalvin"))

as well as all other occurrences where you compare strings.

EDIT

On the problem with the infinite loop:

Your while loop runs until .size() is smaller than 2. But in your while loop you only do something if one of the .name() values in dailyPlanetStreet is "James Kalvin". From what I see those values are generated/selected randomly. So if you happen to not get any James Kalvins you will loop forever there.

To be able to tell more about why exactly it loops forever I would have to see the constructor for BadGuy that you're using..

EDIT 2

After looking at the comment below, I see that all the if else ifclauses in the while loop start with a condition that's comparing String's with ==. Therefore none of those will return true and nothing will be done. Please change all your ....name()=="Something" to ....name().equals("Something").

Octoshape
  • 1,131
  • 8
  • 26
  • I was told offline by the OP that these two lines ensure that you'll get a James Kalvin: BadGuy superMan=new BadGuy( "haha" , "Darkness" , true , 10 , "Batman" , "James Kalvin" , "farmer" , 100 , 10,10); dailyPlanetStreet.add(superMan); – Keshav Srinivasan Dec 07 '13 at 04:33