Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Cannot Find symbol error! On new Object

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot Find symbol error! On new Object

    In my program for adding subtractin multiplying and dividing fractions the RationalTest.java file wont complie, the Fraction.java does complie

    Any help would be appreciated thanks!

    package rationaltest;
     
    import java.util.Scanner;
     
    public class RationalTest {
     
    	public static void main (String args[]) {
     
    		Scanner input = new Scanner(System.in);
    		int Choice, Num1, Num2, Den1, Den2;
     
    		System.out.printf("Enter numerator 1:");
    		Num1 = input.nextInt();
    		System.out.printf("Enter denominator 1:");
    		Den1 = input.nextInt();
    		System.out.printf("Enter numerator 2:");
    		Num2 = input.nextInt();
    		System.out.printf("Enter denominator 2:");
    		Den2 = input.nextInt();
     
                    Fraction f1 = new Fraction(Num1, Den1);
                    Fraction f2 = new Fraction(Num2, Den2);
                    Fraction result = new Fraction(1,1);
                    char operation = ' ';
     
                    System.out.printf("Enter precision:");
    		int Prec = input.nextInt();
     
    		do{			
    		    System.out.printf("\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\nChoice:");
    		    Choice = input.nextInt();
                        switch (Choice) {
                            case 1: operation = '+'; result = f1.Add(f2); break;
                            case 2: operation = '-'; result = f1.Subtract(f2); break;
                            case 3: operation = '*'; result = f1.Mult(f2); break;
                            case 4: operation = '/'; result = f1.Div(f2); break;
                        }
                        System.out.printf("%s %c %s = %s (%s)\n",f1,operation,f2,result,
                                          result.asDecimal(Prec));	
     
    		} while(Choice != 5);
    	}
    }

    package rationaltest;
     
    public class Fraction {
    	private int Numerator;
    	private int Denominator;
     
            public Fraction() {
                Numerator = 0;
                Denominator = 1;
            }
     
            public Fraction(int NumValue, int DenValue){
    		if (DenValue == 0){ 
                        System.out.println("!!Error: denominator Zero, a default rational 0/1 is stored instead.");
                        Numerator = 0;
                        Denominator = 1;
    		}
    		else {
                        Numerator = NumValue;
                        Denominator = DenValue;
                        int divider = gcd(Numerator > 0 ? Numerator : -Numerator,
                                          Denominator > 0 ? Denominator : -Denominator);
                        if (divider > 1) {
                            Numerator /= divider;
                            Denominator /= divider;
                        }
    		}
    	}
     
            private static int gcd(int a,int b) {
                if (b==0) return a;
                return gcd(b,a%b);
            }
     
            public Fraction Add(Fraction x){
                return new Fraction(this.Numerator*x.Denominator+x.Numerator*this.Denominator,
                                    this.Denominator*x.Denominator);
            }
     
            public Fraction Subtract(Fraction x) {
                return new Fraction(this.Numerator*x.Denominator - x.Numerator*this.Denominator,
                                    this.Denominator*x.Denominator);
            }
     
            public Fraction Mult(Fraction x) {
                return new Fraction(this.Numerator*x.Numerator,this.Denominator*x.Denominator);
            }
     
            public Fraction Div(Fraction x) {
                return new Fraction(this.Numerator*x.Denominator,x.Numerator*this.Denominator);
            }
     
            public String toString() {
                return String.format("%d/%d", this.Numerator, this.Denominator);
            }
     
            public String asDecimal(int Prec) {
                return String.format("%." + Prec + "f\n",(((double) getNum()) / ( (double) getDen())));
            }
     
    	public void setNum(int NumValue){ Numerator = NumValue; }
     
    	public void setDen(int DenValue){ Denominator = DenValue; }
     
    	public int getNum(){ return Numerator; }
     
    	public int getDen(){ return Denominator; }	
     
    }









    Here are all the errors hope this helps

    [jrauscher]$ javac RationalTest.java
    RationalTest.java:25: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction f1 = new Fraction(Num1, Den1);
                    ^
    RationalTest.java:25: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction f1 = new Fraction(Num1, Den1);
                                      ^
    RationalTest.java:26: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction f2 = new Fraction(Num2, Den2);
                    ^
    RationalTest.java:26: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction f2 = new Fraction(Num2, Den2);
                                      ^
    RationalTest.java:27: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction result = new Fraction(1,1);   
                    ^
     
    RationalTest.java:27: cannot find symbol
    symbol  : class Fraction
    location: class RationalTest.RationalTest
                    Fraction result = new Fraction(1,1);      
                                          ^
     
    6 errors


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Cannot Find symbol error! On new Object

    try using the -d option with javac to tell it where the root of the output class path is:

    javac - Java programming language compiler

    . should be okay if you don't mind your classes being output to ./rationaltest

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot Find symbol error! On new Object

    I couldn't get javac -d to work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,608
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Cannot Find symbol error! On new Object

    Describe where the source files are located and where you are issuing the javac command.
    With the classes in packages, the classpath must point to the folder containing the package folder.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Cannot Find symbol error! On new Object

    Quote Originally Posted by jtrtoday View Post
    I couldn't get javac -d to work.
    You could alternatively comment out the 'package' statement at the top of your files. Your problem is caused by Java's package<->filepath mapping. When you compile Fraction.java the way you're doing it, you're getting a Fraction.class in the working directory, right? When you compile your main method class, javac is attempting to resolve references to 'Fraction' by looking in the same package you declare your main method class to be in: rationaltest. To find the class files necessary to check method names etc against, javac looks in <classpath>/rationaltest. You haven't specified your classpath, so it's looking in ./rationaltest - which I'm guessing does not yet exist.

    You could work around the problem with
    javac -d . Fraction.java
    javac -d . RationalTest.java

    or (maybe, untested, but I'd be surprised if it didn't work)

    javac Fraction.java RationalTest.java

    Norm is right - I doubt there's anyone contributing here who doesn't explicitly set their classpath one way or another. My build files always do, with a line such as:
    javac -d $CLASSROOT -cp $CLASSROOT:$JARS <filespec-for-sources>
    Last edited by Sean4u; February 15th, 2012 at 03:06 AM. Reason: spelled 'javac' wrong

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot Find symbol error! On new Object

    Thanks you very much everyone I got it fixed

Similar Threads

  1. [SOLVED] cannot find symbol error
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 08:57 AM
  2. Cannot find symbol ERROR
    By yacek in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 21st, 2011, 11:39 PM
  3. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  4. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 01:50 PM
  5. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 01:36 AM