0

I'm practicing Java and I've run into a little odd issue or should I say issues.

When I compile the below code it says java line # :error:

missing return statement.

If I remove the comment where I have return "not valid", it compiles.

Now this is where the other issue comes in. When I pass the init_config and final_config string that I input as A and B, it returns the not valid. But when I pass "A" and "B" to other function (other("A", "B") the specific return which is "C" is returned/printed.

I am not sure if the the issue lies with my input method. The data I input for my init_config and final_config should be string values, correct? I'm not sure if Scanner is a good string input method. However, if I print the two inputs it works fine so I'm not sure if it is data loss or the string reference is lost when it is passed.

I also tried replacing init_config = in.next() with init_config = in.nextLine() but it did not make any difference.

Is it necessary to compile the code with the return "not valid" at the end of the function or can I bypass this by some method? And how can I pass String data using Scanner input method without any loss?

Here is my code:

import java.util.Scanner;
public class towerGen
{
    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);
        String init_config, final_config;

        System.out.print("Enter initial configuration: ");
        init_config = in.next();

        System.out.print("Enter final configuration: ");
        final_config = in.next();

        System.out.print(other(init_config, final_config));

    }

    public static String other(String src, String dest)
    {
        if (src=="A" && dest=="B") 
            return "C";
        if (src=="B" && dest=="A")
            return "C";
        if (src=="B" && dest=="C")
            return "A";
        if (src=="C" && dest=="B")
            return "A";
        if (src=="A" && dest=="C")
            return "B";
        if (src=="C" && dest=="A")
            return "B";
    //return "not valid";
    }
}
Maljam
  • 6,244
  • 3
  • 17
  • 30
mickey4691
  • 9
  • 2
  • 7

2 Answers2

1

Is it necessary to compile the code with the return "not valid" at the end of the function?

Yes, it is. Because the compiler recognizes that if the input strings do not meet any of the conditions you listed, there is nothing to return, which is an error in a non-void method.

Also, in your other method, you should use src.equals("A") and not src == "A" when comparing Strings.

Maljam
  • 6,244
  • 3
  • 17
  • 30
  • That's good. I did not know about the .equals() I was searching all day but maybe my terms were incorrect. Thanks. – mickey4691 Apr 02 '16 at 07:29
0

I think you pretty much got the answer from the above discussion i.e.,

Use equals method instead of ==. Because == checks if the both the object references are referring to the same object and not the contents inside it.

Would like to answer why it was working when you called the function other ("A", "B"), as it brings into the picture the concept of String pool, which is worth noting.

But when I pass "A" and "B" to other function (other("A", "B") the specific return which is "C" is returned/printed.

When we create a String object without new operator (aka String literal), it uses the String pool to get that object. If not present, it will create on and puts it in the string pool. String pool is shared. And if we use new operator it will always create a new String object and doesn't refer the one in pool even if one exists.

When you called other("A", "B"), the arguments here are pointing to the string objects in the string pool. And subsequently in the other(..) method when you performed a check like src=="A" it returns true because both src and "A" are referring to the same string object in the pool.

To test this you can try with System.out.print(other(new String("A"), "B")); and this would once again return not valid.

You can refer to java string with new operator and a literal for more information.

Community
  • 1
  • 1