Jeremy B
Jeremy B

Reputation: 863

validating and sending input to a text doc

I have a java program where I am collecting entries from a user. The user is prompted to enter a name, phone number, and email address. When I type the full name (ie: Mike Smith) the program only retains the first name. When it sends the email address and phone number to the text doc they are swapped so the email address is in the phone number section and the phone number is in the email address section.

Here is the section of my main getting the information from the user

                String name = Validator.getEntry(ip, "Enter name: ");
                String email = Validator.getEntry(ip, "Enter email address");
                String phone = Validator.getEntry(ip, "Enter phone number: ");
                AddressBookEntry newEntry = new AddressBookEntry(name, email, phone);
                AddressBookIO.saveEntry(newEntry);

Here is the section of my validator class validating the entry

public static String getEntry(Scanner ip, String prompt)
{

    System.out.println(prompt);
    String e = ip.next();
    ip.nextLine();
    return e;
}

I did try to troubleshoot this by eliminating the validator and just typing

    system.out.println("Enter name:");
    name = ip.next();

and so on for the email and phone but I got the same results as running it through the validator class. I'm confused on what to check next. Is there anything wrong with what I have done?

Here is my AddressBookEntry clas

     public class AddressBookEntry 
    {
private String name;
private String emailAddress;
private String phoneNumber;

public AddressBookEntry()
{
    name = "";
    emailAddress = "";
    phoneNumber = "";
}

public void setName(String name)
{
    this.name = name;
}

public String getName()
{
    return name;
}

public void setEmailAddress(String emailAddress)
{
    this.emailAddress = emailAddress;
}

public String getEmailAddress()
{
    return emailAddress;
}

public void setPhoneNumber(String phoneNumber)
{
    this.phoneNumber = phoneNumber;
}

public String getPhoneNumber()
{
    return phoneNumber;
}

public AddressBookEntry(String newname, String newphone, String newemail)
{
    name = newname;
    emailAddress = newemail;
    phoneNumber = newphone;
}
    }

Here is my IO class

    import java.io.*;

    public class AddressBookIO
    {
private static File addressBookFile = new File("address_book.txt");
private static final String FIELD_SEP = "\t";
private static final int COL_WIDTH = 20;

// use this method to return a string that displays
// all entries in the address_book.txt file
public static String getEntriesString()
{
    BufferedReader in = null;
    try
    {
        checkFile();

        in = new BufferedReader(
             new FileReader(addressBookFile));

        // define the string and set a header
        String entriesString = "";
        entriesString = padWithSpaces("Name", COL_WIDTH)
            + padWithSpaces("Email", COL_WIDTH)
            + padWithSpaces("Phone", COL_WIDTH)
            + "\n";

        entriesString += padWithSpaces("------------------", COL_WIDTH)
            + padWithSpaces("------------------", COL_WIDTH)
            + padWithSpaces("------------------", COL_WIDTH)
            + "\n";

        // append each line in the file to the entriesString
        String line = in.readLine();
        while(line != null)
        {
            String[] columns = line.split(FIELD_SEP);
            String name = columns[0];
            String emailAddress = columns[1];
            String phoneNumber = columns[2];

            entriesString +=
                padWithSpaces(name, COL_WIDTH) +
                padWithSpaces(emailAddress, COL_WIDTH) +
                padWithSpaces(phoneNumber, COL_WIDTH) +
                "\n";

            line = in.readLine();
        }
        return entriesString;
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
        return null;
    }
    finally
    {
        close(in);
    }
}

// use this method to append an address book entry
// to the end of the address_book.txt file
public static boolean saveEntry(AddressBookEntry entry)
{
    PrintWriter out = null;
    try
    {
        checkFile();

        // open output stream for appending
        out = new PrintWriter(
              new BufferedWriter(
              new FileWriter(addressBookFile, true)));

        // write all entry to the end of the file
        out.print(entry.getName() + FIELD_SEP);
        out.print(entry.getEmailAddress() + FIELD_SEP);
        out.print(entry.getPhoneNumber() + FIELD_SEP);
        out.println();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
        return false;
    }
    finally
    {
        close(out);
    }
    return true;
}

// a private method that creates a blank file if the file doesn't already exist
private static void checkFile() throws IOException
{
    // if the file doesn't exist, create it
    if (!addressBookFile.exists())
        addressBookFile.createNewFile();
}

// a private method that closes the I/O stream
private static void close(Closeable stream)
{
    try
    {
        if (stream != null)
            stream.close();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
}

// a private method that is used to set the width of a column
private static String padWithSpaces(String s, int length)
{
    if (s.length() < length)
    {
        StringBuilder sb = new StringBuilder(s);
        while(sb.length() < length)
        {
            sb.append(" ");
        }
        return sb.toString();
    }
    else
    {
        return s.substring(0, length);
    }
}

}

Upvotes: 0

Views: 157

Answers (1)

bvulaj
bvulaj

Reputation: 5123

Scanner.next() will read one word at a time, which is why it is only reading the first name. Use Scanner.nextLine() if you want the whole line.

System.out.println(prompt);
String e = ip.nextLine();
return e;

Upvotes: 2

Related Questions