1

My professor asked for us to make two separate java classes for a popular problem where you have to build an employee email based off of a first name, last name, and employee ID number.

If you'd like to see the problem: Assignment

The problem is happening in the substring. I think I vaguely know why, but I'm not actually entirely sure how to solve the issue.

Here's the first class:

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");
   }
}

And the second class:

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());
   }
}

And this is the error I'm getting:

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)

The Scanner isn't necessary, but our professor offered extra credit if we were able to do it. I'm just absolutely lost.

Mythrim
  • 11
  • 3

3 Answers3

0

There is an error cause you are trying to substring the name variables and assign them to member variables in the class body. Not inside any method. As they will be empty strings when the class gets intialized, the exception is thrown.

Change the EmployeeSchmidt java class to following.

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");
    }
}
Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
0

Note the placement of the declarations of variables fName, lName, and eID in your code:

   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");
   }

They are outside any method, and therefore they are instance variables. These initializations will be performed when each instance is initialized, before the body of the constructor. At that point, you are guaranteed that the base strings are all empty (because of their own, prior, initializers).

Perhaps you want those to be local variables of the Generator() method. In that case, their initializers would not run until Generator() is invoked, at which point the FirstName, LastName, and EmID variables will have the values set by the constructor.

Note, however, that even that does not fully protect you from an out-of-bounds string index. Consider, for instance, an employee whose last name is "Wu" -- that's fewer than four characters. There are two things you can do to solve this problem:

  1. Pad the strings with blanks or some other character to ensure that they are at least as long as you need them to be before computing the substrings (and be prepared for trailing blanks/whatever in the results; example: fName = (FirstName + "..").subString(0,2);), or
  2. Test the string lengths before trying to extract substrings, and use the whole string instead where appropriate. Alternatively, fail (throwing an exception) if the string is not long enough.
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

you must make the substring in method because if it outside the method you make substring of the

  FirstName = "";
  LastName = "";
  EmID = "";
  Email = "";

and that's error you want to substring of the input of user you can make that solution make it in method

    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;

then in the second class you do that

System.out.println("Your Employee Email is " +  em1.getinfo());

without Generator before

i hope you understand me

ruba ahmad
  • 24
  • 2