0
我有一個Java程序,我從用戶收集條目。提示用戶輸入姓名,電話號碼和電子郵件地址。 當我輸入全名(即:邁克史密斯)時,程序只保留名字。當它將電子郵件地址和電話號碼發送到文本文檔時,它們將被交換,以便電子郵件地址位於電話號碼部分,電話號碼位於電子郵件地址部分。驗證併發送輸入到文本文檔
這裏是我的主要獲取信息從用戶
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);
這裏的部分是我驗證類驗證進入
public static String getEntry(Scanner ip, String prompt)
{
System.out.println(prompt);
String e = ip.next();
ip.nextLine();
return e;
}
我曾嘗試通過消除解決此節驗證器,只是打字
system.out.println("Enter name:");
name = ip.next();
等等爲e郵件和電話,但通過驗證器類運行它我得到了相同的結果。我對接下來要檢查的內容感到困惑。我做了什麼有什麼不對嗎?
這裏是我的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;
}
}
這裏是我的IO類
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);
}
}
}
我們不知道爲什麼你的電子郵件/電話號碼正在交換,而沒有看到你的AddressBookEntry/IO類正在做什麼。 – bvulaj 2012-03-07 16:36:44
我會發布他們。 – 2012-03-07 16:45:28