• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing values from method1 to method2  RSS feed

 
Madleine Gerster
Greenhorn
Posts: 3
Linux Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks!

I am new to Java (and this forum), so there's a lot to learn for me. But that's what I am here for

I wrote a programm that does all I want it to, but I wrote all the code into the main-method...
Now I want to divide my code into several methods to make it easier to use and read. But I keep having problems with returning values between the methods.

This fragment reads in the file. line and br_in are global variables because I want to use them also somewhere else in the code.
(I deleted some stuff that is not relevant here, for example the exeption handling.)

If I use System.out.println(line) in the while-loop above, it works and prints the file correctly.
Now I want to go through the file line by line (which is what the code above does) and do the following with everly line that fits:

It is supposed to count every line that starts with NAME and then print the number. But as I stated in the code, the line is always null so it cannot count and just returns the value 0 (from float sum).
How can i pass the actual line i read from the first method (read_file) to the second (calc) so that it can count the occurrences of lines starting with NAME?

In my all-main-ocde it looks like this and works:


Please don't tell me to just leave it the way it works I don't want to have ALL my code in the main-method. I'd like to have a method that reads the file, one that calculates something and another that does other stuff. And since my original code is way longer i need to split it up in methods. Even I have already problems reading it
Sorry if my text is not that easy to understand... it is hard for me to explain, especially because english is not my first language (and Java is hard to learn...).

Thanks in advance, have a nice day
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you hopefully, know, a method can only return one thing. However, that 'one thing' can be a collection that holds lots of other things - just like a single egg carton can hold many eggs.

So, you have a few options. You could have your first method build an ArrayList of Strings, and then pass that around:



Then call it:

ArrayList<String> tempList = myMethod();

Then pass it in to your second method:




Note: My syntax may be off on how exactly to define everything

You could also declare a class level arraylist, the same way you have defined your 'line' or 'br_in' variables

 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't have a Reader as a field (there should really be no such thing as a global variable in Java™), but as a local variable. When you close it, you can't reopen it, so it is best it go out of scope.
Don't mark fields public, except constant fields.
You have a subtle but serious error about the line field. Since you say line in the reading code, and don't re-declare it as a local variable, you will be altering the value of the field frequently. And why did you make that field static? I think you would do well to get rid of the line field.
 
Madleine Gerster
Greenhorn
Posts: 3
Linux Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies and the welcome!

Fred,
just to make sure I understood you right: I would create an Arraylist that stores every line as an element and then I can go through the elemets of that Arraylist and make my calculations and whatsoever? That sounds good . But then I have a question: why do I actually have to create another "thing" (the Arraylist)? Is it not possible to pass every single line (which I already read in my read_file method) to another method instead of storing all the lines to an Array? I'd like to hear about the reasons for using that Arraylist (I have never worked with that before).

Ritchie (or Campbell? ^^),
I marked my methods public because I want to access them from other classes as well. And as far as I know, this is the only way to do so.
Same with the global variables: I want to use them in other methods as well as other classes. But that's exaclty the point what my posting is about: I don't know other ways to exchange values (e.g. my lines or other variables) between methods. And by using a global variable I had at least access to line in another class. Static was used because I found that on a website where they explained how to declare global variables. Until now I thought global variables were quite useful, I never came across any problems by using them (but I only do so if I can't find another option).
What do you mean by re-declaring? How would that work?
But I guess I see what you pointed out about that error: public static String line is null until I run the read_file method. While running it changes it's value since the lines change as well. And after that i close the file and line is null again, did I get that right?

Unfortunately I'm still not sure how to pass my lines to other methods... (apart from what fred metioned, but is there really no other way? I mean, it works in my all-main-code, why not here?).

I am asking lots of questions, I know, but I really want to understand this
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are always other ways...

A common goal in software design is to have each method do ONE thing (and hopefully do it well). So, you may have one method read a file. You may have one method that can process a line. you may have another method that prints your output, etc.

Now, a method can only return one 'thing', like a String, or an Integer, or a boolean. Often, it would be nice if a single method could return more than one thing - but that's not possible in Java (some languages do allow it). So if you want to return two Strings, your out of luck.

UNLESS you return a single thing that happens to HOLD two (or possibly more) Strings. Let's say you only have one free hand, and need 12 eggs. What do I do? I either hand you one egg, wait for you to use it, then hand you the next, wait, etc...

OR

I hand you a carton that holds 12 eggs, and you then take them out and use them as you need them.

One option you have is to have a method that reads the file, puts each line into an ArrayList (which will automatically grow larger as you need it to) - this is the egg carton. You can then hand this entire carton to your next method, which then processes through it....

You could also have your method read a line, and put it into a String. You then pass that String into a method that process it (so, inside one method you call another method).

there are other ways to do what you want, but these are probably the easiest/simplest.
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Readers are particularly bad things to have hanging around. You want to read from the file, close the Reader and get rid of it. Otherwise you get all these Readers hanging around occupying space needlessly. It's not as if you could open them again. And if you don't close them it's like keeping a phone off the hook. You keep the connection and don't use it, and that connection is no use to anybody else. Or: you can't have two processes reading the same file. The first has to release access to the file before the second starts.
Global variables are error-prone. They are all right if they never change . . . so global constants are useful, but what if they can change, like this?Now are classes using that going to know whether tax is 20% or 30%?

I wrote about when methods should be static this morning. Try this thread.

Fred has told you to put the lines into a List. That List can be a field, and you can use it in other methods. You can add the lines read to itwatch this space
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, you can get your hands on that List, even though it is private, if the class Foo permits it.So you read those lines into the List in ff, and get the List back and print every other line. Now, how does the Foo class make that List available? Answer, with a "get" method, like what they show you here in the Bicycle class. Or you can be very clever and release the information about that List in "read-only" form, because there are methods which allow you to copy that ListYou will find the unmodifiable method here.

Good luck with it
 
Madleine Gerster
Greenhorn
Posts: 3
Linux Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, you helped me a lot: I already made an ArrayList like you suggested and it was easier than I thought. I managed to pass it to another method and try out some little things; right now, it works. Hopefully this will go on like that
I really appreciate your explanations since I don't just want to do something without understanding why I do it.

So, have another nice day
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!