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 4 of 4

Thread: Error of "class or interface expected" at Mylong class for problem of API's

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Error of "class or interface expected" at Mylong class for problem of API's

    Wondering if anyone can help with problem of API's

    compiler issue found at MyLong class imports error says 'class' or 'interface' expected

    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
     
    public class PGM4
    {
      public static void main(String[] args) throws IOException
      {
        String fname; int temp;
        MyLong nums [];
     
        Scanner kb = new Scanner(System.in);
        System.out.println("How many MyLong objects do you want to have? " 
                          + "\nAn input of 30 or more is suggested.");
        temp = kb.nextInt();
        nums = new MyLong[temp];
     
        System.out.print("Enter the output filename: ");
        Scanner file1 = new Scanner(System.in);
        fname = file1.nextLine();
        FileWriter fwriter = new FileWriter(fname);
        PrintWriter myOutputFile = new PrintWriter(fwriter);
        PrimeChecker pCheck = new PrimeChecker();
     
        for(int i = 0; i < nums.length; i++)
        { nums[i] = new MyLong();
          nums[i].fillPrimeInfo();
        }
        myOutputFile.println("Mixed Numbers: ");
        for(int i = 0; i < nums.length; i++)
        { 
          myOutputFile.print("\n" + nums[i].getNum());
          if(pCheck.primeOrNot(nums[i].getNum())== 1)
          {
            myOutputFile.println(" is a prime number.");
            myOutputFile.println("The largest prime number before " + 
                                 nums[i].getNum() + " is " + 
                                 nums[i].getNextSmallerPrimeNumber());
            myOutputFile.println("The largest prime number after " + 
                                 nums[i].getNum() + " is " + 
                                 nums[i].getNextLargerPrimeNumber());
          }
          else if(pCheck.primeOrNot(nums[i].getNum())!= 1)
          {
            myOutputFile.println(" is not a prime number." +
                                 " It is divisible by: " +
                                 pCheck.primeOrNot(nums[i].getNum()));
            myOutputFile.println("The largest prime number before " + 
                                 nums[i].getNum() + " is " + 
                                 nums[i].getNextSmallerPrimeNumber());
            myOutputFile.println("The largest prime number after " + 
                                 nums[i].getNum() + " is " + 
                                 nums[i].getNextLargerPrimeNumber());
          }}
        myOutputFile.println("\nPrime Numbers: ");
     
        for(int i = 0; i < nums.length; i++)
          if(pCheck.primeOrNot(nums[i].getNum())== 1)
          myOutputFile.println(nums[i].getNum());
     
        myOutputFile.println("\nNon-Prime Numbers: ");
     
        for(int i = 0; i < nums.length; i++)
          if(pCheck.primeOrNot(nums[i].getNum())!= 1)
          myOutputFile.println(nums[i].getNum());
     
        myOutputFile.close();
      }
    }
     
     
    // *****************MyLong class**********************
     
    import java.util.Random;
    import java.math.*;
    public class MyLong
    {
      private long num;
      byte smallestFactor;
      long nextSmallerPrimeNumber, nextLargerPrimeNumber;
     
      public MyLong()
      { Random rd = new Random();
        num = Math.abs(rd.nextLong());
        smallestFactor = 1;
        nextSmallerPrimeNumber = 0; nextLargerPrimeNumber = 0;
      }
     
      public void fillPrimeInfo()
      {
        PrimeChecker pCheck = new PrimeChecker();
        long ck = pCheck.primeOrNot(num);
        if( ck == 1) smallestFactor = 1;
        else smallestFactor = ck;
        for(int s = num-1; s > 0; s--)
          if(pCheck.primeOrNot(s) == 1)
        { nextSmallerPrimeNumber = s; break;}
        for(int j = num + 1; j < System.currentTimeMillis(); j++)
          if(pCheck.primeOrNot(j)==1)
        { nextLargerPrimeNumber = 1;
          break;}
      }
      public long getNum() {return num;}
      public byte getSmallestFactor() {return smallestFactor;}
      public long getNextSmallerPrimeNumber() {return nextSmallerPrimeNumber;}
      public long getNextLargerPrimeNumber() {return nextLargerPrimeNumber;}
    }
     
    //********************primeChecker class***********************
     
    public class PrimeChecker
    { public static long primeOrNot(long x)
      {
      if(x == 2) return 1;
      for(long y = 2; y < x/2; y++)
        if(x%y == 0)
        return y;
      return 1;
    }
    }
    Last edited by Deep_4; November 8th, 2012 at 01:23 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with an API problem

    Umm... Wow. That's a lot of errors. First: Are all 3 classes in separate files? If not, you can only import at the top of the file, not for each class (each class gets all the imports, though). Second, any class you create inside the main class must be declared static if you want to be able to use the class without a PGM4 object. Along with this, it seems you're trying to access some methods in a static way or in an instantiated way, but the method is declared otherwise. The last compilation problem I noticed was that Java won't automatically let you cast numbers "down" (lost of precision). You must put the cast in manually. It will, however, let you cast up automatically or manually. However, since you're dealing with longs, I went a ahead and changed the variables that needed casting into longs.

    Here's the code I had after I fixed all the compiler errors. Note: this doesn't guarantee that the program will do what you want it to!
    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
     
    public class PGM4
    {
     
        public static void main (String[] args) throws IOException
        {
            String fname;
            int temp;
            MyLong[] nums;
     
            Scanner kb = new Scanner(System.in);
            System.out.println("How many MyLong objects do you want to have? " + "\nAn input of 30 or more is suggested.");
            temp = kb.nextInt();
            nums = new MyLong[temp];
     
            System.out.print("Enter the output filename: ");
            Scanner file1 = new Scanner(System.in);
            fname = file1.nextLine();
            FileWriter fwriter = new FileWriter(fname);
            PrintWriter myOutputFile = new PrintWriter(fwriter);
            PrimeChecker pCheck = new PrimeChecker();
     
            for (int i = 0; i < nums.length; i++)
            {
                nums[i] = new MyLong();
                nums[i].fillPrimeInfo();
            }
            myOutputFile.println("Mixed Numbers: ");
            for (int i = 0; i < nums.length; i++)
            {
                myOutputFile.print("\n" + nums[i].getNum());
                if (pCheck.primeOrNot(nums[i].getNum()) == 1)
                {
                    myOutputFile.println(" is a prime number.");
                    myOutputFile.println("The largest prime number before " + nums[i].getNum() + " is "
                            + nums[i].getNextSmallerPrimeNumber());
                    myOutputFile.println("The largest prime number after " + nums[i].getNum() + " is "
                            + nums[i].getNextLargerPrimeNumber());
                }
                else if (pCheck.primeOrNot(nums[i].getNum()) != 1)
                {
                    myOutputFile.println(" is not a prime number." + " It is divisible by: "
                            + pCheck.primeOrNot(nums[i].getNum()));
                    myOutputFile.println("The largest prime number before " + nums[i].getNum() + " is "
                            + nums[i].getNextSmallerPrimeNumber());
                    myOutputFile.println("The largest prime number after " + nums[i].getNum() + " is "
                            + nums[i].getNextLargerPrimeNumber());
                }
            }
            myOutputFile.println("\nPrime Numbers: ");
     
            for (int i = 0; i < nums.length; i++)
                if (pCheck.primeOrNot(nums[i].getNum()) == 1)
                    myOutputFile.println(nums[i].getNum());
     
            myOutputFile.println("\nNon-Prime Numbers: ");
     
            for (int i = 0; i < nums.length; i++)
            {
                if (pCheck.primeOrNot(nums[i].getNum()) != 1)
                {
                    myOutputFile.println(nums[i].getNum());
                }
            }
     
            myOutputFile.close();
        }
     
        // *****************MyLong class**********************
        public static class MyLong
        {
            private long num;
            long         smallestFactor;
            long         nextSmallerPrimeNumber, nextLargerPrimeNumber;
     
            public MyLong ()
            {
                Random rd = new Random();
                num = Math.abs(rd.nextLong());
                smallestFactor = 1;
                nextSmallerPrimeNumber = 0;
                nextLargerPrimeNumber = 0;
            }
     
            public void fillPrimeInfo ()
            {
                PrimeChecker pCheck = new PrimeChecker();
                long ck = pCheck.primeOrNot(num);
                if (ck == 1)
                    smallestFactor = 1;
                else
                    smallestFactor = ck;
                for (long s = (num - 1); s > 0; s--)
                    if (pCheck.primeOrNot(s) == 1)
                    {
                        nextSmallerPrimeNumber = s;
                        break;
                    }
                for (long j = (num + 1); j < System.currentTimeMillis(); j++)
                    if (pCheck.primeOrNot(j) == 1)
                    {
                        nextLargerPrimeNumber = 1;
                        break;
                    }
            }
     
            public long getNum ()
            {
                return num;
            }
     
            public byte getSmallestFactor ()
            {
                return smallestFactor;
            }
     
            public long getNextSmallerPrimeNumber ()
            {
                return nextSmallerPrimeNumber;
            }
     
            public long getNextLargerPrimeNumber ()
            {
                return nextLargerPrimeNumber;
            }
        }
     
        // ********************primeChecker class***********************
     
        public static class PrimeChecker
        {
            public long primeOrNot (long x)
            {
                if (x == 2)
                    return 1;
                for (long y = 2; y < x / 2; y++)
                    if (x % y == 0)
                        return y;
                return 1;
            }
        }
    }

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    JavaPF (July 22nd, 2009), lostgoat (July 22nd, 2009)

  4. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with an API problem

    Thank you for your help. I see that alot mishaps were there.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with an API problem

    I understand what it feels like everyone's new to programming at some point and time. If you stick at it, i'm sure you'll be able to pick up on these small details.