Jump to content

How can I get this to work? It's Java

Go to solution Solved by MrSuperb,


class test

{

public static String method (String arg)

{

switch(arg){

case "hello":

return "hello";

case "goodbye":

return "goodbye";

default:

return "Error";

}

}

public static void main(String[] args)

{

System.out.println(method("hello") + method("goodbye"));

}

}

class test{	public static String method (String arg)	{		switch(arg){			case "hello":	return "hello";				break;			case "goodbye":	return "goodbye";				break;			default: return "Error";				break;		}	}	public static void main(String[] args)	{		System.out.println(method(hello) + method(goodbye));	}}

This is copy/pasted from my code but with the variable names changed and stuff. It still gives me the same error "Cannot find symbol"

Link to comment
https://linustechtips.com/topic/212723-how-can-i-get-this-to-work-its-java/
Share on other sites

Link to post
Share on other sites

Needs quotes around the strings.

class test{	public static String method (String arg)	{		switch(arg){			case "hello":	return "hello";				break;			case "goodbye":	return "goodbye";				break;			default: return "Error";				break;		}	}	public static void main(String[] args)	{		System.out.println(method("hello") + method("goodbye"));	}}

I am gettig this now:

test.java:7: error: unreachable statement				break;				^test.java:9: error: unreachable statement				break;				^test.java:11: error: unreachable statement				break;				^test.java:13: error: missing return statement	}	^4 errors
Link to post
Share on other sites


class test

{

public static String method (String arg)

{

switch(arg){

case "hello":

return "hello";

case "goodbye":

return "goodbye";

default:

return "Error";

}

}

public static void main(String[] args)

{

System.out.println(method("hello") + method("goodbye"));

}

}

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to post
Share on other sites

class test{	public static String method (String arg)	{		switch(arg){			case "hello":					return "hello";			case "goodbye":					return "goodbye";			default: 				return "Error";		}	}	public static void main(String[] args)	{		System.out.println(method("hello") + method("goodbye"));	}}

So I don't need a break; because return exits it already?

Link to post
Share on other sites

So I don't need a break; because return exits it already?

 

yes. return always exits the current method.

 

you could also write something like this:

then you need the "break;"s

class test{	public static String method (String arg)	{        String result;		switch(arg){			case "hello":					result = "hello";				break;			case "goodbye":					result = "goodbye";				break;			default:				result = "Error";		}        return result;	}	public static void main(String[] args)	{		System.out.println(method("hello") + method("goodbye"));	}}

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to post
Share on other sites

 

yes. return always exits the current method.

 

you could also write something like this:

then you need the "break;"s

class test{	public static String method (String arg)	{        String result;		switch(arg){			case "hello":					result = "hello";				break;			case "goodbye":					result = "goodbye";				break;			default:				result = "Error";		}        return result;	}	public static void main(String[] args)	{		System.out.println(method("hello") + method("goodbye"));	}}

Is there a benefit to doing it this way?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×