Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Debuggin Help

DJ0191DJ0191 Posts: 2Member
Hi guys.
Im a beginner in java and im doing a course in data structures using java. we're supposed to learn java ourselves.
I am having trouble in debugging a program that we were supposed to write.

Its a program to multiply sparse matrices using linked lists. The input of the matrix values are taken from a file in form of tuples of the form (4,3,1) i.e 4th row, 3rd column, value in that box.

My input code definitely works, its the implementation of the sparse matrices using linked lists that seems to be causing problems. I've tried my hand at finding the source of error but i couldnt manage it.

The problem with the program is that the output file while we're supposed to create is not showing any value in it.
Its being created with only one number 0 coming up in the file.

the following is the code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package spa;


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.PrintWriter;



/**
*
* @author DJ
*/
public class Main {

public static class link
{
int row,column,value;
public link next;
link()
{
row=0;
column =0;
next = null;
value =0;
}
public link( int r, int c, int v)
{
row=r;
column=c;
value=v;
next = null;
}

public void displayLink() // display ourself
{
System.out.print("{" + row + ", " + column + ", " + value + "}");
}


}
public static class linklist{

public link first;
public link last;
public link currentlink;


public linklist()
{
first= new link();
last= new link();
currentlink = new link();

}



public boolean isEmpty()
{ return first==null; }






public void insert(int r, int c, int v )
{
link newlink = new link(r, c, v) ;

if( isEmpty() )
{
first = newlink;
}
else
{
last.next = newlink;
}
last = newlink;
}


public void move()
{
currentlink=currentlink.next;
}


public link currentlink()
{
return currentlink;
}




}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

File file = new File("C:\Dumps\A.txt");
FileInputStream fis = null;
BufferedInputStream ins = null;
DataInputStream dis = null;



linklist matrix1 = new linklist();


String size;
int siz;
try
{
fis = new FileInputStream(file);
fis = new FileInputStream(file);


ins = new BufferedInputStream(fis);
dis = new DataInputStream(ins);

String s=dis.readLine();

size = s;

siz=Integer.parseInt(s);



while (dis.available() != 0)
{

s= dis.readLine();
int i=0;
int b=0;
int j=0;
int k=0;
char d;
int l=0;
do
{
l=s.length();
for(i=b;i<l;i++)
{
d=s.charAt(i);
if(d=='(')
break;
}

for(j=i+1;j<l;j++)
{
d= s.charAt(j);
if(d==',')
break;
}

for(k=j+1;k<l;k++)
{
d=s.charAt(k);
if(d==',')
break;
}

for(b=k+1;b<l;b++)
{
d=s.charAt(b);
if(d==')')
break;
}

String m = s.substring((i+1),j);
String o = s.substring((j+1),k);
String p = s.substring((k+1),b);

int x =Integer.parseInt(m);
int y =Integer.parseInt(o);
int z =Integer.parseInt(p);


matrix1.insert(z,x,y);

}while (b!=l-1);
}
}
catch (FileNotFoundException e)
{
}
catch (IOException e)
{
}
catch (NumberFormatException e)
{
}


//INPUT FROM FILE A IS COMPLETE

File fileb = new File("C:\Dumps\A.txt");
FileInputStream fisb = null;
BufferedInputStream insb = null;
DataInputStream disb = null;




linklist matrix2 = new linklist();


int sizb=0;
String sizeb;
try
{
fisb = new FileInputStream(file);
fisb = new FileInputStream(file);


insb = new BufferedInputStream(fis);
disb = new DataInputStream(ins);

String sb=disb.readLine();

sizeb = sb;

sizb=Integer.parseInt(sb);



while (disb.available() != 0)
{

sb= disb.readLine();
int ib=0;
int bb=0;
int jb=0;
int kb=0;
char db;
int lb=0;
do
{
lb=sb.length();
for(ib=bb;ib<lb;ib++)
{
db=sb.charAt(ib);
if(db=='(')
break;
}

for(jb=ib+1;jb<lb;jb++)
{
db= sb.charAt(jb);
if(db==',')
break;
}

for(kb=jb+1;kb<lb;kb++)
{
db=sb.charAt(kb);
if(db==',')
break;
}

for(bb=kb+1;bb<lb;bb++)
{
db=sb.charAt(bb);
if(db==')')
break;
}

String mb = sb.substring((ib+1),jb);
String ob = sb.substring((jb+1),kb);
String pb = sb.substring((kb+1),bb);

int xb =Integer.parseInt(mb);
int yb =Integer.parseInt(ob);
int zb =Integer.parseInt(pb);


matrix2.insert(zb,xb,yb);
matrix2.currentlink.displayLink();
}while (bb!=lb-1);
}
}
catch (FileNotFoundException e)
{
}
catch (IOException e)
{
}
catch (NumberFormatException e)
{
}


int[][] C = new int[100][100];
matrix1.currentlink = matrix1.first;
matrix2.currentlink = matrix2.first;
while (matrix1.currentlink != null)
{
System.out.println("tt");
while(matrix2.currentlink != null)
{
System.out.println("tt");
if(matrix1.currentlink.column == matrix2.currentlink.row)
{
System.out.println("ss2");
C[matrix1.currentlink.row][matrix2.currentlink.column] = C[matrix1.currentlink.row][matrix2.currentlink.column] + matrix1.currentlink.value * matrix2.currentlink.value;
System.out.println(C[matrix1.currentlink.row][matrix2.currentlink.column]);

}
System.out.println("ss");
matrix2.currentlink = matrix2.currentlink.next;
}
matrix2.currentlink=matrix2.first;
matrix1.currentlink=matrix1.currentlink.next;
}




try
{


FileWriter fo = new FileWriter("C:\Users\DJ\Documents\C.txt");
BufferedWriter ft = new BufferedWriter(fo);
PrintWriter fc = new PrintWriter(ft);
fc.println(sizb);
int p=0;
int q=0;

while(p<100)
{
while(q<100)
{
if(C[p][q] == 0)

{
q++;
}
else
{
fo.write("(" + p + "," + q + "," + C[p][q] + ")");
q++;
}
}
p++;

}
fc.close();
fo.close();
ft.close();
}
catch (IOException ex) {
}

}

}

I understand im asking for a lot, but any help whatsoever will be much appreciated.

Comments

  • silveredge52silveredge52 Posts: 198Member
    Hey,
    The first thing that may be wrong is that exceptions may be occurring
    and those events are being ignored. In [b]every[/b][u][/u] catch block there should be some output to show that an exception occurred. ie.
    [code]} catch (FileNotFoundException e) {System.out.println("#01 FileNotFoundException
    ");}
    catch (IOException e) {System.out.println("#01 IOException
    ");}
    catch (NumberFormatException e) {
    System.out.println("#01 NumberFormatException
    ");
    System.out.println(e.getMessage());
    e.printStackTrace();
    }[/code]
    I am not sure of the correct input format but I got NumberFormatExceptions when I ran the program and guessed at the input data format.

    If you provide a sample of the input data, that will help in the debug process.
  • silveredge52silveredge52 Posts: 198Member
    Hey,
    Did a few changes. see attached file. There are debugging statements in the program that should help.

    regards, se52

    ps. you will need to change back the input / output file specifications to reflect your environment
  • silveredge52silveredge52 Posts: 198Member
    Hey,
    Not sure about sparse matrix multiplication, but attached program maybe does what needs to be done.

    regards, se52

    ps. same ps. as prior post
  • DJ0191DJ0191 Posts: 2Member
    Thnks a lot man. Really helped a lot. The code worked. The comments were extremely helpful.
    Thanks again!
    DJ
Sign In or Register to comment.