3

I am not sure what is wrong and my Professor is stumped as well. The while loop condition only terminates if the condition is met right away. Once the loop is running, the met condition no longer stops it and it just keeps going. Its like the look isnt checking the condition anymore? Below is my code. Thank you for any help.

    import java.io.*;
    import java.util.*;


public class createPhoneList {

    public static void main(String[] args) {

        DataOutputStream ostream;//creates output stream
        Scanner input = new Scanner(System.in);//creates scanner in object
        final String DONE = "DONE";//will be used to end data entry loop
        String firstName;
        String lastName;
        long phoneNumber;

        try{
            ostream = new DataOutputStream(new   FileOutputStream("javaContactsUnit4Assignment.txt"));
            System.out.print("Enter First Name or type 'DONE' to quit: ");
            firstName = input.nextLine();
            while(!firstName.equalsIgnoreCase(DONE)){
                /*
                 * Error occuring at runtime where while loop terminates only if
                 * "done" is typed first, but once the loop is running "done" no longer
                 * stops it. Not sure what is wrong...
                 */
                input.nextLine();
                System.out.print("Please enter Last Name: ");
                lastName = input.nextLine();
                System.out.print("Please enter the Phone Number(with NO DASHES, as they will cause a fatal error): ");
                phoneNumber = input.nextLong();
                ostream.writeUTF(firstName);
                ostream.writeUTF(lastName);
                ostream.writeLong(phoneNumber);

                System.out.print("Enter First Name or type 'DONE' to quit: ");
                firstName = input.nextLine();
                }

        }
        catch(IOException e){
            System.err.println("Error opening file.");
        }
        catch(InputMismatchException e){
            System.err.println("Invalid data was entered. Please try again.");
        }
        catch(Exception e){
            System.err.println("You encountered a fatal error. Please try again.");
        }

    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

4

You should add another input.nextLine(); after phoneNumber = input.nextLong();.

That's because input.nextLong() command reads only the long value (and skips the \n which is the enter key you press right after).

So when you continue reading with input.nextLine() you receive the \n and not "done", that's why your loop never terminates. So adding another input.nextLine() will "swallow" the \n and the next one will read the actual String.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • So why is it still messing up when i change all the 'Long' variable types to 'Int'? Changing that should have fixed it as well if it was as you say, but it didnt. That was a very good answer though. – user2286647 Apr 16 '13 at 13:56
  • The same happens for `nextInt`, just add another `nextLine` after you read int/long/double.. see my explanation. – Maroun Apr 16 '13 at 13:57