Results 1 to 6 of 6
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default for loop stops working

    Hi, I am writing a program that will have the user input information on 10 players into arrays, then display the players that meet a set of conditions. The program compiles fine, but when I input two players into the array (I set the max value of the arrays to 3 to make it easier for me to check the program) the for loop stops working. Could someone please help?

    Thanks,

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class baseball{
        public static void main(String []args) throws IOException
    	{
    		//Start Variables
    		int a;
    		String name [] = new String [3];
    		int age [] = new int [3];
    		String position [] = new String [3];
    		double average [] = new double [3];
    		boolean draft[] = new boolean [3];
    		String age_i;
    		String average_i;
    		
    		//Start Input Stream
                    BufferedReader in;
                    in = new BufferedReader(new InputStreamReader(System.in));
    		
    		//Main Menu
    		System.out.println ("|--------------|");
    		System.out.println ("|1. Input      |");
    		System.out.println ("|2. View Draft |");
    		System.out.println ("|3. Quit       |");
    		System.out.println ("|--------------|");
    		System.out.println ("|Input Choice: |");
    		String choice_i = in.readLine();
    		System.out.println ("");
    		
    		//Convert Choice
    		int choice = Integer.valueOf(choice_i).intValue();
    		
    		//Switch
    		switch(choice)
    		{
    			//Input
    			case 1:
    				for(a = 0; a <= 1; a++)
    				{
    					//State Player #
    					System.out.println ("|Player " + (a+1));
    					System.out.println ("|--------------");
    					
    					//Get Name
    					System.out.println ("Input Name:");
    					name [a] = in.readLine();
    					System.out.println ("");
    					
    					//Get Age
    					System.out.println ("Input Age:");
    					age_i = in.readLine();
    					System.out.println ("");
    					age [a] = Integer.valueOf(age_i).intValue();
    					
    					//Get Position
    					System.out.println ("Input Position:");
    					position [a] = in.readLine();
    					System.out.println ("");
    					
    					//Get Average
    					System.out.println ("Input Batting Average:");
    					average_i = in.readLine();
    					System.out.println ("");
    					average [a] = Double.valueOf(average_i).doubleValue();
    					
    					//Check For Can Draft
    					if (age[a] < 25)
    					{
    						if (average[a] >= 0.280)
    						{
    							draft[a] = true;
    						}else{
    							draft[a] = false;
    						}
    					}else{
    						draft[a] = false;
    					}
    					
    					System.out.println ("");
    				}
    				break;
    			
    			//View Draft Choices
    			case 2:
    				for(a = 0; a <= 1; a++)
    				{
    					if (draft[a] = true)
    					{
    						System.out.println (name[a] + ", " + age[a] + ", " + position[a] + ", " + average[a]);
    					}
    				break;
    			}
    		}
    	while (choice != 3);
    	}
    }

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default Re: for loop stops working

    Could you show us which line produces error. And correct line 88, you need == instead =.

  3. #3
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: for loop stops working

    It is lines 38-82

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default Re: for loop stops working

    There is a break; in line 92 which will stop the loop

  5. #5
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: for loop stops working

    Thanks for the help; I didn't solve the issue, but I think I can figure it out. I must end this thread now.

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default Re: for loop stops working

    OK, you have next part of code:
    Java Code:
    case 2:                 
        for(a = 0; a <= 1; a++){                    
            if (draft[a] = true){                         
                System.out.println (name[a] + ", " + age[a] + ", " + position[a] + ", " + average[a]);                     
            }                 
            break; // this will break for loop        
        }
    That is the first problem. The second is
    Java Code:
    while (choice != 3);
    What is your intention with this condition. What do you think the program will do when reach this?

Similar Threads

  1. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 10:36 PM
  2. While loop stops before told?
    By Naxix in forum New To Java
    Replies: 8
    Last Post: 07-08-2011, 07:24 AM
  3. Replies: 3
    Last Post: 12-07-2010, 09:56 PM
  4. Timer stops working :P
    By Addez in forum New To Java
    Replies: 13
    Last Post: 09-21-2010, 08:36 PM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •