StackOverFlow Error
Hye Dear Gurus
I have a Matrix of 9000x3 size and I have to read this Matrix from a file .txt for 9000 times for 9000 variables (For the time these are 9000, Later,it can increase and also I cannot change input).
I have done program through recursion.Recursion goes so long that it cannot even complete for first vertex and gives error of (StackFlow).I have tried to do with Iteration but it looks difficult if not impossible.Can you guide me what should I do in this situation: Should I do it with iteration or there is another solution too?
Thanks in advance .waiting
[594 byte] By [
Kimsa] at [2007-11-27 2:08:10]

Try to avoid recursion if possible
Try this:
try{
File file = new File("C:/123456789.txt");
Scanner scanner = new Scanner(System.in);
final int rows = scanner.nextInt();
final int cols = scanner.nextInt();
BufferedReader br = new BufferedReader(new FileReader(file));
int[][] matrix = new int[rows][cols];
String tempSt = "";
while ((tempSt = br.readLine()) != null){
//update matrix with tempSt
}
} catch(IOException e){
e.printStackTrace();
}
This is the Recursive Funtion. Please Review it.May be I am having problem due to bad programming.
Infact I need to calculate the Minimum distance between all vertices.
public void Sh_Fun(int source)
{
//////////////////////////////////
String out1=" ",rec;
//Glob.rec_counter++;
boolean found=false;
////////////////////////// Preparing List of Functions that are passed to this recursive function /////////////
if (Glob.RV.indexOf(Integer.toString(source))==-1)
Glob.RV=Glob.RV.concat(Integer.toString(source).concat(" "));
//////////////////////////////////////////////////////////////////////////////////////////////////////////
try
{
//////////////////// open and close files //////////////////////////
// open the input file
RandomAccessFile dis = new RandomAccessFile( "ListeSortant.txt", "rw" );
///////////////////////////////////////////////////////////////////////////////////////////////
while ( (rec=dis.readLine() ) != null )
{
String[] s1=rec.split("\\s"); // one line read
if (source==Integer.parseInt(s1[0]))
{
out1=out1.concat(s1[1].concat(" "));
found=true;
if (source==Glob.x)
Glob.direct=out1.concat(s1[1].concat(" ")); // JUST to create a string of vertices that are directlt attached with source
}
else
{
if (found==true)
break;// because a particular pattern of source exits in .txt file consectively
}// end of else
}// end of while reading file
////////////////////////////////////////////////////////////////////////////////////////////////////////////
out1=out1.trim(); // to eliminate the leading and trailing spaces
String[] s2=out1.split("\\s");// now s2 contains all the vertices attached directly with source (s2 and out1 contain the same thing but s2 contains no spaces
//////////////////////////////////////////////////////////////////////////////////////////////////
if (out1.equals("")) //infact when no vertex is attached directly with a passed source then problem occurs
return;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// System.out.println("Source Passed to This function is ="+source+" length of s2 is ="+s2.length);
// for (int k=0;k<s2.length;k++)
//System.out.println(" Total Vertices Attached to Vertex "+source+ " are= "+s2.length+" and attached vertices are="+s2[k]);
////////////////////////////////////////////////// MAIN LOOP of Writing in OUT2 /////////////////////////////////////////////////////////////////////
Glob.ps.println("This is for vertex "+source);
for (int i=0;i<s2.length;i++)
{
if (source==Glob.x)
{
Glob.out2[Integer.parseInt(s2[i])]=1;
Glob.ps.println(Glob.x+ " "+s2[i]+" 1"); // writing to output file
} // end of if
else
{
if (Glob.RV.indexOf(s2[i])==-1 && Glob.out2[Integer.parseInt(s2[i])]==0 && Glob.direct.indexOf(s2[i])==-1)//The condition Glob.direct.indexOf(s2[i]==-1 is for checking that if the source is othe than Glob.x then it should not change the distance of vertices directlt aattached to Glob.x, it should change only others
{
Glob.out2[Integer.parseInt(s2[i])]=Glob.out2[source]+1;
Glob.ps.println(Glob.x+ " "+s2[i]+" "+(Glob.out2[source]+1)); // writing to output file
}
} // end of else
}// end of for loop for i
/////////////////////////////////////////////////// Recursive CALL /////////////////////////////////
for (int j=0;j<s2.length;j++)
{
if (Glob.RV.indexOf(s2[j])==-1)
Sh_Fun(Integer.parseInt(s2[j])); // Recursive Call
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
dis.close();// Close our input stream
}// end of try
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}// end of catch
}// end of function
>
Kimsa at 2007-7-12 1:57:17 >
