A little help requested, please.
I am writing a little java program. Essentially, it reads one file line by line and depending on what it finds in the line, then takes certain actions, the most important being writing something to an html file. The basic structure is:
start of loop (which means new line read from file)
identify parts of the line: if A and B then methodX
"" "": if A and C then methodY
"" "": if A and D then methodZ
etc
end of loop
The work is done in the methods, since depending on B, C, D, etc different actions are to be taken. Some lines write new rows into a table, while some lines do not get the new row. All of this is fine and good and works well up until I decided to add an element of time tracking to the program. Each line has a timestamp, and I am using a pretty ghetto method of converting them to a number (essentially the timestamp converted to the number of seconds in the day, i.e. 01:23:32 would be 3600 + 1380 + 32 or 5012). In order to figure out the time that has passed since the previous line in the log, I'm trying to track it with a variable, so I can take the current line's converted timestamp and substract the previous line's converted timestamp from it.
This in itself would be simple, but the problem comes in when I essentially want to ignore certain lines... basically, I only want to note down the timestamp whenever a line comes up that warrants writing a new row into the table in the html file. Normally I could just set it up to note down the timestamp only when I call methods that will result in a new row.... but the problem comes in with one of my methods that sometimes does and sometimes does not write in a new row. I can't access my timestamp variable from within the method, so I can't update it from there. I've looked into trying to change my method declaration in ways that I can see variables from my main method, but I haven't found anything that works yet.
I can't even come up with a workaround.
I keep running into the wall that seems to separate my methods from my program proper. I know there's no way to see variables made in a method outside of that method, but from various results on Google I'm not seeing anything that nixes the idea in the other direction, which makes me think I'm missing something. I wrote what I have in just 3 days from knowing 0 java, and most of my time has been spent pinning down syntax, so I'm a bit shaky on the higher level structural sort of concepts of the language.
Maybe I can find some way to get around having a method that sometimes does and sometimes does not write a new row. Basically it defaults to writing a new row, but depending on a certain part of the line in the file, it will write a different line without the row added. A and D are detected, let's say, but for that specific line I have to check F, and some F's will warrant a new row while other F's will not. Perhaps I can somehow do that detection after A and D are both detected, and make two methods, one for approved F's and one for unapproved. Then I can just brute force add the timestamp tracking to every time I detect A and B or whatever pairs will require a new row. It won't be pretty, but it seems to me like it works.
Whether it works or not, I'd just like some clarification on this whole detecting/changing a variable from my main program from within a method. For added clarity, something like:
Code:
public static int someMethod(int withinmethod) {
int methodadjusting = withinmethod + 5;
adjusted = 'y'
}
public static void main(String[] args) {
char adjusted;
int basenumber = 3;
if (args[0] == "y") { someMethod(basenumber); }
else adjusted = 'n';
System.out.println("was number adjusted? answer: " + adjusted);
}
This won't compile because it can't find the variable "adjusted" in the method. Is there any way to break down this wall or does it always have to be worked around carefully?
And hrm, now that I'm explaining all of this to you guys clearly, I am coming up with ideas. Would the best way to handle this just simply have all of my methods that result in a new row being written to the file return a value? Or just the troublesome method that doesn't know at the time of calling, at least. Let's say the troublesome one is A and D, I can do something like..
if (A and D) {
doAandDmethod(some, params);
if (doAandDmethod(some, params) == y) { update timestamptrackervariable; }
}
while having all the other methods that I know will write a new row can do the same with the second if statement removed.
Well, it's too late for me to try and work that in now. I probably shouldn't even be trying to solve this problem at this hour, let alone expose my lack-of-sleep-affected ideas to the public.
Even if this new idea does work, though, I'm still interested in the theory behind my question. :p Various websites I come across don't tell me, at least not in ways I understand, so either I don't know how to search for what I want to know or I'm not recognizing it when I see it. Some example pages I found are
here and
here.