Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

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
11
votes
3answers
15k views

LinkedHashMap as LRU cache

I have a simple implementation for a LRU cache using LinkedHashMap. I want it to be as generic as possible. This is not for production use, just practice, so I ...
9
votes
2answers
26k views

How can I avoid unchecked cast warning in my generic recursive Iterator?

It's somewhat odd that Java's collection framework has no iterator for recursive data structures. Since I needed something like this, I wrote my own. First off, I need recursive elements: ...
13
votes
5answers
16k views

Remove useless whitespace from styled string

I have a big string resource (basically the "about" text of the application) which contains styles (such as <b>, <a> ...
8
votes
5answers
2k views

Simplify a loop [closed]

What would your preferred loop type be for a case like this: StringBuilder sb = new StringBuilder(); for(Integer id : idList) { sb.append("?,"); } In short: ...
2
votes
1answer
113 views

Externalize to a function

I have the following code block which I'd like to refactor into a method, I just don't see a good way to get the following code into a method which doesn't have multiple responsibilities. Here is the ...
8
votes
2answers
1k views

Adding a Fish Eye Distortion to a Bitmap

I have the following class that processes a Bitmap to place a fisheye distortion on it. I've run my app through TraceView and found that virtually all the processing time is spent looping through the ...
5
votes
1answer
4k views

Simple multithreading task

I wrote program which simulates the work of the restaurant. It works but I would like to change it because I think there is better solution.Thanks for your suggestions. Client.java ...
4
votes
1answer
5k views

EPUB reader for Android

How do I improve this code for reading an epub file? The code is as follow: ...
3
votes
1answer
152 views

Obtaining column status definition

I know exactly how I would handle this in PHP, Python, JS, and AS, but I want to confirm that this is the correct approach to Enums in Java. I have a database view which has a status column. It will ...
4
votes
1answer
6k views

Unit Converter App

Please review this Android UnitConverter App which has been designed using Strategy Pattern. ...
5
votes
2answers
733 views

Code review - parse a query to arrays

I have a query like this "abc=1, def=2, ghi=3" and I need to parse it into arrays like this ...
5
votes
3answers
4k views

Java using PostMethod multiple times

I found a example of Java PostMethod here. But I want to post several times by using for loop. I made it like this. ...
3
votes
4answers
4k views

Iterator pattern/iterator class

I have implemented a simple iterator. Just want to check if I have missed anything. ...
6
votes
3answers
5k views

fix ugly initialisation in while loop over queue

I have a loop structure, that I really don't like. It's the initialisation I find appalingly ugly. Is there a nicer way to do the following? ...
89
votes
6answers
57k 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. ...
10
votes
3answers
3k views

Linked list loop detection in Java

A schoolmate is going through the interview process, and was given the question of detecting if a linked list data structure has an infinite loop in it. I wrote this example, and wanted to see what ...
11
votes
15answers
1k views

Java practise exam question

Doing practise questions for a Java exam which have no answers (useful) I have been asked to do the following: Write a class called Person with the following ...
4
votes
2answers
159 views

jInternalFrames code

I'm new to this page I want to ask you to critique my code on jInternalFrame. Basically the problem is that there is no complete example on using this, so I wrote this "template" but I don't know if ...
4
votes
1answer
2k views

A random relative prime number based on the modulus

I am implementing RSA in java using BigInteger and I have a method to return a random relative prime number based on the modulus m. The method looks like this: ...
5
votes
1answer
8k views

Is there another way / proper way of parsing this JSON array ?

I have a JSON object which holds some json_array like this: (with org.codehaus.jettison.json.*) ...
10
votes
2answers
771 views

Remake of Java's doubly LinkedList

I've been learning to program in Java and I decided to implement a doubly linked list as practice and as a bridge project before going on to learn C++. I know Java includes a ...
14
votes
3answers
560 views

Return explicitly or implicitly?

Consider the following two code examples: 1. void foo(boolean cake) { if (!cake) return; // TODO some work. } 2. ...
5
votes
2answers
4k views

Producer Consumer scenario implementation in Java

Please suggest improvements in the following java program I've written for producer-consumer scenario. The program seems to be working fine. Does it suffer from possible deadlock scenarios? How better ...
12
votes
4answers
52k views

Simplify splitting a String into alpha and numeric parts

Requirement: Parse a String into chunks of numeric characters and alpha characters. Alpha characters should be separated from the numeric, other characters should be ignored. Example Data: Input ...
6
votes
3answers
814 views

Tetris clone in Java

I have a representation for each initial shape as an Enum with points and another for each rotated shape in the same Enum. Here ...
3
votes
4answers
1k views

Days of life calculator for first-year mathematics students

I'm giving a lecture on basic programming to first-year mathematics students. The programming language is Java (defined by curriculum). Last week I gave the following task: Write a program, that ...
4
votes
3answers
813 views

Making a job completed, rolling back on error

I have the following construction in the program: ...
4
votes
1answer
684 views

Parsing random phrases from a file

This essentially reads a specially-structured file from the scanner and then parses random phrases and prints them: ...
8
votes
5answers
5k views

Aborting a program for invalid floating-point input

My friend does a bunch of Java work (commandline, still toying around) and I notice a bunch of try/catch blocks like this: ...
7
votes
5answers
14k views

Comparing Color objects with == [closed]

This example comes from a code snippet posted on Stack Overflow: ...
149
votes
14answers
47k 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 ...
7
votes
2answers
386 views

Handling optional parameters in a servlet

I have a form that submits to a servlet that has optional parameters. If they are empty they are just ignored. This is the template I am using: ...
11
votes
5answers
5k views

Version comparison function

I have this version comparison function in java. Any cleaner way to write this without all the nested 'if' statements? Note that the suggestions must be in Java code only. ...
4
votes
1answer
307 views

How can I make my decrypt method more efficient?

I am making a simple transposition cipher. I am testing the program with the word "computer" with the secret key "5,2,8,3,7,1,4,6", where the encrypt method will put the 5th letter 'u' in the first ...
26
votes
2answers
57k views

JAXB XJC code generation - adding @XmlRootElement and Joda DateTime

Here's an interesting scenario that I encountered the other day. I did eventually reach a solution on my own. However, I'd welcome any comments and better approaches. The requirements I want to ...
-1
votes
1answer
187 views

Specify table data in code, reference objects

...
7
votes
5answers
3k views

Accessing the only element of Java ResultSet

Does this code look OK? ...
11
votes
1answer
539 views

Is this the way to use HibernateCompositeUser type for handling localized contents?

I am trying to use HibernateCompositeUser type to handle i18n specific data in my application. I am trying to use the below approach. I have a table named ...
10
votes
10answers
27k views

Optimizing Java Anagram checker (compare 2 strings)

An anagram is like a mix-up of the letters in a string: pots is an anagram of stop Wilma is an anagram of ilWma I am going through the book Cracking the Coding Interview and in the basic ...
1
vote
4answers
3k views

How to make this TLV Reader class look nicer and work smoother?

I am quite new to Java and was trying to improve my skills a little by doing a binary file reader. The choosen one was the TLV format. I've chosen that one for being reasonably simple, but not so ...
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 ...
2
votes
1answer
168 views

Can you make this Key-interable view of a List of Maps better?

I have a list of map entries, and I need an iterable that returns the keys. Of course, we could be naive and copy over into a new collection of the desired type, but that's inefficient. So let's ...
3
votes
1answer
3k views

Shuffling a JSON array in Java

Since I did not get a satisfactory answer here (and I really needed to get it going on this weekend), I decided to implement my own Fisher–Yates shuffle, porting the code I found in other SO posts. I ...
2
votes
1answer
2k views

Querying the same fields for many different values on AppEngine

I'm creating a gradebook. Many students earn a grade in many standards each. I'm hoping to get help analyzing the code below for efficient use of asynchronous result fetching. ...
2
votes
6answers
957 views

Change HashSet to a set of strings found in a file

This function changes a HashSet in the root of the class called 'commands' to a set of strings found in a file at "plugins/UC/Commands.txt" (it's what was requested by the people using the plugin). ...
6
votes
1answer
4k views

Interfacing with RESTful web services

The following working function seems a bit hacky to me as it has been pieced together from a handful of (probably poorly written) online tutorials, and I want to know if there is a better (or more ...
9
votes
1answer
50k views

JPA Query to Return a Map

I wrote the following JPA method that queries a database to return a distinct list of a String field (called mtfcc) of a Location object. I then took the list of strings to query for the associated ...
22
votes
2answers
17k views

Texas Hold'em in Java

I have been a programmer for 12 years, mainly ERP software and C development and am looking to make a career/specialty change to Java. I've read it countless times if you want to learn a new language ...
4
votes
1answer
1k views

Cryptographic key exchange

I initially posted this on StackOverflow, and was redirected here. I have been playing around with Bouncy Castle (Java) for the last few days, and I've reached the point where I believe I can securely ...
5
votes
2answers
4k views

MVC Controller in Java Swing Apps - Singleton or public static

This article has left me a little confused. In explaining a method on how to design MVC into Java Swing apps the author defines the controller class as Singleton and relays a series of calls to the ...