6.6k views
3 votes
Write a method, getFirstLine, that is passed a String argument and that returns the first line. (Recall that lines are terminated with the "\\" character.) Assume that the argument contains at least one complete, newline-terminated line.

My Solution:
public String getFirstLine(String a)
{
String[] myLines = a.split("\\");
return(myLines[0]);
}

1 Answer

5 votes

Answer: The answer to this question can be given as:

Method definition :

String getSecondLine (String aString) //define function getSecondLine()

{

return aString.substring(aString.indexOf("\\") + 1, aString.indexOf("\\") + 1 + aString.substring(aString.indexOf("\\") + 1, aString.length()).indexOf("\\"));

//return value.

Step-by-step explanation:

User Martin Peck
by
5.8k points