TRENDING NEWS

POPULAR NEWS

How Do I Exit This Java Program With Code

How do you exit a Java program?

You haven't specified what type of Java program but for the most straightforward one - not in a runtime environment like OSGI its a matter of just exiting from the main function.There are also methods in System and Thread which will cause a more abrupt exit.(Edit for phone keyboard induced spelling)

How to set up a loop for a java program.?

The easiest solution is to use a 'while' loop that will loop forever, then inside the loop check whether the user's input is the 'exit' string and if so, execute a 'break' statement to abandon the loop. Like this:

Scanner kbReader = new Scanner(System.in);
while (true) {
    System.out.print("Type in a sentence and press ENTER. ");
    String s = (kbReader.nextLine() + "x").toUpperCase(), sp[];
    if (s.equals("EXIT")) {
        break;
    }
    sp = s.split("SA|S\\s+A");
    int len = sp.length - 1;
    System.out.println("There are " + len + " occurences.");
}

Problem with java program?

its givng me this error when i complile my java program:
C:\Documents and Settings\Daniel\My Documents\KilowattApplet.java:19: ';' expected
^
1 error

Tool completed with exit code 1

here is my program:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KilowattApplet extends Applet implements ActionListener
{
//declare variables
int costKwhr, kwhours;
double average

//construct components
Label welcomeLabel = new Label("Welcome to the Appliance Energy Calculator");
Label costKwhrLabel = new Label("Please enter the cost per kilowatt-hour in cents: ");
TextField costKwhrField = new TextField(8);
Label hoursPerYearLabel = new Label("Please enter the kilowatt-hours consumed: ");
TextField hoursPerYearField = new TextField(8);
Button calcButton = new Button("Calculate");
Label outputLabel = new Label("Click the Calculate Button to display the average energy cost");

public void init()
{
setBackground(Color.green);
setForeground(Color.black);
add(welcomeLabel);
add(costKwhrLabel);
add(costKwhrField);
add(hoursPerYearLabel);
add(hoursPerYearField);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);

}

public void actionPerformed(ActionEvent e)
{

double costKwhr = Double.parseDouble(costKwhrField.getText...
double kwhours = Double.parseDouble(hoursPerYearField.get...
average = costKwhr * kwhours;
outputLabel.setText("The average annual cost to operate this appliance is $" + Math.round(average* 100)/100D);
}


}

Java Programming Help?

The code that you posted was a long way from compiling. A lot of things were missing such as Product and many other classes.

Also, it would be easier to help you if you posted a SSCCE

http://sscce.org

I believe that I was able to figure out the problem that you're having anyways. In your method of ProductMaintApp in the method updateProduct, you have two Product objects, one called "product" and the other called "p". The one called "product" is the existing one but it is being updated with the new values and the other product called "p" is the one that's passed back to the product list to be the new one.

You could get by with just one Product object in this update method. You could lookup the product object, like your do, ask the user for new field values, like you do, and set the new field values, like you do and the product back in the product list would be set with the new values because its the same object. It's still in the product list.

Does that make sense?

Programming Help cant find code!?

Write a program that displays a fast food menu. The last option should be exit. Your
program will calculate the total of all the food purchases. Also, get your program to ask
"Would you like fries with that?" before exiting, if they did not already buy fries.


It should look something like this when run.

---- HEART ATTACKS ON A BUN -----
1. The Big MOO Combo . . . 5.99
2. Big MOO . . . . . . . . 3.99
3. Spring Surprise . . . . 1.99
4. Fries . . . . . . . . . 1.29
5. Pop . . . . . . . . . . 1.19
6. Exit
---------------------------------

What would you like? 2
What would you like? 3
What would you like? 6
Would you like any fries with that [y/n] ? n
Your total is $5.98


The program is WingIDE and im doing this with python please help this is important!!

How do I make the Java program run multiple times?

I want to make my (simple) program run multiple times instead of finishing the program after one. Preferably without having to copy the base of the code multiple times. I am a COMPLETE beginner at Java and this is one of my first programs, so can you try to make it somewhat simple?

Here's my code:

import java.util.Scanner;
public class JavaClass {

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

//The key is to use the 'myScanner.something'//
System.out.println("How many dollars do you have?");
Scanner myScanner = new Scanner(System.in);
amount = myScanner.nextDouble();
amount = amount * 100;
/*Key here is to use 'print' instead of 'println'*/
System.out.print("You have ");
System.out.print(amount);
System.out.print(" cents.");


}
}

It just says "How many dollars do you have?" and you say a number, and it then says "You have xxx cents" depending on what the user said. How do I make it so they can put that dollar input in multiple times?

Thanks in advance,

From Jake

In Java programming, why do I need to use -1 to quit?

There is no need to use -1 to quit (return code 0 is normally used for normal termination). Java usually manages crashes fairly fine without any additional fault management. Since java runs on a virtual machine you will not affect any other systems on the computer. It will even provide its own error codes on crashes.However if you do not want the users, who uses your program as a command line tool, to stand there looking like fools at a crash you can provide an error code. This will help them to handle the crash. Troubleshooting can likely be done anyway, with error logs and error messages, but it is hard to automate this.Further, sometimes you may use a daemon to handle programs which needs to be stable. This daemon might also want to know why a program crashes to be able to take the right measures (a segmentation error in a java program, crashing after several days may for example be solved by a program restart).

Why do I get this error "Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1" in Gradle?

A non zero exit value denotes an error during the process. This could be due to some error in the code which is preventing it from building. Just open the console, re-run gradle build and check for the exception there. You will get an idea of where is the problem. If finding difficulty paste brief log.

Can we write a Java program without using the ‘main’ function?

Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:public class Foo { static { System.out.println("Message"); System.exit(0); } }The System.exit(0) lets the program exit before the JVM is looking for the mainmethod, otherwise the following error will be thrown:Exception in thread "main" java.lang.NoSuchMethodError: mainIn Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:The program compiled successfully, but main class was not found. Main class should contain method: public static void main (String[] args).Here an alternative is to write your own launcher, this way you can define entry points as you want.Source :How can you run a Java program without main method?

TRENDING NEWS