How to exit as Sub in Java

In VB you would create this code:

Sub doitnow()
dim a = 1
   for a = 1 to 45
    if a = 5
      exit sub
    end if
    a = a +1
   next a
end sub

In Java how do you exit early? I have a program with settings and diferent ways to enter the program and if you enter without having the settings done, I wish to stop the activity and send them to the settings activity.

Currently I have:

try {
  Ls = new LoadSettings(Add.this);
  gs = new GlobalSubs();
} catch (Exception e) {
  Intent c = new Intent(this, Preferences.class);
  finish();
  startActivity(c);
}

But the same activity continues and does not stop creating a force stop error.

Answers


To exit a function early in Java, simply place return on the line where you want to exit:

for (int a = 1 ; a != 45 ; a++) {
    if (a == 5) {
        return;
    }
}

The equivalent of Exit Sub in Java is return. From the JLS:

A return statement returns control to the invoker of a method

For void methods simply use return on its own.


Need Your Help

How do I pass a textbox (DatePicker) variable to my controller using Html.ActionLink?

c# asp.net .net asp.net-mvc actionlink

I have a link that opens a pdf in a new window, without leaving the page. I had this link working...

Javascript how to convert sstring to date

javascript date

I need to write a script to update a time. I want to make it increase by 20 seconds so

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.