Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Glenn Jayasuriya

Ranch Hand
+ Follow
since May 15, 2012
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Glenn Jayasuriya


I'm currently making a recipe book on android.
I'd like to be able to tag recipes however not sure what would be the best way to do this.

I was thinking of a table that will contain tag objects.
We could have positions 0 to 25 be the first letter of a tag, or just a hash function of a string.
Each position would have an arraylist of tags.
Each tag object would have a arraylist of recipes.

So to add a recipe just search if the tag is there, if not create the tag and add recipe to the tag object.
Search by first letter of the tag (or hash function) to get to the right column of the table, look for the tag and display all the recipes that have the tag.

Is there a better way to do this?
Any advice would be appreciated!
8 months ago

Jeanne Boyarsky wrote:Glenn,
It is a matter of style. I don't have any problems with a test having multiple assertions that cover the same scenario. Some people believe a test should only have one assertion.

I do have two observations:
1) It appears you are testing the value passed into the constructor is the value returned by getName(). If so, you could create a String variable so you aren't duplicating that String and to make the intent clearer.
2) Does the getName() method have logic in it? If it just returns the value (vs doing conversion/validation), most of your tests are overkill.

Ya it's a getter method.
I know now i shouldn't be testing that cause it's a waste of time.
I did have another question though, made  test for something that had logic:

I was reading somewhere that unit tests should be self contained.
Is it ok to declare private variables at the top like this.
Or within each method must I repeat instantiating the object in each one.
This way it's less repetitive but I worry that the object could possibly change between test methods and if it's wrong in one it could affect the other.
If that's the case i might just have to instantiate in each method.
9 months ago

Stephan van Hulst wrote:You can eliminate testName and returnedName. Just pass the name directly into the constructor, and pass the result of getName() directly to assertEquals(). Also, move each case to their own test method. One test should test one case. Rename your test methods to explain what they do: testNewRecipe(), testNewRecipeWithEmptyName(), testNewRecipeWithLongName(), testNewRecipeWithSpecialName().

My test code is in a class called RecipeTest so i don't think i need to write test for each method but I changed my code to this:

Question, these tests are for a method called getName.
Another method called getDirections could also use some of these test methods.
Am I allowed to add code in each test method that covers both the getName and getDireciton method at once?
So instead of called it "getNameSpecialCharacters" for instance I just call it "testSpecalCharacters" or something.
Or can each test method only correspond to one method of code?

9 months ago
First time doing unit tests, is this too repetitive? Thanks!

If it is too repetitive, what would you recommend to fix it?
9 months ago
I need help with the handling events issue.
Debugging is becoming more and more difficult for me.
Before it was easier but now there are times where I have no clue why something is not working.
10 months ago
Use the same loop to iterate over the array.
Use comparators like <,> etc
Keep track of the max and min value with a variable
10 months ago

Paul Clapham wrote:"Configuration" covers a lot of territory. Maybe there is supposed to be some other folder somewhere where your code expects to find files. Maybe you have to run the application as an administrator and you aren't. The list could go on indefinitely.

Don't take those guesses seriously, they are based on no evidence. You've got Eclipse running on the target machine? Good. Then run your application and see where exceptions are thrown, for a start. If that doesn't happen then you'll have to start debugging. But without any evidence of a problem you aren't going to be able to solve that problem.

And that's sorta my issue. I've already tried putting try and catch statements and as mentioned above have been running it through the debugger.
It would be easier if it would just tell me an error so I could know what to do in that case.
I might just have to rewrite this from scratch on the target computer.
Still wish I knew what the actual reason was.
10 months ago

Paul Clapham wrote:That means you haven't configured Eclipse to know where the source code is for the JRE it's using. Just click that debig button which gets you out of that method and back to whatever called it.

It doesn't have anything to do with why your code doesn't run on a different machine -- that would be because the other machine isn't configured the same way as the one you did your testing on.

Ok in eclipse i went to toolbar and went to Run --> Run Configurations.
I'm just comparing the configurations between the two machines.
The original machine had a runtime JRE of JavaSE-1.7 (Java SE 8 [1.8.0_40
The new machine has a runtime JRE of JavaSE-1.7 (java-8-openjdk-i386)
Besides that the jar files seem to be in the right places for their machines according to the sources section of the run configurations.

Am I looking at the wrong place?
10 months ago

Forgot the mention that when I step over this line it says that "this" was "source not found"
This referred to ScheduledThreadPoolExecutor so I guess it couldn't find ScheduledThreadPoolExecutor
Also mentioned this: java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@41aa62

This is what I see:
10 months ago
What I did was run eclipse on another machine. Made a new project with new java files and just copied the java code.
Imported all the jar files which removed all the error messages I was having. (Note also there were messages saying that the source level was wrong but if i just right clicked it resolved those problems for me).
I ran it in eclipse and same thing as with terminal, nothing happened.
So I ran it through the eclipse debugger with breakpoints and found my issues was with the following:

I took this code directly off the Google Spreadsheets API:
I ran the debugger for eclipse on original machine and the eclipse debugger was able to get past this part just fine.
So I'm wondering if the spreadsheet code is what is causing my problem.
Which is a problem because I didn't write it, I don't understand how it works exactly.
From my understanding it uses OAuth consent, through a google wizard you can create a client json file, you add this to your java project to be credentials to the google app.
I made sure to add the json file to the resources folder of the new machine like I did with the old machine but no luck.
I even repeated the process of creating that json file as noted in the link above and included that new json file in the new machine but I had no luck with that as well.
10 months ago

Knute Snortum wrote:You probably should include .settings, .project, and .classpath in your .gitignore file as these files are very system specific and proprietary to Eclipse.  It also exposes the internal structure of your system to the public.

Ok thank you. Honestly I don't know very much about git and just used the github desktop app on my computer to sync with github.
10 months ago

Knute Snortum wrote:It looks to me that your project is very dependent on local jars and resources.  How did you build your runnable jar?  Have you considered using Maven or some other build program?

Hi I built my runnable jar through eclipse. Export ---> Generate runnable Jar ---> package required libraries into runnable Jar.
10 months ago
Hello I'm trying to run my first runnable jar file made using eclipse on a different machine.
When I do: java -jar jarName.jar it does not do anything.
I also tried it with java -jar 'jarName".jar

To clarify what I mean by it does not do anything after I press enter no error message is generated (Note I tried putting a try and catch block around my main method).
The terminal appears frozen. I have to press control c to get out of it.

When I run my runnable jar file on my own machine it works as expected.
I also wondered if it had to do with the java versions of the machines.
The machine that compiled with eclipse was running 1.8.0_40
The machine I'm trying to run the jar file on is running java 1.8.0_121

I made a runnable jar file with just a System.out.println and it runs on the other machine so I'm wondering if it's my code.
I'm really not sure what part of my code I should put here, but here's a link to it on github:
Sorry in advance for the frankenstein messy code, I didn't really think I was going anywhere with this.

I'm just wondering why it doesn't run on the other machine and how to fix it. Thanks!
10 months ago

Stephan van Hulst wrote:Why don't you just use a WatchService to get notified when your spreadsheet changes

I'm not familiar with watchservice.
Forgot to mention that this is a google sheets document and not a excel spreadsheet. Would watchservice work with that?
10 months ago
Initially I made my code to have 1 Scheduledexecutorservice.
A linked list of dates organized by time were used in this scheduledexecutorservice.
And this worked perfectly, at a specific time and date the action would be enacted.

The issue was that if I made any changes to the spreadsheet I was drawing data from, the changes wouldn't update in my code because it would have already reached the last line of my code when the edits happened.
So instead I planned on implementing polling.
At a certain time of the day reread from the spreadsheet.
To do this I had another Scheduledexecutorservice run the code that reads the spreadsheet at specific intervals/periods, updates the linked list, and uses the original scheduledexecutorservice to schedule.
So now I have a Scheduledexecutorservice within a scheduledexecutor service.

Now my issue is with stopping time of the original (inner) scheduledexecutorservice.
For example if I deleted a row, i'd expect that whatever date and time the row had wouldn't be executed.
However if I didn't have a stop time for the original scheduledexecutorservice the row's action at the specific would still be executed because it had been scheduled before and I never stopped it!
What's worse if it repeats many times before a certain date/time before a deletion of a row then it performs the action many times instead of just once or none (if deleted).

I added a stop to the inner/original scheduledexecutor service and the best I could get was it perform 2 actions if the row is not deleted (want 1 action) or do no action (if the row is deleted which is what I want!).
My setup right now is that the outer scheduledexecutor service polls every minute (actually not going to do this frequently but for testing purposes) reading off the spreadsheet.
The inner/original scheduledexecutor stops after (61 seconds).
I did this on purpose because the dates i have are on the minute, so at 11:30:00 instead of 11:30:10 or something.

Initially what I had was the inner/original stops after (59 seconds)
But what I found out was that if there is an action supposed to happen between 59 seconds and the minute mark it won't get executed because it's after the inner/original scheduled executor service stops (what actually does the execution) and before the outer scheduledexecutorservice runs (that reads the spreadsheet and calls the inner scheduledexecutorservice).

Therefore having the inner scheduledexecutor stop a second longer than the outer ensures that I don't get the above problem at the cost of having the action performed twice.
My code is for a simple announcements program that runs on Discord which is a voip/text chat service.
So having double announcements posted at a certain time is not that big of a deal.
However I'd prefer to get this resolved if possible.
10 months ago