简体   繁体   中英

Java Program terminated in eclipse without running

I'm new to Java and I'm just trying to run a simple program using eclipse that takes numbers that are factors of 3 or 5 through 0 to 1000 and adds them all together. I try to run the code, but the program just terminates after a second of running and displays nothing on my console. Here's my code.

public class PrimeSum {


    public static void main(String args[]){

    }
    private double Num= 0.0;
    private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }

    }

    System.out.println("The total is "+ sum);
}

Can someone tell me why this is, I've been searching for these past 2 hours and come up with nothing.

Your main method is empty. So nothing happens:

public static void main(String args[]){

}

Probably you want to create a method but you just created a code block here:

private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }

    }

Now even this code may work once you create an object of your class in main method. Because this code block will execute on object creation.

I can't explain all the basics in this answer about code structure. But this is maybe what you want:

public class PrimeSum {

    public static void main(String args[]){
    PrimeSum obj =  new PrimeSum(); // creating an instance of your class will trigger the instance code block
    }
    private double Num= 0.0;
    private double sum = 0.0;{

    for(int i=0;i<1001;i++){
        Num = i;
        if(i % 3 == 0.0){
            sum += i;
        if(i % 5 == 0.0){
            if(i % 3 != 0.0){
                sum += i;
            }
        }
        }
    }   
    System.out.println("The total is "+ sum);
    }
}

Nothing is displayed because the looping code and println doesn't run . The construct used is an instance initialization block . However, an instance of the PrimeSum class is never created - hence the block never executes .

The simple fix is to move the code into the main method which is executed. (Note that it is static so it can be called without an instance being created.)

Consider:

public class PrimeSum {

    public static void main(String args[]){
        System.out.println("Hi, in main!");
        // Now create instance, run initialization block..
        new PrimeSum();
        // .. but really, just put the code in main, or better,
        // a method called from main ..
        System.out.println("Sum is " + calculateSum());
    }

    /* private double sum = 0.0; <-- note newlines added here for clarity */

    {
       // This is an instance initialization block, it does NOT run
       // until/when an instance is created.
       // (The original never ran code in here, because an instance was
       //  never created.)
       System.out.println("Hi, in instance initialization block!");
    }

    static double calculateSum() {
        // Do math, return value
        return 42;
    }
}

You don't have any coding in your main method. So I think you are expecting for a visible output on console. If you need to see your result in a console, You should add a System.out.println() to your code.

import java.sql.*;
public class JDBCConnect 
{

public static void main(String[] args) {

Connection con;
Statement st;
ResultSet rs;
int no;
String nm,typ;
double bal;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@217.212.7.169:1521:cashutv3","cash_test","cash_test");
st=con.createStatement();
rs=st.executeQuery("select * from accounts");

while(rs.next())
{
no=rs.getInt("accno");
nm=rs.getString("accnm");
typ=rs.getString("acctype");
bal=rs.getDouble("balance");
System.out.println("account no is "+no);
System.out.println("Name is "+nm);
System.out.println("account type is "+typ);
System.out.println("balance is "+bal);
}
con.close();
}

catch(Exception e)
{
System.out.println(e);
}

}


    }

// program got terminated and its displaying the path of javaw.exe file

if you have imported some packages in your projects so there may be a possibility of expiring the same. and there are two solutions:

  1. change your local system date
  2. download the package again with a new user id. I hope it will help.

Here is the modified code :

public class PrimeSum {
  public static void main(String args[]) {

        double Num = 0.0;
        double sum = 0.0;
        {

            for (int i = 0; i < 1001; i++) {
                Num = i;
                if (i % 3 == 0.0) {
                    sum += i;
                    if (i % 5 == 0.0) {
                        if (i % 3 != 0.0) {
                            sum += i;
                        }
                    }
                }

            }

            System.out.println("The total is " + sum);
        }

    }
}

Try this:

public class PrimeSum {

 public static void main(String args[]) {

    private double Num= 0.0;
    private double sum = 0.0;

    for(int i=0;i<1001;i++) {
        Num = i;
        if(i % 3 == 0.0) {
            sum += i;
            if(i % 5 == 0.0) {
                if(i % 3 != 0.0) {
                   sum += i;
                }
            }
        }
    }

    System.out.println("The total is "+ sum);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM