First, I am running on linux and when i use the java command to run a program (i.e. java foo), it just sits after it finishes. how can I get it to fully close(specifically, the program I am posting at the end of this post)

Second, is there an equivalent to the return 0 in C++. i.e. input validation fails and you want to exit? I can't figure out how to do that.

here's the program I've written:

import javax.swing.*;

class Ch2Sample1
{
	public static void main(String[] args)
	{
	String name;
	while(true)
	{
	name = JOptionPane.showInputDialog(null, "What is your name?");
	if(name.length()!=0)
	break;
	else
	{
	JOptionPane.showMessageDialog(null, "Oops!  You didn't enter anything.\nPlease try again.");
	}
	}
	JOptionPane.showMessageDialog(null, "Your name is " + name);
	}
}

Recommended Answers

All 2 Replies

To exit the sytem you need to use the exit method on the System class.

So something like the following:

System.exit(0); // This exits the application without errors.

On a side note why are you using a break?

Just do the following:

import javax.swing.*;

class Ch2Sample1
{
  public static void main(String[] args)
  {
    String name;
    while(true)
    {
      name = JOptionPane.showInputDialog(null, "What is your name?");
      if(name.length()!=0)
      {
        JOptionPane.showMessageDialog(null, "Your name is " + name);
        // You can place your System.exit(0); here if you wish or you 
        // can add behavior that allows you to exit your while loop.
      }
      else
      {
        JOptionPane.showMessageDialog(null, "Oops!  You didn't enter anything.\nPlease try again.");
	}
    }
  }
}

Regards,

Nate

thanks. the reasone i'm using a break is because i have to do other things with the name after I get it from the user. this is simply the data input part of the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.