即使我导入了java.lang。*,deleteCharAt()方法也出现“找不到符号”错误

时间:2020-03-08 04:42:52

标签: java

我是一个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;
      }
    }

1 个答案:

答案 0 :(得分:6)

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

String businessID;

StringBuilder businessID;

businessID = temp[0];

businessID = new StringBuilder(temp[0]);

在您要使用deleteCharAt(int)的其他地方也是如此。完成后,如果需要,您可以在toString()上调用StringBuilder并获得不可变 String