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
12
votes
4answers
51k 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
803 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
812 views

Making a job completed, rolling back on error

I have the following construction in the program: ...
4
votes
1answer
682 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: ...
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 ...
7
votes
2answers
379 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
301 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 ...
25
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
186 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
537 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 ...
11
votes
10answers
26k 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
167 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
945 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
49k 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
16k 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 ...
13
votes
1answer
15k views

OAuth Provider token generation

I'm currently creating an OAuth provider in Java using Jersey. To the best of my knowledge Jersey does not provide a method to create oauth tokens so I'm creating my own. For those unfamiliar with ...
5
votes
2answers
807 views

Thread Safe Service to Perform Tasks in a Queue

In answering a question on SO, I provided this example. The intention was to provide a thread safe service that queues requests to make directories. I figured I could definitely use some review on ...
11
votes
4answers
1k views

Different character outputs using loops and strings

I've just given this as a response to an absolute beginner on SO, and I'm wondering how terrible it is. Output for an odd number of lines: ...
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. ...
2
votes
1answer
2k views

Hibernate proxy converter for GWT

Here's a class that converts Hibernate proxies to normal classes. This is useful with GWT for example, when sending objects loaded from database to the GWT client. Please review it. ...
4
votes
1answer
464 views

Dao function using hibernate

I'm just wondering if the following is the best way to write a dao function. Should I get the entity manager before the transaction and close it after the transaction every time? Should I write ...
7
votes
2answers
2k views

How to increase download speed using the following method?

I have written an Android application which downloads pdf files through web service. I am using kSoap2 android library to parse the response of web service which basically contains file name & ...
6
votes
1answer
1k views

Optimized thumbnail generation

Here is a snippet of code that generates a thumbnail from a given file. It works well, but is much slower than desired. Without using an external library (which I'm trying to avoid for learning ...
6
votes
3answers
15k views

Data validation for preventing blank entries

I want to create a validation wherein blank entries will not be accepted. So if I call this code: ...
4
votes
3answers
3k views

“Consecutive Heads” program

So... I have a program in which I want to flip heads three times in the row. What I'm asking for is for proposals of other solutions for this program, in pro way, as You do in natural sense. That's ...
15
votes
2answers
2k views

Guessing game - am I using the concept of objects in Java properly?

I'm new to Java and find it hard to grasp the concept of objects. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly. The objective ...
8
votes
1answer
1k views

Implementing a fast 'split' function on a stream of characters in Java

I was writing a piece of code (a custom EntityProcessor for Solr, but that isn't too relevant) designed to break lines of input based on a string delimiter (...
6
votes
1answer
2k views

Parser for CNF formulas

I have a parser for CNF formulas in Dimacs format, which is very slow. Any suggestion on how to improve its speed? I did some profiling and I might have to replace ...
9
votes
5answers
3k views

Providing unchecked exception “wrapper” interfaces for an API with checked exceptions

I recently had a discussion in the forum of an API, because they changed an exception from checked to unchecked. I believed it needs to be checked, because it is recoverable. The arguments of "the ...
7
votes
1answer
561 views

Waiting for game server connection

...
8
votes
3answers
3k views

Java synchronous queue implementation

I just want to make sure there aren't any deadlocks or inconsistencies (the code is also available on github). Disclaimer - In real life, I would not implement a queue myself, but use an existing ...
11
votes
2answers
810 views

Setting up radio button click listeners

Do you have any suggestions for improving it? Instead of writing four almost identical setOnClickListener method calls, I opted to iterate over an array, but I'm ...
7
votes
2answers
1k views

Multithreaded log test

I'm writng a logger extension that allows multiple threads to log a process and then dump that log to the main log in one atomic operation. The point of it is to make the logs easier to read when many ...
7
votes
4answers
512 views

Spawning threads to process data

I have a class that spawns threads to process data. I am working on instrumentation code. When the process is started, time = System.currentTimeMillis(); When it ...
1
vote
2answers
214 views

Function to turn to requested bearing

I've been writing a function: float turnToRequestedHeading(float initialHeading, float requiredHeading, float turnRate) I keep thinking there must be a clever ...
11
votes
3answers
19k views

Simplified Android Spinner

I have written a simple spinner wrapper, but was wondering if any of you could think of any ways to make it more robust. It only handles strings at the moment. ...
7
votes
6answers
452 views

Arguments in constructors matching fields

What is your opinion in arguments in constructors matching members as in the following example ...
11
votes
3answers
2k views

Pattern Against Anemic Domain Model

I would like to solicit advice on everyone's thoughts on how best to combat the Anemic Domain Model anti-pattern when building out a system based on web services. One of our goals is to build a set ...