Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes passing value of string from one to another file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing value of string from one to another file " Watch "passing value of string from one to another file " New topic
Author

passing value of string from one to another file

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Hi all,

Good evening. I have done a coding to perform a string to be called from one file to another. Here i am trying to use the string template_no and the result of template_no should be displayed in the Foo program. i tried to get all the values existing in the template_no. But i am getting only the last value in template_no. How to get all the values of the template_no?



input file
send file.txt

001 Apple
001 mango
002 Pappaya
003 Jackfruit
003 orange
004 gauva
005 pineapple
006 stawbery

can any one help me.How to proceed. I am not able to proceed further. Help me..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7551
    
  18

deepika deepi wrote:I have done a coding to perform a string to be called from one file to another. Here i am trying to use the string template_no and the result of template_no should be displayed in the Foo program. i tried to get all the values existing in the template_no. But i am getting only the last value in template_no. How to get all the values of the template_no?

Deepika, I strongly urge you to stop coding and think about what you're doing.

For one thing, it'll help you to explain it to us better, because right now I have no idea what you want. All I see is a pile of code with two methods that appear to be doing almost exactly the same thing.

I think it's already been suggested on one of your other threads, but give your classes and fields meaningful names. It'll help us (and I suspect you) a LOT.

Right now, we're having plough through a lot of code with classes named Foo (not to mention a method called paint() that reads a file and returns a String (???)); and it gives us absolutely no context to work with.

What I see is the coding equivalent of flailing arms and machine-gun questions; and it's NOT the way to learn a language like Java.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Sorry Winston Gutkowski. Sorry for not giving clear explanation. I have two different class files. I want the result of one file to be called in the other file. That what is the requirement. In this i have taken template_no. i want that result to be displayed inside the other file. Am i clear now? I am trying to explain as my level best.

not to mention a method called paint()

i have searched in google for passing string from one to other from this link

http://stackoverflow.com/questions/3840438/java-passing-strings-from-one-class-to-another

So i have used that. IF i am wrong guide me. I have no idea how to do. what method should be called ? if i am not clear i am sorry. I am trying my level best to tell what i want.

is it possible to use hash function to get the result of one class output inside other class file?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
I am not convinced you have understood Winston’s suggestion. The reason you are trying so hard to explain what you want and not succeeding is that you have not yet worked out what you want. That stackoverflow link probably does more to confuse you than help you.

Don’t use DataInputStreams for text files. Use a BufferedReader, as you will find in the Java Tutorials.
Your handling of the inappropriately named parameter template_no is simply wrong. Also please indent your code correctly. If you have two } above each other, you must have indented a } incorrectly somewhere earlier.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7551
    
  18

deepika deepi wrote:Sorry for not giving clear explanation...

No need to apologise, it's a common beginner's mistake; but I hope you take the advice (and Campbell explained it even better than me).

Programming is NOT about coding, it's about thinking; and in order to solve a problem you must first understand exactly what it is.
And in order to do that you must be able to describe the requirements in full.

My suggestion:
1. Get a copy of the problem description as it was given to you.
2. TURN YOUR COMPUTER OFF.
3. Sit down and write out, in your own words, exactly what the description is asking you to do.

When you've done that, come back here and show us (a) the original problem description and (b) your interpretation of it. And do it before you write another line of code.

That will give us a basis to start helping you.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya,
i will follow it ..
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199

Campbell Ritchie

I am able to say what i want. But i am not able to do it programming wise. i feel difficult. i want to call a string from one method to another. i searched in google and get some results. and tried with that. But still i have no idea to proceed. How to do that? am i wrong . In perl they use sub followed but that file name and get the result by suing an hash and for loop function.In java how to do i have no idea. please if you know tell some good links there to use like that ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7551
    
  18

deepika deepi wrote:I am able to say what i want. But i am not able to do it programming wise. i feel difficult. i want to call a string from one method to another.

But that's the point: we don't understand what you mean by "I want to call a string from one method to another".
Do you mean you want to pass a String from one method to another? In which case, why is there a File involved?

i searched in google and get some results. and tried with that. But still i have no idea to proceed. How to do that? am i wrong . In perl they use sub followed but that file name and get the result by suing an hash and for loop function.

I'm afraid my perl isn't that great, but what you describe in this paragraph is NOT what you described above.

You must be clear when you're trying to describe a problem to other people. It appears that you want to read a String from a File, and pass it to another method; is that right?

What String (or Strings) do you want to read? Each line? A part of a line? The whole File? Any one of those things could be put into a String object, but unless you explain exactly what you want, we can't help you.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski
Do you mean you want to pass a String from one method to another? In which case, why is there a File involved?

ya i want to pass the string from one method to another. If i am correct it would be exactly the results of templateno (it consists of many values so i think it can be called as array.If you see the input you can tell whether i am right Eg:001 001 002 etc )sorted in one file to be called in the main file Check. So i have used files here.


It appears that you want to read a String from a File, and pass it to another method; is that right?


ya right.

What String (or Strings) do you want to read? Each line? A part of a line? The whole File?

ya the part of file that declared by string named templateno. that output i want to pass in the other file.

Any one of those things could be put into a String object

i have put a string object Send send = new Send();

in the line number 35 i am getting the output of templateno. that one i am trying to pass it to another class and i want the result to be displayed in that class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Do you mean you want to pass a String to the Send class’ constructor? Can it take a String? Do you want a String, or a number? Is 123 different from 00123?
You need to work out the answer to all that sort of question. And when you have those answers, you will probably be surprised by how easy it is to implement the code!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7551
    
  18

deepika deepi wrote:ya i want to pass the string from one method to another. If i am correct it would be exactly the results of templateno (it consists of many values so i think it can be called as array.

Or a List<String> (probably easier).

Right, so you want to do the following:
1. Read in each line of sendfile.txt.
2. Split the line into two parts: the number, and whatever comes after it.
  • Questions: What is the delimiter? Is it a space? Can there be more than one?
  • 3. You want your method (and please don't call it print()) to return a list of ALL the numbers in the file (ie, 1 for each line).
  • NOTE: If that's the case, then you method declaration is WRONG. You should be returning a String[] or a List<String>; I suggest the latter.
  • 4. ???

    And that's where you lose me still. What exactly do you want to do with these numbers? What is CheckProgram supposed to do? What is a Send object? From the look of it to me, one or other of them is redundant unless, as Campbell asked: Do you want one Send object for each line?

    BTW: The above is exactly what YOU need to be doing: Writing down each step IN DETAIL. And you really must give your classes and methods proper names, because the ones you have at the moment are incredibly confusing.

    Stop thinking about the code, and try and describe THE PROBLEM; and describe it as though the person you're talking to has never heard of Java.

    Winston
    deepika deepi
    Ranch Hand

    Joined: Jan 23, 2012
    Posts: 199
    hi all,

    Happy week end. Thank's for your replies. i have made some changes in the code and Hope i could explain in clear manner than before.



    i have changed the string to array in static void Send() . Is it possible to declare it above and use it in the static void Input(). If so. how to do that. Kindly help me.

    In this code CheckProgram is the class name and where main is declared. static void Send() is having a String called rule. next i have a static void Input() the rule from the code of this line

    should be used to check whether the match occurs in the Input.txt.In this rule is the key word for matching. The rule has fruits name. the input is in form of sentences. If the Input matches the rule. It should display only that result in the static void Input(). that what i need to do. I hope i am little clear in telling my requirements. Now help me hoe to pass the rule to the Input and check whether match occurs.

    send.txt contains
    001 Apple
    001 mango
    002 Pappaya
    003 Jackfruit
    003 orange
    004 gauva
    005 pineapple
    006 stawbery

    Input.txt contains
    In the section of the description is given about fruits.
    Apple is good for health than Mango.
    plums fruit is available in only certain months in the year.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: passing value of string from one to another file
     
    Similar Threads
    command line arguments for a file
    Reading one line from a text
    to read lines inside brackets
    Why do i get NoClassDefFound error??
    class that prints its own source code