0

here is my entire code. now, the only problem I have stumbled upon is in the last method, getRow, where i have to extract a row from an array of string. could anyone please help me with this, i've been stuck for hours, not knowing why it calls "missing return statement", when i clearly do it -.-' thank you in advance

import java.util.ArrayList;

public class Photograph
{
    
    private String[] photograph;
    
    public Photograph(String[] photograph)
    {
        if (photograph == null)
            photograph = new String[] {""};
        else this.photograph = photograph;
    }

    public int getWidth()
    {
        return (photograph[0]).length();
    }

    public int getHeight()
    {
        return photograph.length;
    }
   

    /**
     * @null if the index is out of bounds.
     */
    public String getLine(int index)
    {
        if (index < 0 || index >= getHeight())
            return null;
        return photograph[index];
    }
    
    public String getRow(int index)
    {
        if (index < 0 || index >= getWidth())
            return null; 
        for (int row = 0; row < getHeight(); row++) 
             return photograph[row].substring(index, index + 1);  
    }
    
}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by Mehnad
Featured Replies
  • 1
    Ezzaral 2,714   5 Years Ago

    Both of your returns are in conditional statements. The compiler requires a guaranteed return value for all possible conditions. Add a default return value at the end to cover the case of your other conditions not being satisfied. Read More

  • 1
    Ezzaral 2,714   5 Years Ago

    getLine has a guaranteed return even if the if() statement is false. getRow() has two returns but one is in if() and the other in for(). You need a return in case those do not execute. Read More

1

Both of your returns are in conditional statements. The compiler requires a guaranteed return value for all possible conditions.

Add a default return value at the end to cover the case of your other conditions not being satisfied.

0

thank you very much for the quick response! but i dont think i completely understand what you mean.. to me the getLine and getRow methods function in principally the same way, and that's why i don't see why it wouldn't work..

1

getLine has a guaranteed return even if the if() statement is false.

getRow() has two returns but one is in if() and the other in for(). You need a return in case those do not execute.

0

getLine has a guaranteed return even if the if() statement is false.

getRow() has two returns but one is in if() and the other in for(). You need a return in case those do not execute.

Ahh, thank you very much, now i get it!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.