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
3
votes
1answer
75 views

Passing around Connection

I have a myCon=getUnusedConnection() method that gets a connection from Connection pool. I also have a ...
7
votes
1answer
25k views

Parse strings and convert the value to .java types

Below is a generic code for parsing strings (read from file for example) and converting them to .java types, such as primitives (and their wrappers), java.io.File, Enum etc. There is also possible to ...
1
vote
1answer
166 views

min and max simultaneously

I have two numbers: a and b. I want to put the smaller of them in one variable, and the larger in another variable (if they are equal, then any of them can be in any of the variables). Currently my ...
5
votes
1answer
162 views

Optimize list comparison method?

I have below Java code to compare two list values with some condition: ...
4
votes
1answer
654 views

Reducing repetitive Android code

How can I reduce the amount of repetitive code in my Android app? A lot of the code seems to be doing the same thing twice. I think that there is a more compact way to do this. What are some ways ...
4
votes
2answers
2k views

Battleship type game in Java

I made the code, and it works, but it uses many if conditions and it looks ugly. I would really appreciate it if someone could give me a hand by pointing me in the ...
8
votes
4answers
3k views

Poker game classes

I've built this two classes for a Poker game on Android and I would appreciate some feedback on my code. Have no mercy. The harsher you are, the better. Feel free to add your number of WTFs/minute. ...
1
vote
2answers
2k views

What's better: return null or handle the Exception?

I'm in doubts what approach to take here, please have a look on the code below: ...
3
votes
1answer
8k views

Clockwise and counterclockwise spiral matrix traversal

An interviewer asked me to write clean Java code for clockwise and counterclockwise spiral matrix traversal. e.g. {{1,2,3}, {7,8,9}} becomes ...
12
votes
4answers
953 views

Suggestions for improvement on Project Euler #7

Here is a solution for Project Euler Problem #7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? I've used ...
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 ...
19
votes
8answers
50k views

Java function to read a CSV file

I have a java function that reads a csv and returns its content as a Vector<Vector<String>>. The function seems to work as I need it, but my gut ...
4
votes
2answers
131 views

Review of java interface for constructing brain model

I have built a partial human brain model and the following is an example of how I use all the classes. I was wondering if anyone could critique my implementation strategies because I feel like the ...
2
votes
1answer
1k views

Find the smallest number whose sum of digits add to a number [closed]

Request for review, optimization, best practice recommendations etc. ...
5
votes
3answers
386 views

Should I design my enumeration in some way that indicates what the highest value is?

This is a subtle question about design in which I want to find the most elegant and appropriate solution. I have an enumeration representing a French card deck (see code below). With it I need to do ...
3
votes
1answer
4k views

Basic Poker Draw

I humbly ask that you review my simple (albeit a little over-done) program. The intention is to create a deck of cards, shuffle them, and draw the first five off the deck as a poker hand. Can you ...
3
votes
2answers
968 views

Kings Drinking Game

This Review Request evolves into this Review Request now with a custom UI and other changes. I wrote a program to simulate the drinking card game Kings. This is my third Java Project. I didn't quite ...
3
votes
2answers
1k views

Constructing a binary tree in java

I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. ...
14
votes
4answers
424 views

Finding subtuple in larger collection?

I've got a flat array of < 1000 items and I need to find the indices of a smaller tuple/array within the array. The tuple to find can be a varying length (generally between 2 and 5 items, order is ...
1
vote
1answer
758 views

Max depth of tree when all parent pointers are provided

Request for optimization, good practices, recommendations. ...
2
votes
1answer
1k views

Getting the list of people who RT'd a particular Twitter account (using Twitter4J)

I'm looking for feedback (both general Java, and Twitter specific) on how I'm getting the Twitter handles of everyone who retweeted me over the last 200 Tweets. Comments welcome. ...
2
votes
2answers
176 views

Determine if a word can be constructed from list of subsets [closed]

The question is explained well by examples in the comments below. Also I request verifying complexity: O( n * n!), where n is the number of words in the subsets. Review my code for optimizations, ...
7
votes
3answers
11k views

Determining common divisors of two numbers

Is there a better algorithm for finding the common divisors of two numbers? Can this code be shortened? ...
5
votes
1answer
204 views

Optimising Funny Marbles

Problem statement is at http://www.codechef.com/DEC13/problems/MARBLEGF Lira is given array A, which contains elements between 1000 and 2000. Three types of queries can be performed on this array:...
-3
votes
2answers
568 views

Compressing a string

The task is to compress a string. eg. "abcbcdabcbcd" Here as you can see some characters are repeated, so it can be compressed. "abcbcdabcbcd" -> "(a(bc2)d2)$" '$' denotes end of string. My code: ...
1
vote
2answers
576 views

A query on Hibernate database design

I am writing a simple web application where I have a User entity and set of pre-defined questions to be answered by the User. The answers provided by the users for these questions would need to be ...
0
votes
1answer
119 views
7
votes
2answers
2k views

Implementation of the Strategy pattern

I am learning the Strategy pattern but hate the idea of passing a new object to the Context each time I need to change the desired algorithm. Instead I think it would be best to create an enum that ...
12
votes
5answers
10k views

Optimize random number generator

I have a method that generates a 10 digit random number where each digit occurs only once. eg: 0123456789 3645720918 Is there any way this method could be ...
8
votes
1answer
274 views

Streaming pages of a different size than a cache's pages

I need to write a service to return objects in pages of size 20. The caching of these objects is done in pages of size 60. To abstract the logic of streaming differently-sized pages, I wrote this <...
2
votes
2answers
401 views

{Sum+=i++} to Reach N

I am developing code for a problem posted on StackOverflow. I have written my solution in Java but it is taking too much time to execute for numbers such as ...
2
votes
2answers
11k views

Change View visibility programmatically or through XML?

I have an XML layout used by 2 activities. Within this layout, there's a view that is visible for Activity1 and ...
3
votes
2answers
62 views

Improving some plugin user-input code

I made this nice bit of code that I can insert into any of my projects requiring user input. It seems quite long for what it does and I would love some insight as to how I can do this more efficiently ...
3
votes
1answer
355 views

Create GUI components with states

I am asking about the right way to make a component that holds some state. This includes a Jbutton that saves a color in it, or a list item that saves a certain object. So when those GUI components ...
7
votes
3answers
6k views

Marbles game with classes

Project contains 3 classes: ...
8
votes
2answers
1k views

Hello Java World ~> Parsing a Sudoku Grid

This is my first, very-very first attempt at java. I haven't started tackling the actual resolution of the sudoku puzzle (not even sure where to start, haven't looked at other weekend-challenge ...
5
votes
3answers
205 views

Improve this image file browser (remove redundancy)?

Let me start by saying that the code works as is but I think it has a lot of redundancy. It is written this way as I kept getting nullpointerexceptions if I try to put the fileCounter > gameDiff if/...
0
votes
1answer
241 views

Review my PuzzlesView Android component

I've began to read the book Clean Code by Robert Martin, I had a strong desire to learn how to write code that's easy for others to understand. Please criticize my code. PuzzlesView.java ...
4
votes
1answer
633 views

Listing groups by ID and name

I have this JavaFX code which is used for listing groups by ID and name: ...
2
votes
1answer
436 views

Changing bits in given big strings

I came up with this question. Let A and B be two N bit numbers. You are given initial values for A and B, and you should write a program which processes three kinds of queries: set_a idx x: ...
3
votes
1answer
113 views

Better way to create samples

I've done this piece of code for creating all possible samples without repetitions, but I think that it is a so heavy algorithm because every time I call this recursive function I create a new vector ...
1
vote
1answer
984 views

Correct logic in Monty Hall Problem? [closed]

I've written this code in an attempt to create the Monty Hall problem and check the ratios. I would like someone to check the logic on it. I know it isn't organized well at all; I just need to see if ...
8
votes
4answers
13k views

Finding all indices of largest value in an array

I have the following code that gives me the indices of an array's largest value: ...
3
votes
1answer
96 views

How to make this calender program simpler/optimized?

I've made a working calendar program, BUT I believe it's extremely inefficient. I think I have way too many else if statements: ...
7
votes
4answers
464 views

Looping Strategy: Change behaviour after first iteration

I have often encountered the following situation. I want to call a method in a Loop, but want to skip the call at the first run. It do not have to be a method, it also can be some lines of code that I ...
2
votes
2answers
153 views

Retrieving doubles from string - simple problem

The problem is not very complicated (I mean it is easy to solve it), but the question is how to make it in the easiest way (Java) Input String: ...
1
vote
2answers
65 views

Which way should I use while writing this method? [closed]

I've got a homework that is about methods. So first part is Write a method that prompts the customer for the car type. There are three acceptable car types (E: Economy, M: Midsize, F: Fullsize). ...
7
votes
3answers
10k views

Find elements occurring even number of times in an integer array

I came across an interview question, which is like this: You have an array of integers, such that each integer is present an odd number of time, except 3 of them. Find the three numbers. I tried ...
4
votes
3answers
1k views

Sorting strings in a one dimensional array

What are the better solutions possible? I have a character array with different strings. There is a max size for the strings. In case a string is smaller than max size remaining places are filled ...
3
votes
1answer
5k views

Very basic Context Free Grammar in Java

I wrote this program for an assignment, thus I had some requirements as far as the format of the cfg.txt file and some basic other classes that we had to use. Other than that, I am curious if there is ...

1
189 190
191
192 193
202