wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Passing a Variable value from one Method to another Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing a Variable value from one Method to another Method" Watch "Passing a Variable value from one Method to another Method" New topic
Author

Passing a Variable value from one Method to another Method

Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear All,
I have the following code.

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

class testParse
{
String a = "";
String str;
public String readXML()
{
try
{
File fileToRead = new File( "c:/account.xml" );
RandomAccessFile raf = new RandomAccessFile fileToRead, "r" );
while ((str=raf.readLine()) != null)
{
System.out.println(str);
}
}
catch(Exception e)
{}
return str;
}

int parse(String str)
{
int ON = 1;

try
{
a = str;

PacketLength=a.lastIndexOf(">");
System.out.println(PacketLength);
}
catch(Exception e)
{}
return ON;
}

public static void main(String args[])
{
testParse p = new testParse();
p.parse("String");
}

}

I want to pass the str value from readXML method to parse method. When i run this program iam not able to get the length of str. It is displayed as -1. The problem is the value of variable str is not passed to parse() method. Suggest me how to pass the value. Your help is very much apprecitated.

Thanks in advance,

regards,
Raghu
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
There is much about your code that I do not like, but the immediate problem is that this method:

a.lastIndexOf(">");

returns -1 because a has a value of "String" and so no match with ">" can be found.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Adisesha Rao Raya
Greenhorn

Joined: Apr 08, 2005
Posts: 6
I am giving the solution for your problem from my prospective of understanding. if it helps you i am very much glad


import java.io.*;
class testParse
{
String a = "";
String str;
public String readXML()
{
StringBuffer stBuf= new StringBuffer();
try
{
File fileToRead = new File( "c:/account.xml" );
RandomAccessFile raf = new RandomAccessFile( fileToRead, "r" );
while ((str=raf.readLine()) != null)
{
stBuf.append(str);
System.out.println(str);
}
}
catch(Exception e)
{}
stBuf.toString();
return str;
}

int parse(String str)
{
int ON = 1;

try
{
a = str;

int PacketLength=a.lastIndexOf(">");
System.out.println(PacketLength);
}
catch(Exception e)
{}
return ON;
}

public static void main(String args[])
{
testParse p = new testParse();
String st = p.readXML()
p.parse(st);
}
}

In your previous program str in readXML() is returning null because of out of scope(i think).Thats why you are getting -1 as string length.
Thanks
Seshu
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Look more closely at your readXML() method:

Notice the line I added here. What output does it produce and why?

Also, since str is used locally in this method, it should be declared locally, instead of as a instance variable as you have it now.

So see if you can fix the readXML() method so that it returns the value that you expect. I think you are on the right track and others have given some useful hints.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a Variable value from one Method to another Method