-3

I have been looking at this for a bit.. and I even put in the item = null to see what happens.. but this bit of code keeps getting stuck in an infinite loop. item is always equal to null and should kick out of the loop but it keeps running.

 ArrayList<String> collection = new ArrayList();
  Scanner arrayRead = new Scanner(new FileReader("SalesStar.txt"));

   //iterate through file to put into Arraylist
   String item = null;
   while( item != null ) {           
       item = arrayRead.next();
       collection.add(item);           
   }

   arrayRead.close();


   System.out.println(collection);
   System.out.println(item);
stinepike
  • 54,068
  • 14
  • 92
  • 112
user2368621
  • 1
  • 1
  • 1
  • Clean and rebuild/compile. – Sotirios Delimanolis May 10 '13 at 04:02
  • 3
    This will not loop at all. Is this the code you are actually having a problem with? – Ted Hopp May 10 '13 at 04:02
  • 1
    How would you even enter the `while` loop - if it is initialized to `null` you should never even go around it once? – Floris May 10 '13 at 04:04
  • 2
    @ay89 - Most likely it should be `String item = arrayRead.next();` – Ted Hopp May 10 '13 at 04:04
  • Using `while(arrayRead.hasNext())` is affordable. – dmahapatro May 10 '13 at 04:05
  • How do you know "it keeps running" - if you add a print statement in the loop, can you observe it going around the loop even though `item==null`? I wonder if you are not getting stuck somewhere else... Please clarify this point by editing your question. – Floris May 10 '13 at 04:10
  • Thank you for looking.. it seems there may be something with the class in general. When I call the class it just loops instead of running once and stopping. – user2368621 May 11 '13 at 01:46

3 Answers3

2

It will not enter the loop

   String item = null;

in this line your are setting the item null

   while( item != null ) 

but in the very next line you set the condition item!=null whill will always false, So it will never enter the loop

To get it working you can do the following (as all already explained)

   while( arrayRead.hasNext() ) {           
       item = arrayRead.next();
       collection.add(item);           
   }
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 2
    Your observation is correct - but this doesn't explain why the OP thinks he gets stuck in an "infinite loop" (as opposed to a "loop that never runs"). – Floris May 10 '13 at 04:06
0

Try this change

while(arrayRead.hasNext()) 

As a side note, in my view this looks better

Scanner arrayRead = new Scanner(new File("SalesStar.txt"));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

I cant see any situation when the loop could go to infinite. So best condition here to check for is checking whether scanner hasNext() element.

String item = null;
   while( arrayRead.hasNext() ) {           
       item = arrayRead.next();
       collection.add(item);           
   }

or if you wanna continue with current logic, String should be initialized as

String item = arrayRead.next();
Ankit
  • 6,554
  • 6
  • 49
  • 71