我是一个非常初学者的JAVA编码器,这个问题可能真的很幼稚,但是正如该问题所指出的那样,我的代码由于无法找到deleteCharAt()而不断给我错误。我真的很感谢您的见解!这是我的代码

    package hw5;
    import java.util.*;
    import java.lang.*;

    public class Business {
      String businessID;
      String businessName;
      String businessAddress;
      String reviews;
      int reviewCharCount;


      // Constructor for the Business Class
      public Business (String s) {
        String[] temp = s.split(", "); // splits the string by a comma and space
        businessID = temp[0]; // stores first index into business ID
        businessID.deleteCharAt(0); // delete the {
        businessName = temp[1]; // stores 2nd index into businessName
        businessAddress = temp[2]; // stores 3rd index into businessName
        reviews = temp [3]; // tores 4th index into businessName
        reviews.deleteCharAt(reviews.length()-1); // delete the last }
        reviewCharCount = reviews.length(); // input character # into reviewCharCount
      }

      public List reviewList() {
        String[] temp = this.reviews.split(" "); // make reviews into an array
        List<String> list = Arrays.asList(temp); // make array into a list
        Iterator<String> itr = list.iterator(); // initializes iterator for list
        while (itr.hasNext()) { // iterates over the whole list
          String uniqueWord = itr.next(); // store next element into string
          // if the string equals a nonimportant word
          if (uniqueWord.equals("a") || uniqueWord.equals("the") || uniqueWord.equals("is") || uniqueWord.equals("and")) {
            list.remove(uniqueWord); // remove the word
          }
        }
        return list; // returns list
      }

      public String toString() {
        return "-------------------------------------------------------------------------------\n"
              + "Business ID: " + businessID + "\n"
              + "Business Name: " + businessName + "\n"
              + "Business Address: " + businessAddress + "\n"
              //+ "Reviews: " + reviews + "\n"
              + "Character Count: " + reviewCharCount;
      }
    }

最佳答案

因为没有String.deleteCharAt(int)方法。有一个StringBuilder.deleteCharAt(int)方法。更改

String businessID;




StringBuilder businessID;




businessID = temp[0];




businessID = new StringBuilder(temp[0]);


在您要使用deleteCharAt(int)的其他所有地方都一样。完成后,如果需要,可以在toString()上调用StringBuilder并获得一个不变的String

07-26 02:01