Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a Variable value from one Method to another Method

 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adisesha Rao Raya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic