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
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? ...
87
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. ...
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
157 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
755 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
557 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
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
798 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: ...
148
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
378 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
300 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 ...
24
votes
2answers
56k 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
185 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
942 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 ...
8
votes
1answer
48k 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 ...