Question: ACTIVITY 4 . 4 . 2 : Write a method with a single return statement. [ ? ] Full screen Some programmers dislike return statements

ACTIVITY
4.4.2: Write a method with a single return statement.
[?] Full screen
Some programmers dislike return statements in the middle of a method. It is always possible to restructure a method so that it has a single return statement. Rearrange the following lines of code to produce such a version of the firstspace method of the preceding exercise.
How to use this tool v
Unused
char ch = str. charAt(i);
int position =-1;
return position;
{
{
}
boolean found = false;
public static int firstSpace(String str)
for (int i=0; ! found &&i str. length(); i++)
position =i;
{
}
FindSpaces.java Load default template...
import java.util.Scanner;
public class FindSpaces {
public static void main (String [] args){
Scanner in = new Scanner(
System.in);
String inputStr = in. nextLine();
int firstSpacePosition = firstSpace (inputStr);
System.out.println(firstSpacePosition);
}
}
 ACTIVITY 4.4.2: Write a method with a single return statement. [?]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!