James
James

Reputation: 23

Trying to correct a program, can't even get it to run

I'm using Netbeans, and trying to get a program "Rational" to run, so that I can see what needs to be fixed. However, when I try to run it, I get the error "Rational.Main class wasn't found in Rational project". I've tried renaming several aspects of the program to make it see the main class (it is there, I assure you), but it still gives this error. I've seen it before, but this is the only time it hasn't seemed to fix itself in time.

Edit: This is more problematic than I thought, here's the updated code. Yes, it's very wrong.

 package Rational;

  public class Rational {
        int x, y;



       public Rational () {
        this.x = 0;
        this.y = 0;
    }

    public static void printRational (Rational x) {
        System.out.println (x);
    }
    public Rational (int x, int y) {
        this.x = x;
        this.y = y;
    }
    public static void negate (int x) {
        x = -x;
        System.out.println (x);
    }
    public static void invert (int x, int y) {
        int g = x;
        x = y;
        y = g;
        System.out.print (x);
        System.out.print ("/");
        System.out.println (y);
    }
    public static void toDouble (int x, int y) {
        double f = x/y;
        System.out.println (f);
    }
    public static int GCD(int a, int b)
{
   if (b==0) return a;
   return GCD(b,a%b);
}
    public static void reduce (int x, int y) {
        x = x/(GCD (x,y));
        y = y/(GCD (x,y));
        System.out.print (x);
        System.out.print ("/");
        System.out.println (y);
    }
    public static void add (int x, int y) {
        double z = x+y;
        System.out.println (z);
    }






public static void main(String args[]) {
     Rational g = new Rational ();{
g.x = 1;
g.y = 2;
System.out.println ("vgds");
//Rational.printRational (g);

      }
}
}

Updated screenshot:

https://i.sstatic.net/SX55P.png

Upvotes: 0

Views: 159

Answers (3)

Rhymes
Rhymes

Reputation: 171

May be you wanted something like this:

package Rational;

public class Rational {
int x, y;

public Rational() {
    this.x = 0;
    this.y = 0;
}

public static void print(Rational r)
{       
    System.out.println("" + (r.x + r.y));
}

// other stuff

public static void main(String args[]) {
    Rational g = new Rational();
    g.x = 1;
    g.y = 2;

    Rational.print(g);


}
}

Upvotes: 0

toadaly
toadaly

Reputation: 667

In addition to the file name problem the previous answer alludes to, you also need to make the class Rational public, and you're probably going to have problems if you make the package name the same as the class name.

The filename should be Rational.java

The signature should be:

public class Rational

The package name should not also be Rational. You can use lower case 'rational' if you like.

Upvotes: 0

Rhymes
Rhymes

Reputation: 171

Your class name is Rational but your file name is Main.java

Just make them same your problem will be solved.

For any public class in java the filename and the class name should be same and also, a file can contain only one public class.

Upvotes: 4

Related Questions