Java 将两个字符串与startsWith进行比较时出现编译错误
我正在用JSP为google应用程序引擎开发。我需要通过String类中的startsWith()方法比较两个字符串 这是我正在编写的代码Java 将两个字符串与startsWith进行比较时出现编译错误,java,string,jsp,Java,String,Jsp,我正在用JSP为google应用程序引擎开发。我需要通过String类中的startsWith()方法比较两个字符串 这是我正在编写的代码 <% String artist = "Surendra Perera"; DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key songKey = KeyFact
<%
String artist = "Surendra Perera";
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key songKey = KeyFactory.createKey("songs", 123454);
// Run an ancestor query to ensure we see the most up-to-date
// view of the songs.
Query query = new Query("Song", songKey).addSort("Artist");
//query.addFilter("Artist", Query.FilterOperator.IN, "Milton Mallawarachchi");
List<Entity> songsList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(30));
if(songsList.isEmpty()){
%>
<p>There are no songs</p>
<%
}else{
%>
<ul class="playlist">
<%
for(Entity song : songsList){
if(artist.startsWith(song.getProperty("Artist"))){
%>
<li><a href="<%= song.getProperty("Link") %>"><%= song.getProperty("Title") %>  <span class="comment"><%= song.getProperty("Artist") %></span></a></li>
<% }}} %>
提前谢谢 错误表明它在
字符串
类中找不到给定的方法方法startsWith(java.lang.Object)
。请注意,错误将java.lang.Object
作为方法参数。这的确是错误的。它必须是java.lang.String
,另请参见
您必须将song.getProperty()
的返回类型从Object
更改为String
:
public String getProperty(String name) {
// ...
}
或在(字符串)
上添加强制转换,如果它实际上是字符串
类型:
if (artist.startsWith((String) song.getProperty("Artist"))) {
// ...
}
或要使用FullWorth:
与
最后但并非最不重要的一点是,这个问题与JSP无关。在普通Java类中执行此操作时,您会遇到完全相同的问题。在JSP文件中编写Java代码是为了正确理解基本概念。错误表明它无法在
字符串类中找到给定的方法方法startsWith(Java.lang.Object)
。请注意,错误将java.lang.Object
作为方法参数。这的确是错误的。它必须是java.lang.String
,另请参见
您必须将song.getProperty()
的返回类型从Object
更改为String
:
public String getProperty(String name) {
// ...
}
或在(字符串)
上添加强制转换,如果它实际上是字符串
类型:
if (artist.startsWith((String) song.getProperty("Artist"))) {
// ...
}
或要使用FullWorth:
与
最后但并非最不重要的一点是,这个问题与JSP无关。在普通Java类中执行此操作时,您会遇到完全相同的问题。在JSP文件中编写Java代码是为了正确理解基本概念。谢谢!我把物体抛到绳子上。因为我无法更改这些方法,因为它们是google app engine API方法。但是你的回答确实帮助我解决了这个问题。谢谢!我把物体抛到绳子上。因为我无法更改这些方法,因为它们是google app engine API方法。但是你的回答确实帮助我解决了这个问题。
if (artist.startsWith(song.getArtist())) {
// ...
}