2 Replies - 748 Views - Last Post: 02 December 2009 - 05:41 PM Rate Topic: -----

#1 soap   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-April 09

Help With Infinite Loop

Posted 02 December 2009 - 05:12 PM

Hello everyone,
This piece of code creates an infinite loop every time I run it. Can anyone see what's causing it? I know I have my own classes in there, but they are not causing the loop. Thanks in advance.

package fileinputPkg;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

import personPkg.Athlete;
public class AthleteFileInput {
	private static ArrayList<Athlete> a = new ArrayList<Athlete>();
	private String[] str;
	public AthleteFileInput(){
		
			try {
				FileReader fread = new FileReader("data.txt");
				Scanner parser = new Scanner(fread);
				int j =0;
				while(parser.hasNext()){	
					str = new String[4];
					while (j < str.length) {
						str[j] = parser.nextLine();
						System.out.println(str[j]);
						j++;
					}
					ArrayList<String> al = new ArrayList<String>();
					for(int i=0;i<str.length;i++){
						al.add(str[i]);
					}
					Athlete at = new Athlete(al);
					a.add(at);
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}//default constructor
	public String toString(){
		return "Athlete File Input";
	}//toString
}//AthleteFileInput



Is This A Good Question/Topic? 0
  • +

Replies To: Help With Infinite Loop

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help With Infinite Loop

Posted 02 December 2009 - 05:32 PM

You make it complicated for nothing what don't you just do

				while(parser.hasNext()){	
					ArrayList<String> al = new ArrayList<String>();
					for(int i=0; i < 4; i++){
						al.add(parser.nextLine();
					}
					Athlete at = new Athlete(al);
					a.add(at);
				}


and forget about the String array and j and the while loop ?

Don't see any infinite loop here
You will have to show us your Athlete class
Was This Post Helpful? 1
  • +
  • -

#3 soap   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-April 09

Re: Help With Infinite Loop

Posted 02 December 2009 - 05:41 PM

View Postpbl, on 2 Dec, 2009 - 04:32 PM, said:

You make it complicated for nothing what don't you just do

				while(parser.hasNext()){	
					ArrayList<String> al = new ArrayList<String>();
					for(int i=0; i < 4; i++){
						al.add(parser.nextLine();
					}
					Athlete at = new Athlete(al);
					a.add(at);
				}


and forget about the String array and j and the while loop ?

Don't see any infinite loop here
You will have to show us your Athlete class

Thanks a lot pbl! My last piece of code was a little too complicated, but it still should of worked. But I'll use this way.
Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1