开发者

help with process method to find the length of a string

开发者 https://www.devze.com 2023-03-28 21:36 出处:网络
This is my song application /* * */ public class SongApp{ public static void main(String[] args){ Song song1 = new Song();

This is my song application

/* 
 *
 */

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();
  }
}

And this is my support class

/* 
 *
 */

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());
  }
}

So my Question is using the process method i need to print out the length of songLine and then produce an output for e.g Length is: 9. but my process method doesnt seem to work so far


You need to return a String from the process() method.

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

....or you can make it void:

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


Did you try to return something in the process method?


Your process method is basically a noop. Either return the results or store it in a member variable.

If you want to return the string length from process do:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号