Results 1 to 8 of 8

Thread: java code error

  1. #1
    omitchel is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default java code error

    what is wrong with this code see error when i run this code


    C:\>javac extend.java
    extend.java:3: error: class ExtGCD is public, should be declared in a file named
    ExtGCD.java
    public class ExtGCD {
    ^
    1 error

    C:\>




    // ExtGCD: Extended GCD (long version)

    public class ExtGCD {
    public static long[] GCD(long x, long y) { // assume not 0 or neg
    long[] u = new long[3];
    long[] v = new long[3];
    long[] t = new long[3];
    // at all stages, if w is any of the 3 vectors u, v or t, then
    // x*w[0] + y*w[1] = w[2] (this is verified by "check" below)
    // u = (1, 0, u); v = (0, 1, v);
    u[0] = 1; u[1] = 0; u[2] = x; v[0] = 0; v[1] = 1; v[2] = y;
    System.out.println("q\tu[0]\tu[1]\tu[2]\tv[0]\tv[1]\tv[2]");
    while (v[2] != 0) {
    long q = u[2]/v[2];
    // t = u - v*q;
    t[0] = u[0] -v[0]*q; t[1] = u[1] -v[1]*q; t[2] = u[2] -v[2]*q;
    check(x, y, t);
    // u = v;
    u[0] = v[0]; u[1] = v[1]; u[2] = v[2]; check(x, y, u);
    // v = t;
    v[0] = t[0]; v[1] = t[1]; v[2] = t[2]; check(x, y, v);
    System.out.println(q + "\t"+ u[0] + "\t" + u[1] + "\t" + u[2] +
    "\t"+ v[0] + "\t" + v[1] + "\t" + v[2]);
    }
    return u;
    }
    public static void check(long x, long y, long[] w) {
    if (x*w[0] + y*w[1] != w[2]) {
    System.out.println("*** Check fails: " + x + " " + y);
    System.exit(1);
    }
    }
    public static void main(String[] args) {
    long[] u = new long[3];
    long x = Long.parseLong(args[0]);
    long y = Long.parseLong(args[1]);
    u = ExtGCD.GCD(x, y);
    System.out.println("\ngcd(" + x + ", " + y + ") = " + u[2]);
    System.out.println("(" + u[0] + ")*" + x + " + " +
    "(" + u[1] + ")*" + y + " = " + u[2]);

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: java code error

    Java source files must be the same name as the single top level public class in the file. So if you have

    Java Code:
    public class ExtGCD  {
    ...
    ...
    }
    Then it must be in a file named ExtGCD.java

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    omitchel is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: java code error

    Great thk i am able to compile without error thk

  4. #4
    omitchel is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: java code error

    trying to run the class file get the following error


    C:\>java ExtGCD.class
    Error: Could not find or load main class ExtGCD.class



    C:\>java ExtGCD
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at ExtGCD.main(ExtGCD.java:35)


    it should ask me for the gcd value any idea...
    Last edited by omitchel; 02-26-2013 at 07:32 AM.

  5. #5
    kuriozal is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: java code error

    Quote Originally Posted by omitchel View Post
    it should ask me for the gcd value any idea...
    no, it should not.

    Java Code:
    long x = Long.parseLong(args[0]);
    long y = Long.parseLong(args[1]);
    this means that the application will read two arguments from command line:

    Java Code:
    C:\>java ExtGCD 5 2

  6. #6
    omitchel is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: java code error

    thk it work what can i do to direct the output into a msword

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: java code error

    When posting code here please use [code] tags [/code] in order to retain formatting.

    If you want to output to a Word doc then you'll need to find some API to allow you to do that (like Apache POI).
    If you just want to output to a text file then have a look at the IO tutorial.
    That links to the specifc text io section, using BufferedWriters. You might need to read the earlier section about paths as well.
    Please do not ask for code as refusal often offends.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: java code error

    ... and please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Odd java code error
    By Craig9008 in forum New To Java
    Replies: 2
    Last Post: 02-01-2012, 04:55 PM
  2. Replies: 5
    Last Post: 06-05-2011, 02:19 AM
  3. Error using xml-rpc with java code
    By mith86 in forum New To Java
    Replies: 1
    Last Post: 01-04-2011, 02:48 PM
  4. Java code giving error
    By alfarida in forum New To Java
    Replies: 3
    Last Post: 11-10-2010, 07:11 PM
  5. Java code error!~ Please help
    By socboy6579 in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 03:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •