0

I am programming a Java program that reads a text file, and outputs the array of emails. This is a section of the code that I am utilziing to obtain the emails.

public static EMail[] input(String filename)
  {
    Scanner infile = null;
    try
    { 
      infile = new Scanner(new File(filename));
    }
    catch (FileNotFoundException e)
    {
      JOptionPane.showMessageDialog(null, "The file could not be found.");
      System.exit(0);
    }
    int numitems = infile.nextInt();
    EMail[] array = new EMail[numitems];
    for (int index = 0; index < numitems; index++)
    {
        String email = infile.nextLine();
        array[index] = new EMail(email);
    }
    infile.close();
    return array;
  }

And this is my email class. As you can see, I am obtaining the UserName and the HostName and the Extension from the email utilizing Substring.

    //Name______________________________ Date_____________
   public class EMail
   {
      private String myUserName;
      private String myHostName;
      private String myExtension;
      public EMail(String address)
      {
       int at = address.indexOf('@');
       this.myUserName = address.substring(0, at);
       int dot = address.indexOf('.');
       this.myHostName = address.substring(at + 1, dot);
       this.myExtension = address.substring(dot + 1, address.length());
      }
      public String getUserName()
      {
         return myUserName;
      }
      public String getHostName()
      {
         return myHostName;
      }
      public String getExtension()
      {
         return myExtension;
      }
      public void setUserName(String string)
      {
          this.myUserName = string;
      }
      public void setMyHostName(String string)
      {
          this.myHostName = string;
      }
      public void setMyExtensionName(String string)
      {
          this.myExtension = string;
      }
      public String toString()
      {
        return( getUserName() + "@" + getHostName() + "." + getExtension());
      }
   }

Finally, this was the text file. The first number corresponds to the number of emails in the file.

12
tangent@angle.com
comp@2018.com
nsa@security.org
yawn@sleep.sleep
theNumber@files.file
fruit@tree.file
amd@cold.com
Mac@Apple.fruit
woooo@sh.email
meme@social.word
clippy@word.nowgone
java@sun.useful

However, after running my Driver08ext, which uses input(data.txt);, I recieve this error, which is..

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Lab08.EMail.<init>(EMail.java:12)
    at Lab08.Driver08ext.input(Driver08ext.java:37)
    at Lab08.Driver08ext.main(Driver08ext.java:15)

I must be misunderstanding how Substring works, since I never access an index less than 0.

  • 1
    You'll get a `-1` from `indexOf` if the character is not found, which you are not checking before passing to `substring`. – rgettman Apr 19 '18 at 00:28
  • 2
    I see `nextInt` and then `nextLine`. This is a `nextFoo` dupe. Change `int numitems = infile.nextInt();` to `int numitems = Integer.parseInt(infile.nextLine());` (and read the linked duplicate, there are other options). – Elliott Frisch Apr 19 '18 at 00:30
  • Sorry, but I don't see why that would occur? All my emails have at symbols and dots. – Martin Williams Apr 19 '18 at 00:30
  • @ElliottFrisch Something like this? Or can you find a better one? https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Erwin Bolwidt Apr 19 '18 at 00:32
  • Thank you, I should have read the documentation. – Martin Williams Apr 19 '18 at 00:32

0 Answers0