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
0
votes
3answers
47 views

Listing last 3 items (or all if less than 3) in an ArrayList

Writing a code to return the last 3 items in a list, if the list has 3 or more elements, otherwise return all list items. But the code looks really bad. What is a better way to write the same code? <...
-4
votes
0answers
28 views

new to python, longest collatz sequence [on hold]

i am new to python and struggling with an assignment. "Consider the following operation applied to positive integer values: if the number is even, then halve it, but if the number is odd, then ...
-2
votes
0answers
17 views

ReentrantLock.signal() not working as expected but ReentrantLock.signalAll() works. Why? [on hold]

In my simple code below, if I replace condition1.signal()/condition2.signal() with ...
1
vote
1answer
29 views

Unit tests code for a Java class that that checks whether an array is sorted

I'm trying to write unit tests code for a java class. The class tests an array for sorted (either way) or not. Here is the Unit Tests code: ...
0
votes
3answers
51 views

Basic Palindrome check comparing chars

I've seen variations of this assignment where you reverse a string to match it to the original to find out if it is a palindrome, but this version I made only iterates through half of the string ...
-4
votes
0answers
26 views

Need help converting my program to M-V-C [on hold]

I'm currently doing a course in Object Oriented programming in Java, and we need to create a game using JavaFX and the Model-View-Controller approach for our last lab so my lab partner and me chose to ...
7
votes
3answers
433 views

Checking an array is sorted in either order efficiently

Here is my code, it works fine, just wondering if there any anything to make it even better. ...
-1
votes
0answers
14 views

Wrapper for object to avoid ambigous boolean in method [on hold]

I recently thought if wrapping an object in a method that returns that object only when a method inside it is called is a good idea. I have a User object with fields like username, email, avatar etc. ...
-3
votes
0answers
15 views

java.lang.NoClassDefFoundError JEE with Maven [on hold]

I have create two project as maven project in eclipse(Jee Version: 2019-09 R (4.13.0)): frontoffice and core . I want to add a dependency between the two project so i did like follow : core : pom.xml ...
8
votes
4answers
1k views

Replacing each letter with the letter that is in the corresponding position from the end of the English alphabet

I wrote a small program for hyperskill/jetbrains academy to encrypt the message "we found a treasure!" and print only the ciphertext (in lower case) by replacing each letter with the letter that is in ...
1
vote
1answer
44 views

Add a value to each of the array element between two given indices successively

I am trying to solve the HackerRank: Array Manipulation problem using Java. The problem that I am facing is that the program is running for half of the test cases, while for the other half it is ...
2
votes
2answers
112 views

Brute-force ID reversal search

EDIT The random number generator in this code is a blackbox for a similar algorithm that isn't publishable for privacy concern. The algorithm works in similar way, in that it takes in a seed value ...
-2
votes
0answers
23 views

Notepad java-swing ( undo redo) [on hold]

anyone helps me please . I create a notepad program , and anything ok except the Undomanager . I can undo and redo in the current file . But when i use new file , the undo and redo still work , it can ...
4
votes
1answer
89 views

Random password generator that ensures diversity of character types and absence of confusing characters

I wrote the following password generator, to generate a password in which each character group appears at least at once. ...
2
votes
1answer
62 views

Verifying Mathematical Expressions

This is a part of my calculator program. I have to check if the input entered by the user is a valid mathematical expression. The first check is performed by another program, where the program checks ...
-2
votes
0answers
18 views

Collatz Conjecture meet of two numbers in Java, getting wrong answer [on hold]

I literally with multiple test cases, it is passing, I even fixed TLE by using Long. Could anyone help in figuring out what I am doing wrong? Below is my code : ...
4
votes
2answers
62 views

JavaRPN (Updated)

I have previously worked on this program in this question, and have gotten some very good answers and help! Now, I have done some more work on the program, and would love if you could give me honest ...
3
votes
3answers
86 views

How to write this method with similar checks and multiple returns more elegantly? [closed]

I have written the following method which works as expected. But is there a way to write this more elegantly in Java 8? Appreciate any feedback. Thanks. ...
7
votes
2answers
80 views

Java command-line API client to fetch Bitcoin exchange rates

I am building a simple Java command-line application. And I want to know the best practice and make my code clean. Can anybody suggest to me how many different classes I can make and what more method ...
11
votes
2answers
312 views

Self-inflicted killing utility

If you need to kill the same process that you're in (for testing purposes or whatever) this code will do it. A definitive, quick, unmerciful dead of the current Java program/app. Not a System.exit(0) ...
3
votes
1answer
81 views

Knuth-Morris-Pratt algorithm in Java Finding substrings in a matrix

Problem Given two matrices: a pattern p and a text t. Write a program that counts the number of occurrences of ...
0
votes
3answers
90 views

Minecraft city class [closed]

This is a class representing a city in a mod I am creating for Minecraft. All the constructor parameters are mandatory and they are used to help the class that calculates and builds the Paths, ...
-3
votes
0answers
40 views

Java ArrayList Sorting 2D ArrayList [closed]

I was trying to sort ArrayList including both Element but did not work great. ...
2
votes
1answer
54 views

A simple priority queue in Java via linked list sorted by priority keys

Now I have this very simple priority queue. add and changePriority both run in \$\mathcal{O}(n)\$ and ...
3
votes
4answers
181 views

Java creating augmented array of size 400,000,000

I was solving one competitive coding question having integer bounds 2^100. Fortunately, it was a dp question and I didn't need an array of that size. Practically we ...
-4
votes
0answers
78 views

binomial distribution [closed]

The code below was amended to handle more cases 0 probability of success for 0 number of successes 0 probability of success for n number of successes p probability of success for 0 number of ...
-3
votes
0answers
20 views

Standard deviation calculation - am I following the right approach. How do we find SD percentage? [on hold]

I have a query which gives me below data. item_name, total_purchase_count_per_week, previous_day_purchase_count. For ex, ...
2
votes
1answer
69 views

Populate an ArrayList of different instances of a class

First of all, here is an overview of my application. I want to create a multiple-choice app, on android, using data stored on a remote MySQL database. The DB has 100 questions that will appear in ...
-1
votes
1answer
34 views

update field when others fields are motifed [closed]

I have a class Installment and a method executeTransaction. The field totalBalance ...
1
vote
0answers
28 views

Pipe ResponseSpec's Body

I'm writing a utility method which pipes given ResponseSpec's body and gives to the caller a change to read it. ...
4
votes
1answer
55 views

Android Java Splash Screen code

Here's my android splash screen. I am trying to figure out a better way to write it. It just moves the logo up to the center of the screen. It's my third time creating a splash screen for an android ...
2
votes
0answers
23 views

Library/API: simple springboot / jdbc query builder and ORM

I have been using and developing a simple ORM and query builder library that I have found useful in it's practical application. I am pretty sure there are improvements to be made, and I welcome any ...
1
vote
2answers
47 views

Files.walkTree is causing Assignment Branch Condition too high [closed]

My code is working correctly, but is resulting in a "Assignment branch condition too high" warning with a score of 12.08 on CodeBeat. Codebeat is an automated code review utility that helps ...
6
votes
1answer
68 views

Remove elements compared with other elements in a Set based on a condition

Simplified the actual problem to focus on comparing/removing performance. Given a set S with n elements , find the most optimal way to compare each element to all others until a condition is met that ...
3
votes
1answer
42 views

Code feedback on running app

I have made an Android app called RunTracker - https://github.com/michaelnares/runtracker/tree/master/app. The intended functionality is that the app saves runs to the local filesystem, shows your ...
-3
votes
1answer
46 views

Please point out any improvement in it [closed]

Its a conference service code from controller to DAO for conference application. it contains controller, conference service, dao and entity class, I am seeking for any improvement or code review ...
6
votes
4answers
188 views

Building a toy from Lego toys

I recently attempted a coding problem, but my solution was not well received during the interview (modified slightly to reduce Google-ability). Suppose a child has several Lego blocks, and wants to ...
1
vote
0answers
29 views

Spring Boot - JPA query execution Time

Any one suggest me right way to write mapping more then one table using spring-boot (jpa), i was done it, but its required more time to extract result, Following are the Mapping of Pojo: First:- <...
2
votes
1answer
36 views

Redis work queue processor

I frequently encounter the need for creating services that concurrently process messages from Redis queues. So I decided to share the knowledge I learned and help others easily bootstrap such a ...
4
votes
3answers
59 views

Finding indices of CSV headers in Java

I'm working on a project where I needed to extract the indices for the headers of a CSV file to initialise objects. I thought that I should work on my documentation process and learn how to document ...
2
votes
1answer
60 views

Storing data in a map that can be retrieved by N keys

I often have data stored in a Map that I need to retrieve by something other than the Key, so wanted to write a generic class that lets you define up to N keys (with the possibility of each key having ...
-3
votes
0answers
44 views

A problem: HackerEarth says it is TIme limit exceeded

I have written a solution to Memorise me problem on Hacker Earth but it takes a lot of memory space and time limit exceeds in the last test case. The first line of input will contain N, an integer, ...
0
votes
1answer
61 views

using a cast in an overriden equals method should I use generics and if so, how? [closed]

here is my issue when trying to override my equals method. this is what I have currently ...
2
votes
3answers
84 views

update object in list

Q: I have a Bank class containing multiple loan accounts (LoanAccount class). I've create a ...
4
votes
1answer
92 views

The best way to unwrap the optional in java [closed]

Instead of using the null checks I tried using Optional and safely unwrapping the optional. Approach 1 My first approach of unwrapping the ...
2
votes
1answer
63 views

Creating an empty Sudoku grid

I am generating a 9x9 grid on which I will play sudoku later by setting click listeners to each square on the grid. I first have a .xml file that represents a single square in the grid. This consists ...
1
vote
0answers
39 views

Recursively look up directories, (a little bit) slow

I wrote some Java code to look up all files in a chosen directory (like it's showing in the Windows Explorer), and to display the results, but it seems to be slow. Can you tell me, how to speed up it? ...
0
votes
0answers
59 views

Unisex Bathroom problem with semaphore

An office has a bathroom that can be used by both men and women, but not both at the same time. If a man is in the bathroom, other men may enter, but any women wishing to use the bathroom should wait ...
2
votes
3answers
100 views

Classifying integers as “weird” or “not weird”

Problem Statement Given an integer N, perform the following conditional actions: If N is odd, print Weird If N is even and in the inclusive range of 2 to 5, print Not Weird If N is even ...
3
votes
1answer
44 views

Hibernate - entity design with multiple dependencies

I am currently working on an API and came across the following design. ...