帮助使用process方法查找字符串的长度

时间:2011-08-17 11:12:46

标签: java

这是我的歌曲应用

/* 
 *
 */

public class SongApp{
  public static void main(String[] args){
  Song song1 = new Song();
  song1.setsongLine("While my guitar gently weeps.");
  song1.display();
  song1.process();
  Song song2 = new Song();
  song2.setsongLine("Let it be");
  song2.display();
  }
}

这是我的支持班

/* 
 *
 */

public class Song{
  // data field declaration
  private String songLine;


  /*sets the value of the data field songLine to input parameter value*/
  public void setsongLine(String songLine){      
    this.songLine = songLine;   
  } //end method  

 /*returns the value of the data field songLine */
 public String getsongLine(){      
    return songLine;      
 } //end method


  //method called process
  public String process(){
    int stringLength = songLine.length();
  }
  /*displays formatted songLine information to the console window */  
  public void display(){     
   System.out.println(songLine); 
   System.out.println("Length is :" + process());
  }
}

所以我的问题是使用我需要打印出songLine长度的处理方法,然后产生一个输出,例如,长度为:9。但我的处理方法似乎无法工作到目前为止

3 个答案:

答案 0 :(得分:2)

您需要从String方法返回process()

public static void main(String[] args){
    ...
    System.out.println(song1.process());
    ...
}
public String process(){
    return "Length is " + songLine.length();
}

....或者你可以void

public void process(){
    System.out.println("Length is " + songLine.length());
}

答案 1 :(得分:0)

您是否尝试在流程方法中返回某些内容?

答案 2 :(得分:0)

你的处理方法基本上是一个noop。 返回结果或将其存储在成员变量中。

如果要从进程中返回字符串长度:

public Integer process() {
    return songLine.length();
}