import java.io.*;
import java.util.StringTokenizer;

public class Proj21110
{
    private static int MAX_UNKNOWNS = 10;

    public static void main (String[] args)
    {
    headerMessage(); // print header 

    BufferedReader in = null;

    if(args.length > 0)
        {
        in = getReader(args[0]); // open file
        System.out.println("Opened file: " + args[0]);
        }
    else
        {
        in = getReader("guess.data"); // open file
        System.out.println("Opened file: " + "guess.data");
        }



    System.out.println();

    printContents(in);

    }

//////////////////////////////////////////////////////////////////////  
/*
     Prints out the Heading of the Program
*/
/////////////////////////////////////////////////////////////////////

    private static void headerMessage()
    {
        System.out.println("CMSC256 Project 2 - Fall '08 - C. Walker");
        System.out.println();

    } //headerMessage


//////////////////////////////////////////////////////////////////////  
/*
     Open file
*/
/////////////////////////////////////////////////////////////////////
    private static BufferedReader getReader(String name)
    {
        BufferedReader in = null;
        try
        {
            File file = new File(name);
            in = new BufferedReader(new FileReader(file));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("The file doesn't exist.");
            System.exit(0);
        }

        return in;
    }

//////////////////////////////////////////////////////////////////////  
/*
    print contents
*/
/////////////////////////////////////////////////////////////////////
    private static void printContents(BufferedReader in)
    {
        double[][] a = new double[MAX_UNKNOWNS][MAX_UNKNOWNS+1];
        int i,j;

        int numUnknowns = 0;

        try{
        String line = in.readLine();

        while(line != null && line.length() > 0)
        {

        // read number of unknowns
        numUnknowns = Integer.parseInt(line);

       // print out unkowns
        System.out.println("\nNumber of unknowns: " + numUnknowns);

        // read in equations
        for(i=0;i<numUnknowns;i++)
        {
            line = in.readLine();;

            StringTokenizer tok = new StringTokenizer(line);


            for(j=0;j<numUnknowns;j++)
            {
                a[i][j] = Double.parseDouble((tok.nextToken()));
                if(j > 0)System.out.print(" + ");
                System.out.print(a[i][j] + "x_" + (j+1));
            }

            a[i][j] = Double.parseDouble((tok.nextToken()));
            System.out.println(" = " + a[i][j]);

        }

       gaussian(a, numUnknowns);

           line = in.readLine();
        } // end while

        } // end try

        catch (IOException e)
        {
            System.out.println("I/O Error");
            System.exit(0);
        }

        catch(NumberFormatException ex)
        {
            System.out.println("Impropper number encountered in file");
            System.exit(0);

        }       


        }

//////////////////////////////////////////////////////////////////////
/*
gaussian elimination
*/
/////////////////////////////////////////////////////////////////////

    private static void gaussian(double[][] a, int n)
        {

        int i,j,k;
        double t;
        double[] x = new double[n];
        double sum;

        // Step 1 For i = 1,...., n-1 do Steps 2 - 4. (Elimination Process)
        for(i=0;i<n;i++){

        // Step 2 Let p be the smallest integer with i <= p <= n and ap,i != 0.
        int p = i;
        boolean found = a[i][i]!=0;

        for(j=i+1;j<n;j++)
        {
            if(a[i][j] != 0)found = true;

            if(a[i][j] != 0 && Math.abs(a[j][i]) > Math.abs(a[i][p]))
            {
                p = j;
            }
        }

        // If no integer p can be found then
        //OUTPUT('singular matrix - no unique solution'); STOP.
        if(!found)
        {
              System.out.println("singular matrix - no unique solution");
              return;
        }

        // Step 3 If p != i then perform (Ep) <-> (Ei)
        if(p!=i)
        {
            // swapping rows p and i
        for(k=0;k<n+1;k++)
        {
            t = a[p][k];
            a[p][k] = a[i][k];
            a[i][k] = t;
        }
        }

        if(a[i][i] == 0)
        {
            System.out.println("singular matrix - no unique solution");
            return;
        }


        // Step 4 For j = i + 1,..., n do Steps 5 and 6.
        for(j=i+1;j<n;j++) {

            //Step 5 Set m=aji/aii.
            double m = a[j][i] / a[i][i];

        // Step 6 Perform (Ej - mEi) -> (Ej).
            for(k=0;k<n+1;k++)
               a[j][k] = a[j][k] - m * a[i][k];
        }
        }


        // Step 7 If ann = 0 then OUTPUT('no unique solution exists'); STOP.
            if(a[n-1][n-1] == 0)
            {
                System.out.println("singular matrix - no unique solution");
                return;
            }

        // Step 8 Set xn = an,n+1=ann. (Start backward substitution)
            x[n-1] = a[n-1][n] / a[n-1][n-1];

        // Step 9 For i = n - 1,....,1  set xi = [ai, n+1 - sum aij,xj] / aii
            for(i=n-2;i>= 0;i--)
            {
                sum = 0;
                for(j=i+1;j<n;j++)
                {
                    sum = sum + a[i][j] * x[j];
                }

                x[i] = (a[i][n] - sum) / a[i][i];
            }

        // Step 10 OUTPUT(x1,...., xn); (Procedure completed successfully)
        // STOP.
            printSolution(x, n);
        }


//////////////////////////////////////////////////////////////////////
/*
    print solution
*/
/////////////////////////////////////////////////////////////////////

    private static void printSolution(double[]x, int n)
    {

    System.out.println("Solution: ");

    for(int i=0;i<n;i++)
        {
        System.out.println("x_" + (i+1) + " = " + x[i]);
        }

}
}

The file that im reading from is called gauss.data. It contains the following elements:

4
2 1 1 -6 -1
-3 6 -1 8 -20
1 1.0 1 -3 0
4 -2 8 -3 43
3
5 -1 -1 11
-1 5 -2 0
-2 -2 4 0
3
0 1 1 1
0 -2 -2 -8
0 0 -6 2

the error i get when i run the program is:

Exception in thread "main" java.util.NoSuchElementException
                                   at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
                                   at Proj21110.printContents(Proj21110.java:112)
                                   at Proj21110.main(Proj21110.java:33)

It reads in the first elements, it recognizes that 4 is the number of unknowns, and it reads in 2, 2, -6, and -1 but then throws this error. What am I doing wrong?

first of all, you forgot to use code tags, which makes this very difficult to read
I sujest you check the line where the exception occurs, check whether or not you've forgotten a test on it and if you still can't find it, put your code back here in a readable way

code tags, clear distinctions between the different .java files, ...

Hi VirusTalker,

Your code seems Ok. However, in the printContents(BufferedReader in) method, after tokenizing the read string from file, try checking if it hasMoreTokens.

Look at the below modified code of your method and try if it works. Hope it does;

private static void printContents(BufferedReader in)
{
double[][] a = new double[MAX_UNKNOWNS][MAX_UNKNOWNS+1];
int i,j;

int numUnknowns = 0;

try{
String line = in.readLine();

while(line != null && line.length() > 0)
{

// read number of unknowns
numUnknowns = Integer.parseInt(line);

// print out unkowns
System.out.println("\nNumber of unknowns: " + numUnknowns);

// read in equations
for(i=0;i<numUnknowns;i++)
{
line = in.readLine();;

StringTokenizer tok = new StringTokenizer(line);

while(tok.hasMoreTokens())
{
for(j=0;j<numUnknowns;j++)
{
a[j] = Double.parseDouble((tok.nextToken()));
if(j > 0)System.out.print(" + ");
System.out.print(a[j] + "x_" + (j+1));
}

a[j] = Double.parseDouble((tok.nextToken()));
System.out.println(" = " + a[j]);
}
}

gaussian(a, numUnknowns);

line = in.readLine();
} // end while

} // end try

catch (IOException e)
{
System.out.println("I/O Error");
System.exit(0);
}

catch(NumberFormatException ex)
{
System.out.println("Impropper number encountered in file");
System.exit(0);

}


}

Its working fine, with no exceptions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.