tb-
  • Member for 9 years, 1 month
  • Last seen more than a month ago
3 answers
votes
5k views
Attempt at method to draw diamond (recursive or not ?)
3 votes

You can make this a recursion exercise with two parts: 1) Write a recursive method to repeat a char n times. Signature: public static String getCharRepeatedTimes(final char c, final int times) (...

View answer
1 answers
vote
2k views
Iterating over an Array and an Iterable (List)
Accepted answer
2 votes

I would be happy with: @Override public boolean match(final InetAddress inetAddress) { final byte[] bytes = inetAddress.getAddress(); int end = Math.min(this.rules.size(), bytes.length); ...

View answer
4 answers
votes
219 views
Number pyramid in Java
0 votes

The recursive looks the cleanest. To extend the normal loop approaches: public static void printNumbersTriangle(final int start, final int end) { if (!(start <= end)) throw new ...

View answer
2 answers
vote
914 views
Encapsulating logging and throwing exceptions into a method. Bad practice?
2 votes

if (var3 == null) { logMessageAndThrowException("var 3 is null"); } return var3.toString(); // NetBeans warn me here for a NPE Netbeans probably does not look into the method. Netbeans does not ...

View answer
5 answers
votes
4k views
Check if a Binary Tree <String> is aBinary Search Tree
Accepted answer
-2 votes

As mariosangiorgio already said, the concept looks good, with some small improvements possible. You should write some tests to test it. You would easily find out the mentioned problems with some ...

View answer
2 answers
votes
278 views
Using volatile instead of synchronized for a simulation
1 votes

I do not think, that this will work as you want. In general, I would avoid the use of volatile. Only use it if you really have to, because of some requierements. Otherwise, use the java.util....

View answer
2 answers
votes
128 views
Adding a list of brokers to a local SQLite database
Accepted answer
4 votes

A classicel one. String concatenation with the + operator is very inefficient, because it creates a new copy of the string every time. See for example item 51 in Effective Java from Joshua Bloch or ...

View answer
1 answers
vote
4k views
Java concurrent Map of List
Accepted answer
1 votes

Ok, after the hint from the comment it makes more sense for me. I felt a bit bad because of the wrong comment, so I have investigated this deeper. As far as I see it, there is only one problem which ...

View answer
1 answers
votes
103 views
Inserting data into column family
Accepted answer
1 votes

It looks like the pattern is alway the same, I am right? Then you could just do: String commonColumnValue = "{\"lv\":[{\"v\":{\"regSiteId\":null,\"userState\":null,\"userId\":" + userId + "},\"cn\":1}...

View answer
1 answers
votes
6k views
Timer/timertask to destroy a process that overruns a defined time limit
3 votes

The main idea looks ok for me. Use a timer with a given time to call the destroy. I would separate both concerns. One class is responsible for external program handling and it will provide a stop ...

View answer
2 answers
votes
736 views
What to do with the Exception when you fail to close() a resource?
1 votes

From your description, I assume you only do reading. At least, you only mention FileNotFoundException, no write exceptions. If you do only reading, you can most likely do it in this way, with a small ...

View answer
1 answers
votes
16k views
Print all binary strings of length n
3 votes

Is there any way to make this shorter or more efficient? Yes, we can do some things. for(int i = 0; i &lt; Math.pow(2,n); i++) You calculate the pow for every iteration. The pow call takes some ...

View answer
1 answers
vote
1k views
Interest calculator
2 votes

are the 'if' statements too restrictive? I am not sure if the statements correspond the the given specification. You have to clarify the corner cases. I would say, 1000.01 belongs to 2%. Rest is ...

View answer
1 answers
votes
39k views
Remove duplicates from string without using additional buffer
Accepted answer
11 votes

Thats not working. Try the input: abaa (result: aa) I do not know what you mean with "without using additional buffer", because finalValue is at least an additional buffer (which is kind of necessary....

View answer
1 answers
votes
101 views
Review of the following methods of a binary tree which contains positive integers as it's elements
Accepted answer
1 votes

Even if you have to use pseudocode, you should do some checks. Either in pseudcode (calculcate by hand) or implement it in any language. Most of them look ok, some comments: return search(i, ...

View answer
3 answers
vote
257 views
Primes Tester for speed performance
Accepted answer
3 votes

The goal of my code is to be faster (and correct). I'm having difficulties achieving this. Suggestions? This is a neverending task. My suggestion is to stop performance improvements without a goal. ...

View answer
1 answers
votes
12k views
Is this the way of truncating the fractional part of a BigDecimal when its scale is zero in Java?
Accepted answer
8 votes

Is there a better way to do this? Yes, stripTrailingZeros(). To check your tests: public static void main(final String[] args) { check(truncate("12.000000"), "12"); check(truncate("12.0001")...

View answer
4 answers
vote
685 views
Random number wrapper class
0 votes

BTW Is it true that not using a Random object improves perfomance? No (not in your case). Math.random() just creates an instance of Random: //from java.lang.Math public static double random() { ...

View answer
2 answers
votes
69 views
Is my logic correct checking for a newer remote file?
Accepted answer
3 votes

Well, if it works, it works. There is not much to say about this small part of the code without any context. You should know this by yourself, because you should have some tests. For the code: The ...

View answer
4 answers
votes
171 views
Nested condition subset of the top one
1 votes

As far as I understand the code extract, you want to create the state if it does not exist, otherwise update it if necessary. I would handle the special case first and continue with the normal work: ...

View answer
3 answers
vote
8k views
How should I read coordinates from a text file?
2 votes

As this is posted on codereview, let us take a look at the whole function. private static List&lt;Position&gt; getPositions(File myFile) throws FileNotFoundException, IOException { I would ...

View answer
1 answers
vote
15k views
Looping through the ResultSet efficiently and the add the values for columns in List<String>
Accepted answer
2 votes

I am not that familiar with JDBC, but I think the method you search for is: isBeforeFirst: "true if the cursor is before the first row; false if the cursor is at any other position or the result set ...

View answer
1 answers
vote
935 views
Log and Count the exception in a Single method
Accepted answer
1 votes

Is there any more cleaner way of doing the same thing? Meaning right now I am counting the exceptions in a method and then printing out the exceptions in the next line inside the catch block. Is it ...

View answer
3 answers
votes
121 views
Defining of new, temporary, variables or usage of already known ones?
3 votes

I agree in a lot of thoughts from Quentin Pradet and mariosangiorgio. I will add some other viewpoint: Is it better to use temporary variables or directly usage of already existed object? I can ...

View answer
2 answers
votes
2k views
multi-recursive replacement function
2 votes

Some thoughts about the given code: /** * &lt;p&gt;Add to 'targets' all possible variations of 'source', with replacements defined by 'translationsMap'. * &lt;p&gt;For example, after the following ...

View answer
4 answers
votes
11k views
Getting country based on IP
1 votes

I would agree to the comment Answer from @Sjoerd. The slowest part is the http request. You could save some very little time to access this in other ways, but most probably, the packet transfer times ...

View answer
2 answers
votes
221 views
Launching a multithreaded simulation of smokers
2 votes

I will add some thoughts about the code. Some other things were already said by radarbob in the corresponding answer. import java.util.Scanner; import java.util.concurrent.CountDownLatch; public ...

View answer
2 answers
votes
2k views
Spotify's "Reversed Binary Numbers" Problem
3 votes

I do not see any problems inside the given range. I would have used already existing methods: public static int reverse2(final int n) { return Integer.valueOf(new StringBuilder(Integer....

View answer
2 answers
votes
8k views
Custom parser for named parameters in prepared statement
Accepted answer
5 votes

If possible, I would completely avoid dealing with raw String for SQL. This is error prone. There are some alternatives in the java world, like hibernate or method chaining (a quick google search ...

View answer
2 answers
votes
524 views
Helper class to memoize DateFormats application-wide
Accepted answer
3 votes

It looks like the questions are very specific. You should probably formulate a goal. Is your main goal to reduce code duplicity? Memory usage? Speed (however we define it here)? Something else? A ...

View answer