Results 1 to 4 of 4
  1. #1
    Muk
    Muk is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Need help fixing infinite loop

    This program supposed to print out a list of regiments. Each regiment has a number, name, and number of men (strength). Every week 100 men are added to the regiments except for regiment 5 which gets added 30 men. The regiment with the highest amount of men gets deleted from the list. The program supposed to run for 20 weeks but I'm pretty sure it supposed to run until all the men are sent away (deleted from the list). In other words, until the list is empty. My loop right now looks like this
    Java Code:
     do
          {
       
          System.out.println(Military.Update());  
    // update basically adds 100 men to the regiments except regiment 5 which gets added 30
      Military.RemoveFromList(Military.getLargest()); 
    // checks for the regiment with the largest amount of men and removes it from the list
       }
          
          while(Military!=null);
    //HERE IS THE PROBLEM
    The problem is that it keeps printing out empty lists. It just keeps printing the heading (Regiment #, Regiment Name, Number of Men
    How can the program be terminated when there are no more regiments on the list. I was thinking about something like this
    while( THERE ARE MORE REGIMENTS ON THE LIST ) but how would I do this? How would I check to see if the list is empty? Any help would
    be really appreciated. Thank you.

    This is my tester class.

    Java Code:
    import java.io.File;  
    import java.io.IOException;  
    import java.util.Scanner;  
    
      
    public class RegimentTester {  
      
       public static void main(String[] args) throws IOException  
       {  
      
      
      Army Military = new Army();  
      
          // create Scanner object to read each line of file until eof  
          Scanner fileScan = new Scanner(new File("Regiments.txt"));  
      
          System.out.println("Report Summary:\n");  
      
          while (fileScan.hasNext()) // while not eof...  
          {  
             // read next line  
             String line = fileScan.nextLine();  
      
             // "echo print" data entered  
             System.out.println(line);  
      
             //  create a Scanner object  
        Scanner lineScanor = new Scanner(line) ;  
            //  extract tokens from current line  
          int RegNum = lineScanor.nextInt();  
            String Name = lineScanor.next();  
      
      
      
      
      
             // create Regiment object passing the tokens to the constructor  
            Regiment adder = new Regiment(RegNum, Name);  
             //  add object to list  
            Military.AddToList(adder) ;  
      
       }  
             
      
          System.out.println(Military.printRegList()); // print the list  
      
          do
          {
       
          System.out.println(Military.Update());  
      Military.RemoveFromList(Military.getLargest()); 
       }
          
          while(Military!=null);
    //HERE IS THE PROBLEM
       
    
       }  
    
    }

  2. #2
    Muk
    Muk is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help fixing infinite loop

    Oh wow I think I got it! I was supposed to have this instead
    Java Code:
    while(Military.getLargest() != null);
    Lol yay!!!

  3. #3
    Muk
    Muk is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help fixing infinite loop

    If someone can explain why the previous loop I had didn't work that would be nice. I understand this one.

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

    Default Re: Need help fixing infinite loop

    It's hard for me to understand your class as you're using non-standard Java naming (note that class names should start with an upper case letter and method and variable names with a lower case letter), your code formatting is a bit wild with non-standard indentation, and we don't know what the Army class looks like.

    Consider fixing your code so that we can better understand it, and posting your Army class.

Similar Threads

  1. Infinite loop
    By jyde001 in forum Java Applets
    Replies: 4
    Last Post: 08-09-2012, 02:13 AM
  2. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 01:52 PM
  3. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  4. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-14-2010, 12:45 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11: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
  •