Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

Newbie question on stopping an application

Thread view: 
Cardinal - 28 Nov 2005 12:38 GMT
I have a simple console application that prompts the user to enter two
numbers and then the program multiplies these two numbers and spits out
the product. No problem so far. The difficulty I'm having is that if
someone enters a negative number, I want the program to reply something
like "Does not accept negative numbers!" then stop. I tried using
"break;" but it generated an error message and said that I was using it
"outside the loop" or something like that. I would be grateful if
someone could give me a clue on what I might use for this. Thank you
very much. My code is below.

public class Kilowatt
{
    private double costKiloWattHours;  //example 8.42 which means cents
    private double hoursUsedPerYear;   //example 653
    private double annualCost;

    public void setCost(double cost)
    {
        if (cost <= 0)
        {
            //I want the program to say "You can't enter a negative number."
            //Then the program should stop execution.
        }

        else
        {
            costKiloWattHours = cost / 100;
        }

    }

    public void setHours(double hours)
    {
        hoursUsedPerYear = hours;
    }

    public double getAnnualCost()
    {
        annualCost = costKiloWattHours * hoursUsedPerYear;
        return annualCost;
    }
}

import java.util.Scanner;

class Kilowatt_Test
{

    public static void main(String[] args)
    {
        double myCost;
        double myHours;

        Scanner myScanner = new Scanner(System.in);
        System.out.print("What's the cost per kilowatt hour? ");
        myCost = myScanner.nextDouble();

        Kilowatt myKilowatt = new Kilowatt();
        myKilowatt.setCost(myCost);

        System.out.print("How many kilowatt-hours are used per year? ");
        myHours = myScanner.nextDouble();
        myKilowatt.setHours(myHours);

        System.out.println("The annual cost is: " +
myKilowatt.getAnnualCost());
       
    }   
}
Thomas Fritsch - 28 Nov 2005 12:57 GMT
[...]
> The difficulty I'm having is that if
> someone enters a negative number, I want the program to reply something
> like "Does not accept negative numbers!" then stop. I tried using
> "break;" but it generated an error message and said that I was using it
> "outside the loop" or something like that.
"break" is for exiting a loop, not for exiting the whole application.
> I would be grateful if
> someone could give me a clue on what I might use for this. Thank you
> very much. My code is below.
[...]

>   if (cost <= 0)
>   {
>     //I want the program to say "You can't enter a negative number."
      System.out.println("You can't enter a negative number.");
>     //Then the program should stop execution.
      System.exit(1);
>   }
[...]

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Cardinal - 28 Nov 2005 16:39 GMT
Thanks very much.
Worked perfectly.
Exactly what I was looking for.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.