I don't know why/how to fix this...It's saying i have a NoSuchElement exception on line n=f.nextLine();

while(i<listFlights.length){
		
			i++;
			n=f.nextLine();
			d=f.nextLine();
			h=f.nextInt();
			m=f.nextInt();

			Time one=new Time(h, m);
			listFlights[i]=new Flight(n, d, one);
		}

Hi,

NoSuchElement is thrown by the nextElement() method of an Enumeration to indicate that there are no more elements in the enumeration, so before calling this method you should try to check if there is any element remaining , you can use method hasMoreElements() to check that.

Also on a different not you should post complete code , here its not clear which type of object "f" is , though I am assuming its Enumeration.

Ok, it seems the nextLine() method doesn't retrieve the "nextLine()" Because, if you just call nextLine() once, it prints the array fine. I made this simple code to test, and it works fine for me.

public static void getFile(Scanner f)
    {

               String[] sArray = new String[12];
		//listFlights=new Flight[count/3];
		//num=listFlights.length;
		int i=0;
		String n;
		while(f.hasNext()){
			n=f.nextLine();
                        sArray[i]=n;
                        System.out.println(sArray[i]);
                        i++;
		}
    }

Thanks so much for your input guys. Sadly, I didn't have time to correct the problem before I had to turn in my program. Luckily, he must have hand-graded it and I got an 87.

Thanks for telling me this problem. I will remember it for next time I get an odd error.

This article has been dead for over six months. Start a new discussion instead.