The Glade 4.0

"Turn the lights down, the party just got wilder."
It is currently Sun Nov 24, 2024 4:13 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Nov 19, 2009 6:14 am 
Offline
User avatar

Joined: Thu Sep 24, 2009 4:57 am
Posts: 849
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Nov 19, 2009 6:08 pm 
Offline
User avatar

Joined: Thu Sep 03, 2009 11:05 am
Posts: 1111
Location: Phoenix
Move the adjusted variable so that it is defined as a member variable of your class. It looks like you are trying to circumvent object oriented programming, which is generally not a good thing in Java, as it just makes things harder. But I'm pretty sure you have to define a class. So using your example snippet of code it probably looks more like:

Code:
public class SomeClassName
{
  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);
  }
}


What you need to do is define "adjusted" right below the class definition, and define it as static.

Code:
public class SomeClassName
{
  static char adjusted = 0;

...

}


Edit: Your confusion comes from thinking of the main method as different than any other method. It is in some ways (it accepts command line arguments, is always static, always public, and is the entry point for a method when the program is executed) but when it comes to variables, that variable you had defined in the main method is just a local method variable name, not visible outside the main method. By moving it like I suggested, it becomes a member variable of the class, and is visible to every method in the class. And actually, since I didn't specify private, it is actually visible to any code within the package, but you probably haven't covered packages too much so ignore that for not. You can put "private" in front of the definition of adjusted if you want.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 19, 2009 10:52 pm 
Offline
User avatar

Joined: Thu Sep 24, 2009 4:57 am
Posts: 849
Ah! I knew something was up. I really should spend a few hours reading about the structure of the language. I was too eager to just dig right in and get something on screen, I suppose, and hoped I could get away without that knowledge for such a relatively simple program. I nearly managed, but nearly doesn't count. ;)

Thanks for the help Aegnor. It is appreciated.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Nov 20, 2009 2:04 am 
Offline
User avatar

Joined: Thu Sep 03, 2009 3:08 am
Posts: 6465
Location: The Lab
parsing passwords ? :p


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Nov 20, 2009 10:49 am 
Offline
User avatar

Joined: Thu Sep 03, 2009 11:05 am
Posts: 1111
Location: Phoenix
No problem Noggel, let me know if you have any other Java questions.


Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: Fri Nov 20, 2009 5:05 pm 
Offline
User avatar

Joined: Thu Sep 24, 2009 4:57 am
Posts: 849
Midgen wrote:
parsing passwords ? :p


Much more nerdy, much more benevolent! A WoW log parser of sorts, to basically show everything a person casts, in the order they cast it, including any gaps of idle time. I haven't seen any tools to do this, and I thought it would be simple enough of a project to do myself.

Doubles as a foray into programming as I am thinking about going to school for some related stuff. The last time I coded stuff was over 10 years ago, and it was erm.. in mIRC scripting language. I did make things like telnet clients and a sendmail type of script! But I don't know I'd go so far as to call it useful. ;) Between that and a QBASIC/Pascal course in my technologically-advanced high school I had a decent foundation/familiarity with coding (though clearly not OOP stuff!) which in many cases just meant learning Java-specific syntax. And up until this issue with tracking a timestamp, it made for a pretty easy transition to Java.

Now if you guys saw my HTML... ;) Good thing browsers still support HTML deprecated 10 years past!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Nov 20, 2009 5:37 pm 
Offline
Web Ninja
User avatar

Joined: Wed Sep 02, 2009 8:32 pm
Posts: 8248
Location: The Tunt Mansion
Fear not, Nogs. The HTML5 spec is actually very simplified :)


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 20, 2009 5:53 pm 
Offline
User avatar

Joined: Thu Sep 24, 2009 4:57 am
Posts: 849
Hooray!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 93 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group