Cannot find symbol : class
I'm sure this is simple, but it's the little things that make my brain pop still. Anyhow, the error is:
AddFractions.java:16: cannot find symbol
symbol : class Rational
location: class rationalpackage.AddFractions
Rational rTotal = new Rational(1, 2);
The code where this error is found is :
(Simple code, a loop to create rationals and add them together using Rationals.java)
Code:
package rationalpackage;
public class AddFractions
{
Rational rTotal = new Rational(1, 2);
long rSum = 0;
public static void main( String[] args )
{
for(int i = 1; i <= 20; i++)
{
int num = 1 + i;
int den = 2 + i;
Rational rToAdd = new Rational(num, den);
if(i = 1)
{
System.out.println(rTotal + " + " + rToAdd + " = " + rTotal.add(rToAdd));
}
else
{
System.out.println("Adding " + rToAdd + " to the previous sum of " + rTotal + " = " + rTotal.add(rToAdd));
}
}
}
}
Since it'll make a pretty little box for it, the code for Rationals.java, which I know to be fully working, is here :
(Pardon all the extra garble if it's relevant to you.)
Code:
package rationalpackage;
/**
* Creates a class to handle rational numbers
* @author Tim Farage
*/
public class Rational implements Comparable<Rational>
{
private long num; //an integer, positive or negative or zero
private long den; //a positive integer
/**
* Construct a default ctor for class Rational
*/
public Rational()
{
//create the fraction 0
num = 0;
den = 1;
}
//////////////////////////////////////////////////////////////////////
/**
* this ctor creates a Rational number from an long
* @param anInteger long
*/
public Rational( long anInteger )
{
num = anInteger;
den = 1;
}
//////////////////////////////////////////////////////////////////////
/**
* Construct a rational with specified n and d.
* It will be stored in simplest form, and the den
* will be positive.
* @param n long
* @param d long
*/
public Rational( long n, long d )
{
if ( d == 0 )
{
//this is not allowed so we'll just set the fraction itself
//to 0. However, the best thing to do here is to throw
//an exception.
num = 0;
den = 1;
return;
}
else if ( n == 0 ) //the fraction is 0 so set den to 1
{
num = 0;
den = 1;
return;
}
//get the greatest common divisor of n and d so
//we can store the fraction in simplest form
//Note that to get here neither n nor d is 0.
long gcd = gcd( n, d );
num = n / gcd;
den = d / gcd;
//If den is negative, multiply den and num by -1,
//so that there is no negative number on the bottom.
if ( den < 0 )
{
den *= -1;
num *= -1;
}
}
//////////////////////////////////////////////////////////////////////
/**
* copy ctor - can use this to copy the data from one Rational to another
* For example:
* Rational ra = new Rational( 3, 4 );
* Rational rb = new Rational( ra ); //rb will be another Rational
* object that is equal to 3/4.
* @param aRational Rational
*/
public Rational( Rational aRational )
{
num = aRational.num;
den = aRational.den;
}
////////////////////////////////
/**
* Uses a recursive form Euclid's gcd algorithm.
* @param n a non-zero long
* @param d a non-zero long
* @return the positive greatest common divisor of n and d. If either is 0,
* returns -1.
*/
public static long gcd( long n, long d )
{
if ( n == 0 || d == 0 )
{
return -1; //error code
}
//obtain the absolute values of n and d
d = Math.abs( d );
n = Math.abs( n );
long remainder = n % d;
//boundard condition of the recursion
if ( remainder == 0 )
{
return d;
}
else
{
return gcd( d, remainder );
}
} // end gcd( )
//////////////////////////////////////////////////////////////////////
/**
* @return long the numerator of the fraction
*/
public long getNumerator()
{
return num;
}
///////////////////////////////////////////////////////////////////////
/**
* @return long the denominator of the fraction
*/
public long getDenominator()
{
return den;
}
//////////////////////////////////////////////////////////////////////
/**
* Add a rational number to this rational
* @param secondRational Rational
* @return Rational
*/
public Rational add( Rational secondRational )
{
long n = num * secondRational.den + den * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* Subtract a rational number from this rational
* @param secondRational Rational
* @return Rational
*/
public Rational subtract( Rational secondRational )
{
long n = num * secondRational.den - den * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* multiply a rational number by this rational
* @param secondRational Rational
* @return Rational
*/
public Rational multiply( Rational secondRational )
{
long n = num * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* divide a rational number by this rational
* @param secondRational Rational
* @return Rational
*/
public Rational divide( Rational secondRational )
{
long n = num * secondRational.den;
long d = den * secondRational.num;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* return this Rational number as a decimal
* @return double
*/
public double toDouble()
{
return (double)num / (double)den;
}
//////////////////////////////////////////////////////////////////////
/**
* Override the toString() method
* @return String
*/
@Override
public String toString()
{
return num + "/" + den;
}
//////////////////////////////////////////////////////////////////////
/**
* Override the equals method in the Object class
* @param aRational Object
* @return boolean
*/
@Override
public boolean equals( Object aRational )
{
if ( num == ( (Rational)aRational ).num
&&
den == ( (Rational)aRational ).den )
{
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////////////////
/**
* Override the compareTo method in java.lang.Comparable
* @param secondRational Rational
* @return long
*/
@Override
public int compareTo( Rational secondRational )
{
Rational difference = this.subtract( secondRational );
if ( difference.getNumerator() > 0 )
{
return 1;
}
else if ( difference.getNumerator() < 0 )
{
return -1;
}
else
{
return 0;
}
} // end compareTo()
} // end class Rational
Help as to why this isn't working for me would be much appreciated!
Re: Cannot find symbol : class
First of all, you should post the actual code you're working on. Trying to compile what you posted, I get the following:
Code:
AddFractions.java:17: incompatible types
found : int
required: boolean
if(i = 1)
^
AddFractions.java:19: non-static variable rTotal cannot be referenced from a static context
System.out.println(rTotal + " + " + rToAdd + " = " + rTotal.add(rToAdd));
^
AddFractions.java:19: non-static variable rTotal cannot be referenced from a static context
System.out.println(rTotal + " + " + rToAdd + " = " + rTotal.add(rToAdd));
^
AddFractions.java:23: non-static variable rTotal cannot be referenced from a static context
System.out.println("Adding " + rToAdd + " to the previous sum of " + rTotal + " = " + rTotal.add(rToAdd));
^
AddFractions.java:23: non-static variable rTotal cannot be referenced from a static context
System.out.println("Adding " + rToAdd + " to the previous sum of " + rTotal + " = " + rTotal.add(rToAdd));
^
Your error is probably a class path issue. Your Rational class isn't on the default classpath the runtime is using. Take a look at this for some guidance.
Also note that since you never save the result of rTotal.Add() anywhere, the running total you're looking for (I think) will be lost.
Re: Cannot find symbol : class
Quote:
Originally Posted by
Kediil
(Simple code, a loop to create rationals and add them together using Rationals.java)
Help as to why this isn't working for me would be much appreciated!
I'm guessing because your post wasn't entirely clear, but if your Rational class is in a file called Rationals.java, it won't work. A public class must be in a file with the same name as the class.
Unless in communicating with it [a computer] one says exactly what one means, trouble is bound to result...
A. Turing
Re: Cannot find symbol : class
The files are all saved in the same folder and the path is set correctly. I have other multi-class programs that work fine.
Re: Cannot find symbol : class
Quote:
Originally Posted by
dlorde
I'm guessing because your post wasn't entirely clear, but if your Rational class is in a file called Rationals.java, it won't work. A public class must be in a file with the same name as the class.
I'm not sure how to make it clear or clearer. I barely understand this stuff as it is. This is a forced area of my major, my tutor went AWOL and I'm struggling to get this finished with barely a clue how it works much less how to fix it.
Assuming I'm understanding what you're saying correctly. Having it named "Rational.java" is what the problem is, but .java is just the file type. It is called "Rational". I'm not really sure what I can do to be clearer, but I would like to know if you're able to help on that as well.
Re: Cannot find symbol : class
Quote:
Assuming I'm understanding what you're saying correctly. Having it named "Rational.java" is what the problem is,
No, dlorde was pointing out that your class is called Rational but in your text you called it Rationals.java (ie with a trailing 's'). If this was a typo then it's not a problem but if your file name does have an additional 's' then it is a problem. The file name must be identical to the class name + '.java'.
Can you compile the Rational.java file?
If it compiles without any warnings/errors then this class is ok.
Quote:
I'm not really sure what I can do to be clearer,
Well you could start by posting the code you are compiling, as ajhampson pointed out, the code posted here has a different set of compilation errors.
Also please post all the errors/warning messages the compiler is emitting.