-4

I am a beginner to Java and I wanted to make a small fun program just for me to play with. I am familiar with the goto command but it doesn't appear to work in my Java code. can someone please direct me to a substitute.

I know my code is missing a lot like else and such but please direct me to my main problem.

import java.util.Scanner;
 PROBLEM:     .begin1
class Game{
public static void main(String[] args){
    System.out.println("Welcome Master");
    Scanner user_input = new Scanner( System.in );

    String filesvar;
    System.out.print("Which files would you like to access?");
    filesvar = user_input.next();

    System.out.println("Accessing files please wait...");

    try {
        Thread.sleep(1000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }


    if (filesvar == "code");{
        Scanner code_input = new Scanner( System.in );
        Scanner code2_input = new Scanner( System.in );

        String codevar;
        System.out.print("Please type in code.");

        codevar = code_input.next();

        String codeanswer;
        System.out.println("Would you like to save your code?");
        codeanswer = code_input.next();
        if (codeanswer == "yes");{

            try {
                Thread.sleep(1000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("Code saved.");
            if (codevar == "no"){
                System.out.println("Very well.");
PROBLEM:                goto begin1

               }
           }


       }
   }
}
DrJava
  • 9
  • 3

1 Answers1

1

From the JLS: goto is one of those reserved keywords, which indicates that a normal Java program will not compile if this keyword is present.

One should not use goto anyway in Java - there are far, far better ways to accomplish anything that you're trying to do without it.

Makoto
  • 104,088
  • 27
  • 192
  • 230