Results 1 to 6 of 6
  1. #1
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default One class never exits, yet I can't find an infinite loop anywhere that's causing it!

    Hello, everyone. I'm working on a simple gradebook program right now, and I've run into an issue I can't crack. Here's the class that causes it when utilized:

    Java Code:
    public static void insertStudent(ArrayList<Student> oneClass, String nameAfterInsert, String insertedName, int qz1, int qz2, int qz3, int qz4, int qz5)
      {
        
        for(int i = 0; i < oneClass.size(); i++)
        {
      
          if(oneClass.get(i).getName().equalsIgnoreCase(nameAfterInsert))
          {
            
            oneClass.add(i, new Student(insertedName, qz1, qz2, qz3, qz4, qz5));
            
          }
        }
      }
    The line that it traces to (line 100 in my code) is line 10 here, which causes a long wait and eventual timeout of the program. Here's the student class it references:

    Java Code:
    public class Student
    {
      
      
      
      String studentName;
      
      int[] quizzes = new int[5];
      
      
      
      public Student(String name, int qz1, int qz2, int qz3, int qz4, int qz5)
      {
        
        studentName = name;
        
        quizzes[0] = qz1;
        quizzes[1] = qz2;
        quizzes[2] = qz3;
        quizzes[3] = qz4;
        quizzes[4] = qz5;
        
      }
      
      
      
      public String getName()
      {
        
        return studentName;
        
      }
      
      
      
      public void setName(String newName)
      {
        
        studentName = newName;
        
      }
      
      
      
      public int getQuiz(int quizNumber)
      {
        
        return quizzes[quizNumber - 1];
        
      }
      
      
      
      public void setQuiz(int quizNumber, int newScore)
      {
        
        quizzes[quizNumber - 1] = newScore;
        
      }
      
      
      
      public String toString()
      {
        
        String scoreList = "";
        
        String nameColon = (studentName + ": ");
        
        for(int i = 0; i < quizzes.length; i++)
        {
          
          scoreList += (quizzes[i] + "   ");
          
        }
        
        return nameColon + scoreList;
        
      }
    }
    Any ideas?

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

    Default Re: One class never exits, yet I can't find an infinite loop anywhere that's causing

    1. Please go through the coding conventions and learn the correct amount of vertical whitespace to use: Code Conventions for the Java Programming Language: Contents

    Quote Originally Posted by ssrock64 View Post
    ... and eventual timeout of the program.
    2. How does a Java program timeout?

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

  3. #3
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: One class never exits, yet I can't find an infinite loop anywhere that's causing

    Sorry, I wasn't clear on the timeout. I have a runtime exit command inbuilt into DrJava that I haven't changed from its out-of-the-box form. If no progress has been made, I believe the max loop time is something like five seconds.

    As for conventions, I'm still learning.

  4. #4
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: One class never exits, yet I can't find an infinite loop anywhere that's causing

    Any ideas, anyone?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: One class never exits, yet I can't find an infinite loop anywhere that's causing

    It looks to be inserting the Student *before* the nameAfterInsert Student. Then when the for loop increments i by 1, it again reaches the same nameAfterInsert, and again the code inserts the new Student before the nameAfterInsert, and the process repeats itself ad infiinitum.

    Solution: use the for loop *just* to find the index of the nameAfterInsert Student. You may break out of the for loop here. Then after the for loop code, insert your new Student. Also consider initializing the indexToPlaceStudent to -1 before the loop, and to set it to i + 1 inside of the loop. This way you can check after the loop if the nameAfterInsert was ever found. If not indexToPlaceStudent will still be -1.

  6. #6
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: One class never exits, yet I can't find an infinite loop anywhere that's causing

    Thanks a lot! I would've responded earlier, but I got caught up finishing a book that I was supposed to have read two chapters of. Oops.

    I solved it by incrementing i (within the loop) the first time it found the matching name, which isn't the most beautiful way to do things but gets the job done for this purpose.

Similar Threads

  1. Infinite loop
    By jyde001 in forum Java Applets
    Replies: 4
    Last Post: 08-09-2012, 01:13 AM
  2. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 12:52 PM
  3. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  4. Recursive for loop causing problems
    By Catfish1 in forum New To Java
    Replies: 2
    Last Post: 02-22-2011, 03:12 PM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 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
  •