我的教授要求我们针对一个流行的问题制作两个单独的java类,您必须根据名字,姓氏和员工ID编号构建员工电子邮件。
如果您想看到问题: Assignment
问题发生在子字符串中。我想我隐约知道为什么,但我实际上并不完全确定如何解决这个问题。
这是第一堂课:
public class EmployeeSchmidt
{
public String FirstName = "";
public String LastName = "";
public String EmID = "";
public String Email = "";
public EmployeeSchmidt(String FirstName, String LastName, String EmID)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.EmID = EmID;
Generator();
}
public String getFirstName()
{
return FirstName;
}
public void setFirstName(String em)
{
FirstName = em;
}
public String getLastName()
{
return LastName;
}
public void setLastName(String em)
{
LastName = em;
}
public String getEmID()
{
return EmID;
}
public void setEmID(String em)
{
EmID = em;
}
public String getEmail()
{
return Email;
}
public void setEmail(String em)
{
Email = em;
}
String fName = (FirstName.substring(0,2));
String lName = (LastName.substring(0,4));
String eID = (EmID.substring(3,4));
public void Generator()
{
Email = (fName + lName + eID + "@initech.com");
}
}
第二节课:
import java.util.Scanner;
public class EmployeeInfo
{
public static void main(String[] args)
{
EmployeeSchmidt em1 = new EmployeeSchmidt("","","");
Scanner in = new Scanner (System.in);
System.out.println("Please enter your first name.");
em1.setFirstName(in.next());
System.out.println("Please enter your last name.");
em1.setLastName(in.next());
System.out.println("Please enter your 5-digit Employee ID.");
em1.setEmID(in.next());
em1.Generator();
System.out.println();
System.out.println(em1.getFirstName());
System.out.println(em1.getLastName());
System.out.println(em1.getEmID());
System.out.println("Your Employee Email is " + em1.getEmail());
}
}
这就是我得到的错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(String.java:1963)
at EmployeeSchmidt.<init>(EmployeeSchmidt.java:57)
at EmployeeInfo.main(EmployeeInfo.java:6)
扫描仪不是必需的,但是如果我们能够做到这一点,我们的教授会提供额外的功劳。我完全迷失了。
答案 0 :(得分:0)
有一个错误导致您尝试对名称变量进行子字符串并将它们分配给类主体中的成员变量。不在任何方法内。因为当类被初始化时它们将是空字符串,所以抛出异常。
将EmployeeSchmidt java类更改为以下内容。
public class EmployeeSchmidt {
public String FirstName = "";
public String LastName = "";
public String EmID = "";
public String Email = "";
public EmployeeSchmidt(String FirstName, String LastName, String EmID) {
this.FirstName = FirstName;
this.LastName = LastName;
this.EmID = EmID;
Generator();
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String em) {
FirstName = em;
}
public String getLastName() {
return LastName;
}
public void setLastName(String em) {
LastName = em;
}
public String getEmID() {
return EmID;
}
public void setEmID(String em) {
EmID = em;
}
public String getEmail() {
return Email;
}
public void setEmail(String em) {
Email = em;
}
public void Generator() {
String fName = null;
if (FirstName != null && FirstName.length() >= 2) {
fName = (FirstName.substring(0, 2));
}
String lName = null;
if (LastName != null && LastName.length()>=4) {
lName = (LastName.substring(0, 4));
}
String eID = null;
if (EmID != null && EmID.length()>=4) {
eID = (EmID.substring(3, 4));
}
Email = (fName + lName + eID + "@initech.com");
}
}
答案 1 :(得分:0)
请注意在代码中放置变量声明fName
,lName
和eID
:
String fName = (FirstName.substring(0,2)); String lName = (LastName.substring(0,4)); String eID = (EmID.substring(3,4)); public void Generator() { Email = (fName + lName + eID + "@initech.com"); }
它们在任何方法之外,因此它们是实例变量。这些初始化将在每个实例初始化时执行,在构造函数体之前执行。此时,您可以保证基本字符串都是空的(因为它们自己的,先前的初始化器)。
也许你希望那些是Generator()
方法的局部变量。在这种情况下,在调用Generator()
之前,它们的初始值设定项不会运行,此时FirstName
,LastName
和EmID
变量将具有构造函数设置的值。
但请注意,即使这样也不能完全保护您免受越界字符串索引的影响。例如,考虑一个姓氏为&#34; Wu&#34; - 那个不到四个字符。你可以做两件事来解决这个问题:
答案 2 :(得分:0)
你必须在方法中创建子字符串,因为如果它在方法之外,你可以创建
的子字符串 FirstName = "";
LastName = "";
EmID = "";
Email = "";
这是你想要输入用户的子串的错误 你可以让这个解决方案在方法中实现
public String getinfo()
{ String fName = (FirstName.substring(0,2));
String lName = (LastName.substring(0,4));
String eID = (EmID.substring(3,4));
Email=Generator(fName,lName,eID);
return Email;}
public String Generator(String fName,String lName,String eID)
{
Email = (fName + lName + eID + "@initech.com");
return Email;
然后在第二课中你这样做
System.out.println("Your Employee Email is " + em1.getinfo());
我希望你理解我