Questions tagged [java]

Java (not to be confused with JavaScript) is a class-based, object-oriented, strongly typed, reflective language and run-time environment (JRE). Java programs are compiled to bytecode and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.

Filter by
Sorted by
Tagged with
150
votes
14answers
46k views

Remove Last Comma

I want to join strings together, but when doing this it often happens that there is a comma too many, and therefore I need to remove that comma. In this code, I use the ...
100
votes
9answers
26k views

'100' is a magic number

Magic numbers are bad... I totally agree. But there's one magic number I find hard to fix: '100' is a magic number. Consider this code: ...
88
votes
6answers
56k views

File Browser GUI

FileBro is a basic GUI based File Browser. FileBro Functionality Directory tree - shows the file system roots at start-up, but is otherwise built lazily as the user browses around the file system. ...
84
votes
5answers
15k views

Load fifty million integers as quickly as possible in Java

I am working my way through Project Euler. Many of the problems deal with prime number calculations: this is the type of code that can be extracted into a separate class and reused. While calculating ...
77
votes
11answers
11k views

Nesting versus GOTO: which is better to avoid?

In Java they're not really known as GOTO statements and are rather referred to as Branching Statements, but I find that the former term is a bit more indicative of ...
75
votes
5answers
6k views

Guessing a number, but comments concerning

I always get marked down for my comments and I just wanted to see if these comments are acceptable or what I should include/where I should include them. ...
67
votes
6answers
184k views

Design a chess game using object-oriented principles

I would like to know if my approach is correct and how could it could be improved? Also, is there a way to get rid of the relation between the Piece and the ...
57
votes
2answers
3k views

You are being watched! - Comments of Interest

You are being watched Code Review has an open system A machine that spies on you on every hour of every day I know because I built it. I designed the machine to detect suggestions to post on ...
52
votes
8answers
34k views

Efficiently checking if a number is divisible by 3

I'm looking for code review, best practices and optimizations. ...
51
votes
6answers
78k views

Is it OK to use while ((line = r.readLine()) != null) construct? [closed]

I want to refactor the following code because I don't feel comfortable about using assignment inside comparison operator. It looks like pretty idiomatic C, but do you think this is a good practice in ...
50
votes
5answers
241k views

Removing elements on a List while iterating through it

I needed a way to remove elements on a List while iterating through it. Supposedly something like this (illegal code): ...
50
votes
3answers
3k views

Analyzing Minesweeper Probabilities

Calculating probabilities in Minesweeper might sound like an easy task, but I've seen so many probability calculators that's either incorrect, horribly slow, or with ugly code (or all of them) so I ...
47
votes
12answers
20k views

Searching in an array in less than O(n) time

I have an array where each element is either one less or one greater than the preceding element \$\{x_i = x_{i-1} \pm 1\}\$. I wish to find an element in it in less than \$O(n)\$ time. I've ...
46
votes
4answers
9k views

Ternary operation in Java - isn't this abuse?

I think the below code is difficult to understand. I also feel it abuses Java's ternary operator. ...
46
votes
3answers
9k views

Sorting millions of integers

Last Friday I was hit with a sorting interview question that I never really had to deal with. Develop a your own sorting algorithm. It cannot use any other Classes for help. It needs to ...
46
votes
5answers
14k views

Thread-Safe and Lock-Free - Queue Implementation

I was trying to create a lock-free queue implementation in Java, mainly for personal learning. The queue should be a general one, allowing any number of readers and/or writers concurrently. Would you ...
43
votes
4answers
167k views

Simple Example of an Iterable and an Iterator in Java

I was a TA for a first-year Java programming course this year. As a very simple example of iterables/iterators, I wrote the following code for the students. I am curious if any styling or other ...
42
votes
7answers
14k views

Interview coding test: Fizz Buzz

I was recently given a coding test to complete for a potential client. It was a FizzBuzz-type of thing with a two-hour time limit. I had a request to write basic FizzBuzz, then add a special case, ...
41
votes
2answers
852 views

Tool for creating CodeReview questions

To download the tool and for usage instructions, see the follow up to this question Description I have realized that there's a whole bunch of code I want to have reviewed, but copying file by file, ...
40
votes
5answers
3k views

Regularity in the “Rusty Towel of Mutual understanding”

I have the following Java class: ...
39
votes
6answers
3k views

SHOUTY_SNAKE_CASED NUMBERS

WE_ALL_LOVE_SHOUTY_SNAKE_CASE_SO_I_WROTE_A_PROGRAM_FOR_IT!!1! Alright, enough of this. This is a rags-to-riches from this question. The pattern is pretty simple, it takes a ...
39
votes
7answers
3k views

Is my code a 'safe' singleton?

I was wondering if my code will produce a true singleton. I am creating an Android app, and all activities should access my API through one instance of the SyncApi ...
38
votes
5answers
24k views

Designing a coffee machine

I was recently rejected from what looked like a really promising string of interviews. I did very well in a questionnaire style review, and then they handed me this assignment (more or less): ...
38
votes
5answers
31k views

Incrementing Integers in Map

I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
38
votes
2answers
972 views

Brainfuck to x86 Assembly Compiler

After my Brainfuck Interpreter written in x86 Assembly I decided that it was time to get to the next step, writing a Brainfuck Compiler in Java that generates x86 Assembly and compiles that to an ...
37
votes
10answers
7k views

Which FizzBuzz is better, and why?

I was in an interview and the guy asked me to do the typical FizzBuzz question of printing numbers 1-100, but for each factor of 3 print Fizz, each factor of 5 print Buzz, and each factor of both ...
37
votes
8answers
8k views

The FizzBuzz challenge in Java 8 written in a short, readable and interesting way

I decided to take on the FizzBuzz challenge with as twist that I would use Java 8 concepts to make it a bit modular, yet still let it be a short, readable and understandable program. This in contrary ...
36
votes
11answers
27k views

Perform instruction in loop every time except the last time?

The specific issue is this: I am writing to a file and want to output a new line after each line written. If I use a normal loop without any further checks, this will create a blank line at the very ...
36
votes
2answers
5k views

Quicksort implementation seems too slow with large data

I've written a program to compare different sorting algorithms with the array size being 10, 1,000, 100,000, 1,000,000 and 10,000,000. I, of course, expect Insertion to win out on 10 and merge heap ...
35
votes
12answers
21k views

Checking whether a number is a power of 10

Is there a better way to check whether a number is a power of 10? This is what I've implemented: ...
35
votes
8answers
4k views

Immutable objects in Java

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since I have getters to ...
34
votes
6answers
36k views

Refactoring large switch statement [closed]

Lately I have been trying to extract till I drop. Well not necessarily till I drop, but I've been trying to be more strict and look at some metrics of my code. I have now come along an old class of ...
33
votes
5answers
12k views

Find min of 3 numbers hardcoded

...
33
votes
5answers
145k views

Implement a Shape abstract class

To learn more about OOP, @nhgrif challenged me to implement a Shape abstract class (more details in code remarks below. Here is how I did it. Any and all advice appreciated! Shape.java ...
33
votes
4answers
1k views

Memory with a twist

Description This is the good old game Memory with a twist: Every time you pick a wrong pair, the two tiles you chose will switch their location. So sometimes you might think that a tile is at one ...
33
votes
2answers
5k views

Asynchronous network callback code

I did not get the job after submitting this piece of work in an interview, but I have no feedback to know what "BAD" things are inside this block of code. The requirements are: Connect to the ...
32
votes
5answers
45k views

Java enum containing a hash map for look up

I have the below enum. I need to get the description by code. It is working but can it be improved still? ...
32
votes
2answers
6k views

Weekend Challenge - Poker Hand Evaluation

Weekend Challenge #2 - Poker Hand Evaluation Very early I decided to support the usage of wild jokers, even though I knew that this was going to lead to trouble more work. I also wanted to support ...
32
votes
2answers
32k views

Custom CursorAdapter Design

Here's the code which I use for my Android custom cursor adapter which I use to bind data to my list view. ...
31
votes
10answers
13k views

Generating Even Random Numbers

I have this class which is used as part of a game. It needs to generate Random Even values, which is done by generating random numbers until the result is even. Is there a better way to do this? ...
31
votes
3answers
883 views

Advanced and Detailed Minesweeper Probabilities

In an earlier question, I showed my code for calculating the mine probability for each and every field in a Minesweeper board. But it doesn't stop there. In that very same Minesweeper Analyze project, ...
30
votes
7answers
53k views

Password validation in Java

I just wrote this password validation code function that checks to see if a password will be accepted in AD or not. I'm not sure this is the best way to do it, but for now it works fine. I would love ...
30
votes
5answers
7k views

Print an ASCII diamond

This takes a width specified by user and prints a diamond of that width. It uses only three for loops, but could I reduce that further? Is there a more elegant ...
30
votes
6answers
4k views

Unbreakable average program

I'm creating a program that averages four numbers - three from a file and one from user input. Simple enough. But the catch is, the professor is going to be deliberately trying to break the program. ...
30
votes
5answers
26k views

Int overflow check in Java

I have a piece of code that takes a couple of integers and check if performing an addition on the inputs would result in an overflow. I was wondering if this code is SOLID: ...
30
votes
1answer
4k views

Full-color clone of Conway's Game of Life, with a decent GUI

I wrote this to learn JavaFX, and as an excuse to re-make the Game of Life. This is the most complex GUI I've ever written, so I'd like feedback mainly on it, but I'll welcome any criticism! My ...
29
votes
7answers
7k views

100 gunmen in circle kill next person

The question is here. 100 people are standing in a circle with gun in their hands. 1 kills 2, 3 kills 4, 5 kills 6 and so on till we are left with only one person. Who will be the last person alive....
29
votes
7answers
64k views

Check for balanced parentheses

Given an expression string exp, write a program to examine whether the pairs and the orders of "{","}","(",")","[","]" are correct in exp. For example,...
29
votes
4answers
9k views

Beginner object-oriented text-based RPG

I'm in the process of writing a text-based RPG in Java using object-oriented programming. I'm fairly new to programming and currently learning through CodeHS and coding this within their sandbox, any ...