我有一个java程序,我正在其中收集用户的条目。系统会提示用户输入姓名、电话号码和电子邮件地址。 当我输入全名(即:Mike Smith)时,程序仅保留名字。当它将电子邮件地址和电话号码发送到文本文档时,它们会被交换,因此电子邮件地址位于电话号码部分,电话号码位于电子邮件地址部分。
这是我主要从用户那里获取信息的部分
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);
这是我的 validator 类验证条目的部分
public static String getEntry(Scanner ip, String prompt)
{
System.out.println(prompt);
String e = ip.next();
ip.nextLine();
return e;
}
我确实尝试通过消除 validator 并仅键入来解决此问题
system.out.println("Enter name:");
name = ip.next();
对于电子邮件和电话等等,但我得到了与通过 validator 类运行它相同的结果。我对接下来要检查什么感到困惑。我所做的事情有什么问题吗?
这是我的 AddressBookEntry 类
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);
}
}
}
最佳答案
Scanner.next()
将一次读取一个单词,这就是为什么它只读取名字。如果您想要整行,请使用 Scanner.nextLine()
。
System.out.println(prompt);
String e = ip.nextLine();
return e;
关于java - 验证输入并将其发送到文本文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9605241/