0

这是第一个类 这是模板类

public class Email{
  private String title;
  private String to;
  private double size;
  private boolean sent;
  static int numEmails;

  public Email(String Title, String To, double Size){
    title = Title;
    to = To;
    size = Size;
    numEmails++;
  }

public void displayEmail(){
  System.out.printf("Subject: %30s%n", title);
  System.out.printf("To: %-35s%n",to);
  System.out.printf("This email has size: %.2f kB%n",size );
  if(sent)
  System.out.printf("This email has been sent%n");
  else
  System.out.printf("This email has not been sent%n");
}

public void sendEmail(){
  sent = true;
  displayEmail();
}

public boolean isValid(){
  int sep = to.indexOf('@');
  if(to.substring(sep).equals("@student.ksu.edu.sa"))
    return true;

  return false;
}

//setters
public void setTitle(String Title){
  title = Title;
}

public void setTo(String To){
  to = To;
}

public void setSize(double Size){
  size = Size;
}

public void setSent(boolean Sent){
  sent = Sent;
}

//getters
public String getTitle(){
  return title;
}

public String getTo(){
  return to; 
}

public double getSize(){
  return size;
}

public boolean getSent(){
  return sent;
}

}//End of class

这是第二节课

当我运行它时,它说错误StringIndexOutOfBounds

我想这与方法 isValid() 有关

我想通过从用户读取来填充数组中的第一个和第二个对象,但是也有一个错误,当我从用户那里读取时我似乎不知道为什么

import java.util.*;
public class Inbox{
static Scanner read = new Scanner (System.in);
static Email[] sentEmails = new Email[Email.numEmails];

//archive method
public static Email[] archive(Email[] emailList){
  for(int i=0;i<Email.numEmails;i++)
    if(emailList[i].getSent())
      sentEmails[i] = new Email(emailList[i].getTitle(),emailList[i].getTo(),emailList[i].getSize());
  return sentEmails;
}

//findByRecipient method
public static void findByRecipient(Email emailList[], String toAddress){
  System.out.println("The emails addressed to \"everyONE@student.ksu.edu.sa\" are:");
  System.out.println("==========================");
  toAddress = toAddress.toLowerCase();
  for(int i=0;i<Email.numEmails;i++)
    if(emailList[i].getTo().equals(toAddress))
      emailList[i].displayEmail();
}

//main
public static void main(String[] args){
  Email[] array = new Email[10];

  for(int i=0;i<2;i++){
    System.out.printf("Please enter the title, recipient, and size of email %d %n", (Email.numEmails+1));
    array[i] = new Email(read.nextLine(),read.nextLine(),read.nextDouble());
;}


  array[2] = new Email("Urgent: Lab final","everyone@student.ksu.edu.sa",200);
  array[3] = new Email("Fwd: Boarding pass","SaadAli@ksu.edu.sa",100);
  array[4] = new Email("You won 1M SR!","email@spam.com",5000.0);

  //1
  System.out.println("The Inbox currently contains");
  System.out.println("==========================");
  for(int j=0;j<Email.numEmails;j++){
    if(array[j]!=null)
      array[j].displayEmail();}

  for(int k=0;k<Email.numEmails;k++){
    if(array[k].isValid())
      array[k].sendEmail();
    else
      System.out.println("Email is invalid");
}


  //2
  archive(array);
  System.out.println("The Inbox after sending contains:");
  System.out.println("==========================");
  for(int s=0;s<sentEmails.length;s++){
    if(sentEmails[s]!=null)
      sentEmails[s].displayEmail();}

  //3
  findByRecipient(array,"everyONE@student.ksu.edu.sa");

  //4
  System.out.println("The titles of emails in archive are:");
  System.out.println("==========================");
  for(int r=0;r<Email.numEmails;r++)
    System.out.println(array[r].getTitle());


  //5
  System.out.println("Number of emails: " + Email.numEmails);


  }//End of main

}//End of class
4

1 回答 1

0

如果没有看到输入,很难确定,但您的isValid()函数不会检查“@”符号是否存在。如果调用此函数并且 var to没有“@”符号,则 var sep将设置为 -1,从而导致“to.substring(-1)”行出错。

于 2017-01-18T10:18:50.807 回答