ajg55
ajg55

Reputation: 17

cannot find out why my program is stuck in an infinite loop

Whenever I try to iterate through the while loop in my Directory constructor, the Eclipse console will not output any message. I tried to run it while commenting out the loop and the program was capable of reaching and printing the intro message from the main method. I tried to use another condition for the while loop like while(directoryposition

public class DirectoryServer {


public static void main(String[] args)
{
    //Initiates Directory called UniversityName.txt***********
    String DirectoryName = "UniversityDirectory.txt";
    //String outFile = "tmp.txt";
    Directory dir = new Directory(DirectoryName);
    //Directory out = new Directory(outFile);
    //*********************************************************


    Scanner stdin = new Scanner(System.in);

    //Introductory Message
    System.out.println("Welcome to the program user\n");
    System.out.println("The actions that can be taken are:\n");
    System.out.println("find    (Last Name)\nadd    (UCID)\ndelete  (UCID)\n");
    System.out.println("What do you want to do?");



  public class Directory {

final int max = 1024;
Person directory[] = new Person[max];//Create a directory of type Person
String position,UCID,first,last,major,email,dept,office;

Scanner inFile=null;
File outFile=null;
Scanner inFileData=null;
Scanner data=null;



Directory(String DirectoryFileName)//Constructor***************
{
    try{
    inFile = new Scanner(new FileInputStream(DirectoryFileName));
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found exiting program\n");
        System.exit(0);
    }

    int directoryposition=0;
    while(inFile.hasNext());
    {
        data = new Scanner(inFile.nextLine());



        position = data.next();
        position=position.toLowerCase();


        switch(position)//do something

Upvotes: 1

Views: 468

Answers (2)

nicomp
nicomp

Reputation: 4647

You have a ; after your while statement. The loop ends there.

while(inFile.hasNext());

Upvotes: 5

Abdelhak
Abdelhak

Reputation: 8387

Try to remove semicolon;:

  while(inFile.hasNext());{...

To:

 while(inFile.hasNext()){...

Upvotes: 1

Related Questions