rishanth chavali
rishanth chavali

Reputation: 115

Return statement not working in java?

Hello i'm trying to print a sequence of characters using a method of return type "String".Below mentioned is the logic .The error which i'm getting is:

 prog1.java:34: error: missing return statement
  }
  ^
 1 error

Below mentioned is the code.it should print characters ..i'm able to do with type "void" but i'm not able to do with return type "int".Any suggestions would be highly appreciated.

import java.util.*;

class prog1 
{

    public static void main(String[] args) 
    {
        double height;
        double width;
        String fillcharac;
        Scanner sc = new Scanner(System.in);
        System.out.println("enter a value for height");
        height = sc.nextDouble();
        Scanner sc1 = new Scanner(System.in);
        System.out.println("enter a value for width:");
        width = sc.nextDouble();
        Scanner sc2 = new Scanner(System.in);
        System.out.println("enter the character value:");
        fillcharac = sc2.next();
        String x = print(height, width, fillcharac);
        System.out.println(x);
    }

    public static String print(double height, double width, String fill) 
    {
        for (double i = 1; i <= height; i++) 
        {
            for (double j = 1; j <= width; j++) 
            {
                return fill;
            }
        }

    }
}

Upvotes: 1

Views: 8798

Answers (4)

DavidPostill
DavidPostill

Reputation: 7921

It seems your inner for loop should be "building" an string that is returned after the loops have finished.

Perhaps the method should be named build instead of print as the printing is done later?

Something like:

public static String build( double height, double width, String fill ) {
  String result;
  for( double i=1; i<=height; i++) {
    for(double j=1; j<=width; j++) {
      // Build your string here using i, j and fill
      // result = ...
      }
    }
  return result;
  }

Then you can do:

//  Build the output string
x = build( height, width, fillcharac );

//  Print the output string
System.out.println( x );

Upvotes: 2

micker
micker

Reputation: 928

As sol4me said, you are missing the return statement within the print() method. Java is protecting you from having possible executions that would miss the return statement entirely. e.g.

print(0, 5, fillcharac);

In this case, the outer loop would never execute and the method would immediately exit, but Java would have no idea what the method should return.

As a side note... why have loops if you are just returning on the first iteration? Perhaps a logical error? The height and width parameters have no effect on the execution of the code (assuming they are >=1). The whole print method could be rewritten as:

public static String print( String fill)
{
     return fill;
}

With a method this simple, there isn't a point to having a method at all.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

You need to return something in the case that return fill; is never executed. That can happen if height or width is less than 1.

In this case, it doesn't make sense to enter a for loop (or even 2 loops) if all you're going to do is return fill. If you're going to do something else, add that code in the loop. Either way, you must add another return statement at the bottom of the method to satisfy the compiler, so it knows that something is returned in all cases.

Upvotes: 2

sol4me
sol4me

Reputation: 15698

You are missing return statement in print method . In order to correct it just return some string value for.e.g.

public  static  String print( double height,double width, String fill)
{
 for(double i =1;i<=height;i++){
 for(double j=1;j<=width;j++){

  return fill;
  }
 }
    return fill;
 }

Upvotes: 2

Related Questions