-1

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.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
user3483844
  • 133
  • 1
  • 1
  • 13
  • nothing in main and it is wrong – PSR Sep 18 '14 at 07:52
  • 1
    @LionC it will compile, does not look correct at first sight but see that opening bracket after sum. That creates a code block, so code will compile. – Juned Ahsan Sep 18 '14 at 07:53
  • @JunedAhsan Ah thanbks, i missed the opening bracket hidden at the end of a line :-) – LionC Sep 18 '14 at 07:54
  • I suggest you [read some more](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html) about the Java language and how objects work. – MC Emperor Sep 18 '14 at 07:57

7 Answers7

2

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);
    }
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

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;
    }
}
Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Read the linked post. In short: *do not use initialization blocks* unless you know what they are. Since you do not know the difference between static/non-static yet either, simply follow the form above (*without* the initialization block and *without* the `new PrimeSum()` line creating a new instance) and put the code in the `sum` method, which is `static`. Also, the variables should be *local* to the `sum` method for now. – user2864740 Sep 18 '14 at 08:01
0

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.

codebot
  • 2,540
  • 3
  • 38
  • 89
0
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
0

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.
-1

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);
        }

    }
}
ARIJIT
  • 676
  • 5
  • 7
  • 2
    Consider explaining what OP did wrong, how you fixed it and why you fixed it that way, to make your answer more useful for OP and future visitors. – LionC Sep 18 '14 at 07:58
  • I the original code : bug1: the main method was empty bug2: private declare inside the main – ARIJIT Sep 18 '14 at 08:03
  • This code does not compile. Access modifiers (like private) cannot be used inside methods like this. – MC Emperor Sep 18 '14 at 08:04
  • @ARIJIT Do not explain it to us in the comments, formulate a nice explanation with it and edit your answer :-) – LionC Sep 18 '14 at 08:18
-2

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);
 }
}
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • Consider explaining what OP did wrong, how you fixed it and why you fixed it that way, to make your answer more useful for OP and future visitors. (Not my downvote btw) – LionC Sep 18 '14 at 07:57
  • Also, this code does not compile. Access modifiers (like `private`) cannot be used inside methods like this. – MC Emperor Sep 18 '14 at 08:02
  • @MCEmperor why not, why do I have to add static to them to make them work. What does that even mean? – user3483844 Sep 18 '14 at 08:31
  • @user3483844 Did your teacher not explain it? Ask your teacher if he can. – MC Emperor Sep 18 '14 at 09:07