Results 1 to 6 of 6
Thread: for loop stops working
- 04-17-2012, 03:58 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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); } }
- 04-17-2012, 04:03 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 8
Re: for loop stops working
Could you show us which line produces error. And correct line 88, you need == instead =.
- 04-17-2012, 04:08 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: for loop stops working
It is lines 38-82
- 04-17-2012, 04:23 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 8
Re: for loop stops working
There is a break; in line 92 which will stop the loop
- 04-17-2012, 04:39 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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.
- 04-17-2012, 04:53 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 8
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 }
Java Code:while (choice != 3);
Similar Threads
-
Loop not working
By swilliams236 in forum New To JavaReplies: 2Last Post: 11-07-2011, 10:36 PM -
While loop stops before told?
By Naxix in forum New To JavaReplies: 8Last Post: 07-08-2011, 07:24 AM -
JFrame stops working after method called through acitonListener urgent ;(
By Addez in forum New To JavaReplies: 3Last Post: 12-07-2010, 09:56 PM -
Timer stops working :P
By Addez in forum New To JavaReplies: 13Last Post: 09-21-2010, 08:36 PM -
while loop not working
By RBNSN83 in forum New To JavaReplies: 6Last Post: 06-21-2010, 07:29 AM
Bookmarks