New answers tagged

0 votes

Reverse string with identical spaces as in original String using Java

If I needed to do this, I'd go with Ivo Beckers' / JollyJoker's approach, but here's another take on it, just for fun - and it might generalize to some extended version of this problem. You could ...
user avatar
2 votes

Calculate square of (sum of even-indexed) - (sum of odd-indexed) subarrays from an array

This problem can be treated as a candidate for Kadane's algorithm after odd indexed elements are negated. Original array A = [1,2,-1,4,-1,-5] negate odd indexed elements B = [1,-2,-1,-4,-1,5] Once ...
user avatar
  • 21
0 votes

Integer to Roman Numerals (1 - 2999)

...
user avatar
  • 1
0 votes

Class for serializing themes for my JavaFX application

You definitely have inefficiency. Consider: ...
user avatar
  • 30.8k
1 vote
Accepted

Task executor thread pool

I am assuming this is not intended for serious use, as there’s no good reason not to use the tools already provided by Java for pooling threads made available in Executors. As I am not an expert in ...
user avatar
  • 6,211
2 votes

Reverse string with identical spaces as in original String using Java

You can use IntStream.range() to get the indices of spaces. ...
user avatar
2 votes

Reverse string with identical spaces as in original String using Java

I think I would do it like this: ...
user avatar
8 votes

Reverse string with identical spaces as in original String using Java

You can make a String directly from a character array. So ...
user avatar
  • 20.5k
1 vote

Reverse string with identical spaces as in original String using Java

You don't need all that much to do this. Both the OP's solution and @coderodde's seem overworked with too many extraneous objects. One StringBuilder is needed for ...
user avatar
  • 1,302
2 votes
Accepted

Using methods to get and display userInput

numberEntered should not be static and should not be a member. It's good that you made a variable for quit, but it should be ...
user avatar
  • 49.5k
4 votes

Reverse string with identical spaces as in original String using Java

I suggest you rely on StringBuilder. That way, appending to it runs in amortized constant time. I had in mind the following implementation: ...
user avatar
  • 25.7k
0 votes

My LRU Cache implementation in Java

Advice In order to improve both memory fingerprint and the CPU cycles consumed, instead of Map<Integer, Node>, you should roll your own simple linked list &...
user avatar
  • 25.7k
0 votes

Clean up multiple if/else statements by using an enum

It depends on the rest of the code, but if what you posted above is the only place it's used, then I'd call your change a significant YAGNI violation. By itself, it replaces a long conditional chain ...
user avatar
  • 1,344
0 votes

Google Foobar - "Distract the Trainers"

Hey I'm super late to the party but I just solved this. I think the key is realize that the sum of the bananas of any pair never changes. ...
user avatar
  • 9
0 votes

Implement simplified liquibase

Some thoughts: STOP_FOLDER_NAMES should be a Set for O(1) lookups. It should definitely not get converted from an array into a ...
user avatar
  • 6,211
4 votes
Accepted

Split word based on terms from an array

The goal when writing code should almost always be to optimize for readability. Only worry about performance when you have a known performance bottleneck. Readable code can always be rewritten to be ...
user avatar
  • 6,211
3 votes

Split word based on terms from an array

Testing for tempArr[0].length() < firstWord.length() looks unwarranted. Nothing in the problem statement requires the first word to be as long as possible. ...
user avatar
  • 53.4k
5 votes

Split word based on terms from an array

I provided the following solution, does anybody have something a little bit more elegant? Depends on how you define elegant. Shorter? More readable? Extendable to not only include string matching but ...
user avatar
  • 651
0 votes

Simple Java Tetris game

Is my code too simple? We need to unpack that a little, because we're probably operating on different definitions of "simple". Your application has only one file and one top-level class. ...
user avatar
  • 49.5k
1 vote
Accepted

Spelling Java long values to human readable text in English

Some thoughts Looking up names for numbers Your WORDS Map looks rather like a "code smell" - otherwise you'd have been able to find a much better name for it. In my suggestions for your ...
user avatar
  • 1,302
0 votes

Java - Select the maximum integers from sliding windows over an array

There may be avoidable overhead in finding the same key in a Map twice in a row, OTOH, an "industrial strength" Map implementation might cache the last ...
user avatar
  • 4,548
2 votes

Simple Java Tetris game

Separation of Concerns You seem to have all the code in one class, which makes it easy in the beginning, but very hard the more functionality you add. Having everything in one place makes code less ...
user avatar
  • 651
0 votes

Spelling Java long values to human readable text in English

I think the Pair class is not necessary, you could just use a LinkedHashMap and call ...
user avatar
  • 113
1 vote
Accepted

Snake Game Clone in JavaFX

On originally writing this answer I had ignored your CSS because you hadn't yet included it. You should be using your own package declarations. Many more of your ...
user avatar
  • 49.5k
1 vote

Spelling Java long values to human readable text in English

I think your methods spell and convertHundredsImpl are too long and have too many nestings. And by too many nestings I mean ...
user avatar
  • 651
1 vote

Spelling Java long values to human readable text in English

I'd say that the guard clause can be a little refined to look more like a guard clause: ...
user avatar
  • 651
4 votes

Converting int values in Java to human readable strings in English

Naming class IntToHumanReadableStringConverter { I would consider "123" to be a human readable string of an int. ...
user avatar
  • 20.5k
0 votes

Converting int values in Java to human readable strings in English

Comment by @PeterCsala: It is minor, but instead of these num -= billions * 1_000_000_000; calculations you could use simply ...
6 votes
Accepted

Converting int values in Java to human readable strings in English

I'm not keen on the class name or method names - see my alternatives below. I find perhaps a little more logic in the code than I'd look for - normally I try to find some way of making this sort of ...
user avatar
  • 1,302
8 votes

Converting int values in Java to human readable strings in English

For the input -2147483648, your program outputs just minus . Also, you never handle 0 ...
user avatar
1 vote
Accepted

MadLibs builder

Please just keep in mind that it's written to be at a High School student level, not a college or professional level. Fine, but: I'll also claim that whereas high school students are inexperienced, ...
user avatar
  • 49.5k
2 votes

An iterator returning all possible solutions to n-queen problem in Java - follow-up

I find nothing worth mentioning about QueensIterable but the visibility of final int dimension, kept for its iterators: ...
user avatar
  • 4,548
1 vote

Generic LRU Cache in Java

Thanks for a very solid first question. Here are my thoughts, randomly assembled: General In idiomatic java, class-level variables appear at the top of the class, not the bottom. This formatting ...
user avatar
  • 6,211
1 vote

Text Based Minesweeper in Java

Users break stuff... As soon as you start dealing with user input, no matter how you get it, they're going to end up sending you invalid input. You're not currently validating any user input, which ...
user avatar
  • 11.5k
2 votes
Accepted

Figure out initial balance for bank accounts to avoid them going into negative balance after transfer transactions

The problem with your solution appears to be that B's balance doesn't change when B makes a transfer out of their account and B had a high enough starting balance to do so. And vice versa for A. You ...
user avatar
2 votes

Text Based Minesweeper in Java

Does the game logic belong in the class or in the main method? I'd argue that your question already has the answer in it, although through a minor hidden detail: Does the game logic belong in the ...
user avatar
  • 651
4 votes
Accepted

Text Based Minesweeper in Java

In my opinion, the main method should be used to set up and start the game. It should not be responsible for running it, so the loop would be in an incorrect place. ...
user avatar
0 votes

Table Driven Test in Java using Junit4

They are combining features of test runner with the test contraption. This is why the code has to "catch exception". If they use a prebuilt test runner for table driven tests feature (as ...
user avatar

Top 50 recent answers are included