I am taking my high school AP Computer Science class.
I decided to throw a goto
statement into a one of our labs just to play around, but I got this error.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "goto", assert expected
restart cannot be resolved to a variable
at Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)
I went to a goto
question on Stackoverflow to find out how to do it properly, and I did exactly as was demonstrated in one of the answers. I really don't understand why the compiler wants an assert
statement (at least that's what I assume it wants), nor do I have any idea how to use assert
. It seems to want the restart part of goto restart;
to be a variable, but restart is just a label that pulls the program back up to line 10 so that the user can enter a valid int
. If it wants restart to be a variable, how do I do that?
import java.util.*;
public class Factorial
{
public static void main(String[] args)
{
int x = 1;
int factValue = 1;
Scanner userInput = new Scanner(System.in);
restart:
System.out.println("Please enter a nonzero, nonnegative value to be factorialized.");
int factInput = userInput.nextInt();
while(factInput<=0)
{
System.out.println("Enter a nonzero, nonnegative value to be factorialized.");
factInput = userInput.nextInt();
}
if(x<1)//This is another way of doing what the above while loop does, I just wanted to have some fun.
{
System.out.println("The number you entered is not valid. Please try again.");
goto restart;
}
while(x<=factInput)
{
factValue*=x;
x++;
}
System.out.println(factInput+"! = "+factValue);
userInput.close();
}
}
Java does not support
goto
, it is reserved as a keyword in case they wanted to add it to a later versionThe Java keyword list specifies the goto keyword, but it is marked as "not used".
This was probably done in case it were to be added to a later version of Java.
If goto weren't on the list, and it were added to the language later on, existing code that used the word goto as an identifier (variable name, method name, etcetera) would break. But because goto is a keyword, such code will not even compile in the present, and it remains possible to make it actually do something later on, without breaking existing code.
goto
doesn't do anything in Java.There is not 'goto' in the Java world. The main reason was developers realized that complex codes which had goto would lead to making the code really pathetic and it would be almost impossible to enhance or maintain the code.
However this code could be modified a little and using the concept of continue and break we could make the code work.
If you look up continue and break they accept a "Label". Experiment with that. Goto itself won't work.
Read more
As already pointed out by all the answers
goto
- a reserved word inJava
and is not used in the language.restart:
is called an identifier followed by a colon.Here are a few things you need to take care of if you wish to achieve
similar
behavior -I'm not sure of whether should I say
similar
as I already have.