r/javahelp • • Jun 30 '21

Why do we need to catch specific exceptions instead of just catching any Exception?

17 Upvotes

Why do we need to do:

catch(FileNotFoundException ioe){};

And not:

catch(Exception e){};

My professor says it "does not promote robust implementation", but he doesn't explain what that means.


r/javahelp • • Jun 27 '21

What is the Java equivalent of Javascript’s (node’s) “npm install”?

17 Upvotes

When you git clone a node project, you usually run “npm install” to download all the project’s dependencies so you can run the project locally. What is the java equivalent for this? I git cloned a java project from github and apparently I need MySQL, maven, and JDK 11 to run the project. I made sure to have all that. I started the MySQL server. I then ran the project locally from visual studio code. I keep getting errors when trying to run the project locally and I’m worried it might be because I don’t have the dependencies installed correctly.


r/javahelp • • Apr 20 '21

Codeless Why do you see interfaces for classes with a single implementation?

17 Upvotes

There's an interface and there's a single implementation of it: CustomerService, CustomerServiceImpl.

It's a pattern i've seen repeated many times at various companies and i'm not really sure why. There's no sign of inheritance, there's nothing to abstract and it's not being used in a marker pattern.

So I thought i'd ask a wider audience.


r/javahelp • • Mar 22 '21

Looking to do a simple Java GUI program for my finals, any ideas? [data, api]

17 Upvotes

Sounds really scary to me as we haven't learned Swing yet, but will soon. The program must be GUI based. I made a Flappy bird clone last semester, and I cannot lie- I would no way be able to do it on my own without watching a step by step YouTube tutorial.

This semester, I will either work with data, an API, or both. I was looking for advice, links, anything I can do to prepare for this project.

Two project ideas that were from classmates that sounded cool:

  1. Working with an API to make a Tweet everytime the Warriors win a basketball game.

  2. Making a airline management system with user data

I could easily Google ideas but the reason I ask here is due to the fact that I am not sure what ideas are too high for a second semester new programmer who never worked with an API or much Swing (Not to mention vague ideas on Google).

Thanks!


r/javahelp • • Mar 19 '21

Do you need a neural network to use a genetic algorithm?

17 Upvotes

Pretty much just curious. I’m looking to build an ai for Tetris and would love to use a genetic algorithm for it but was curious if I need to look more into building a neural network first.


r/javahelp • • Feb 09 '21

Workaround How to create database entities in a micro services based project?

17 Upvotes

I'm a begineer with Microservices implementation using java technologies. I want to understand in which microservice to place database entities as there are many other services that will be using the same database for reading/writing data to same entity tables.

More precisely, I've a backend microservice that handles handles frontend requests and contains some business logic. And therefore as I've just begun with project, I've added all database entity classes to this backend project.

But there are couple of other Microservices that read & write to same database tables. So, should I copy the entities to this microservice as well? I mean, there are lot of entities. And if I don't use the entities approach then it would mean, writing lot of queries without utilizing the JPA and losing all the benefits of JPA. What do you guys suggest?


r/javahelp • • Feb 09 '21

Can anyone explain streams in java and it's associated classes ?

17 Upvotes

I was learning the IO stream objects and IO classes. And I cldnt understand the basic concept. Especially the bufferstream.


r/javahelp • • Jan 15 '21

Preferences on Java environments

17 Upvotes

Hey there, I'm a fairly new CS student, I've taken one quarter of Python courses and this quarter I have begun studying Java. My professor is teaching using BlueJ but recommended any students that are looking to pursue CS as a major to look into other platforms because as programs become more complex apparently BlueJ just starts falling apart. He suggested a few other ones, out of which I have begun using IntelliJ. I was wondering if any of you guys have any input from the professional realm if IntelliJ is a platform that I should be putting significant effort into learning or if there is a better environment that I would be better off learning before I get to deep into IntelliJ, because if I'm learning it, I'd feel much more comfortable knowing that what I'm learning will continue to be useful as I move into the professional sector.


r/javahelp • • Dec 24 '20

I need grep for Java abstract syntax tree

17 Upvotes

I need to do refactoring legacy web system written by Java. This system is made of 10 million lines of Java code which is not well structured. For example,

if (requestType == 1) {
    doSometing1
} else if (requestType == 2) {
    doSometing2
} else {
    doSometing3
}

if (requestType == 1) {
    doSometing4
} else if (requestType == 2) {
    doSometing5
} else {
    doSometing6
}

Typically, there are 100 of boilerplate codes like this in a method. I want to know which procedures are called when request type is 1 simpler. grep is not enough powerful in this use. I need grep for abstract syntax tree like this.

gasp "if (requestType == 1) {/**/}" code.java
  doSomething1
  doSomething4

gasp stands for global search for abstract syntax tree and print. This command takes Java code as the first argument, and do pattern match. /**/ in your first argument matches code block.

I think if such command is exists, it will be really useful for many many use. I searched if there exists such tools, however, I could not find. So I am starting to create this command. Do you have any comments or requests? If you have ideas of alternative solutions, please let me know.


r/javahelp • • Dec 23 '20

Multithreading in Spring Framework

17 Upvotes

Hi all,

I am learning Spring Framework myself, I want to test and test for multithreading in processing. Are there any tools or techniques to help me with multithreading testing? I want to make 100, 1000 or any number of fake requests so is there any tools to help me do that? Sorry my English is bad. Thanks very much


r/javahelp • • Dec 12 '20

Question regarding best practices when unit testing void methods

17 Upvotes

Hey gang,

So I have been brushing up on my TDD practices with JUnit5. I have noticed that when testing void methods, namely ones that insert data into a structure, I am testing the success of the insertion with toString() or search() methods.

Should I be coupling my unit tests like this?

What would be an alternative to better expose the functionality within the void method?

Or is this type of testing, much like with private methods, simply how it is done?

Thanks in advance for your insights!


r/javahelp • • Dec 08 '20

Popular Java frameworks worth learning ?

17 Upvotes

For people that have been using Java in a work environment or that are looking for employment , What java libraries should I pick up as a recent graduate ?


r/javahelp • • Nov 09 '20

Solved Extracting a query string from a url where the '?' is missing.

17 Upvotes

I have created a Java HTTPservlet in which I use reqeust.getQueryString to read the ? (=QueryString) like https://localhost:8080/homepage/?1234567 from a url of an existing application. The problem is the URL being sent from the application is without the ?, so I cannot distinguish between the Path and QueryString, like this:

https://.../somePage/1234567

Is there a way to extract the number from the url without using Regex? I do not have access to the code of the application that creates the url, so I can't get it to send it properly as a queryString. I hesitate to use Regex because there might be more parameters that need to be read and maintaining the regex will be a pain in the ass. Is there another workaround besides Regex?


r/javahelp • • Nov 07 '20

Codeless What are orientations of a (binary search) tree? Which is right and which is left?

17 Upvotes

I checked in multiple textbooks, and all of them refer to the right/left of the BST differently. One textbook would refer to the left side as the reader’s left while others refer to the left side of the tree as the reader’s right. Which of these two is more consistent with the majority of people?


r/javahelp • • Nov 02 '20

What is the best format to store and load large number of audio files?

17 Upvotes

I'm working on what is essentially a custom audio player and the amount of sound files I'm using has been growing quite a bit. Right now I am just storing them as .Wav and loading them on demand when needed. Is there a better way to do this?


r/javahelp • • Oct 26 '20

Solved Trying to implement a MinHeap.*help needed*

17 Upvotes

Hello, so I'm trying to create a Min-heap with the bottom up approach that takes O(n) time and have spent hours debugging it (~6-7 hrs) but still can't figure out what's wrong with it. The heapify() isn't working as it should, if anyone were to point out the problem it'd be a tremendous help. I'm assuming the problem is in the SiftDown() func, what can't exactly figure out what it could be. I'm linking the code below.

Min-Heap Source Code

Apart from the heapify method, any other corrections/optimizations are also welcome.

private void heapify() {
        int i = size / 2;
        while (i >= 0) {
            siftDown(i--);
        }
    }
private void siftDown(int i) {
        while (minChild(i) != -1) {
            if (heap[i] > heap[minChild(i)]) {
                swap(i, minChild(i));
            }
            i = minChild(i);
        }
    }
private int minChild(int x) {
        if (left(x) >= size) {
            return -1;
        }
        if (right(x) >= size) {
            return left(x);
        }
        return heap[left(x)] < heap[right(x)] ? left(x) : right(x);
    }
private void swap(int i, int j) {
        int t = heap[i];
        heap[i] = heap[j];
        heap[j] = t;
    }

I realised I haven't provided an example test-case where this is going wrong so here you go:

INPUT:

[5, 4, 3, 2, 1]

OUTPUT:

[1, 5, 3, 2, 4]

As you can see output is not a heap.

UPDATED

private void siftDown(int i) {
        while (minChild(i) != -1) {
            int child = minChild(i);
            if (heap[i] > heap[child]) swap(i, child);
            else break; // edit3*
            i = child;
        }
    }

Edit1: Already posted this on r/learnprogramming but no one replied.

Edit2: So I put the required amount of code I need the help with, if that helps. Please understand that it is also frustrating for me to debug this, if you can't figure out the bug please consider upvoting this so that someone else can and don't ghost this post. I really want to move forward with heaps now to fib heaps. Thank you.

Edit3: (final) So as u/heckler82 suggested, the minChild returned different indices after the swap so I stored the original result in a temp variable and updated i with the temp variable's value instead of calling minChild again. Thanks Everyone for helping, anyways I'm posting the updated siftDown function here incase this helps anyone in the future.

Edit3*: Optimization by u/sweetno Break off the loop if heap property is satisfied.


r/javahelp • • Oct 14 '20

Is it better to do fragments or new activities?

17 Upvotes

Hello,

I'm developing an app (my first app in Android Studio) and wanted some advice.

Currently, I have a main layout with a mainactivity.java, which is my sign in/new account screen.

Once they sign in, it starts a new activity, homescreenactivity.java with layout homescreen.

From there, the user can select different buttons at the bottom (my own navbar) to take them to different parts of the app. These buttons all start new activities with layouts of their own.

Is there anything wrong with this approach?

I saw bottomnavigationview widget, which seems to use only 1 activity and creates fragments to swap between.

Is it better to have 1 activity with bottomnavigationview widget to move to the different parts of my app?

Thank you!


r/javahelp • • Sep 03 '20

Homework Super basic question - how do I test run my method?

17 Upvotes

Hi! First off, apologies in advance if this is written weirdly because I'm mostly familiar with python and the basic structure of java has really not clicked for me yet.

I'm in a java uni class at the moment (which is my first experience with java) and I think my prof is teaching us a bit backward. I'm trying to troubleshoot a method that I wrote (that will be uploaded and run through an autograder), but we have fully just not learned how to actually write something that will run the method, so I have pretty much no way of troubleshooting it on my own. Can someone help me with this?

For example, my file starts off with

public class Encoder {

public String getCode(String original) {

and then has all the functional bits after it. It takes a String and needs to output a String. But unlike python where I can just whip up a line of code that says to run this function with whatever test parameter I want, I think I need to write like a whole other EncoderDriver class, but I have no idea how to even start it. How do I go about writing code that will run my method? (Or, what search terms should I google? Because I truly have no idea what the "vocabulary" of java is.)


r/javahelp • • Aug 11 '20

How to upload image to MySQL database and display it using spring boot and Thymeleaf?

17 Upvotes

My question has nothing much to explain i just need some guidance on how to insert image into MySql database and show it to webpage when needed using spring boot and thymeleaf.

How I have design our model class like bellow i am posting model class of an product object,assume i have stored image of project in Mysql data base,i want to fetch it using Spring data Jpa and show to user using Thymeleaf Template Engine...

What changes i need to do in below model class and other related things,I searched on google on but unable to understand.

Thanks in advance

Your words are value for me....

  u/Entity
class Product{

u/Id
u/GeneratedValue
int pID;
String ProductName;
int ProductCost;
int QuentityAvailable;
   
//NoArgConstructor
//Constructor
//Getter
//Setter
}

r/javahelp • • Aug 05 '20

JavaFx versus JS+?

17 Upvotes

Hey All!

Not sure if I'm understanding this correctly. I'm a mid-level Dev using JS (node react etc) and I'm trying to really dig deep into Java. Only thing is, I've skipped over learning JavaFx and now I'm not so sure if that was a great idea? I read that not many companies use it, and I honestly thought if people wanted a GUI or some sort of front-end, wouldn't they just use JS With React or something?

Sorry for the confusion, I'm really hoping to clear it up and know if it's something that I should learn or I'll be fine to move forward without it! I was hoping to transition to Fin Tech sometime this year!

Any advise would be awesome!


r/javahelp • • Jul 06 '20

Homework Not sure how to restrict user input to the correct data type

17 Upvotes

Hi so I’m pretty new to coding I’m working through a 101 level java coding class but I’ve come to an assignment where I need to write a few input methods that get input from the user of certain data types and reject input that is not the correct data type. I found some code online that was supposed to work for strings and it sort of works but it only accepts 1 character not whole words or phrases, here’s what I have so far

public static String getString() { String userString; System.out.println(“Please enter a few words”); while(!keyboard.hasNext(“[A-Za-z+]”)){ System.out.println(“You did not enter any letters”) keyboard.nextLine(); } userString = keyboard.nextLine(); System.out.println(“You entered: “ + userString); return userString; }

So this code works for any non-alphabetical data such as numbers and symbols and rejects them and it works for single letters and accepts them but the problem I am having is that when I enter multiple letters at once such as “aaa” the code rejects the input and displays the error message I wrote. I found this code online and it said it would take any length string but for some reason it is not working for me. Also when I have to write the integer version of this method could I pretty much reuse the same code but with “[0-9+]” in my while statement instead of the “[A-Za-z+]”?

Edit: wow a spent a bunch of time formatting that code properly on my phone and reddit still shoved it all together wow.


r/javahelp • • Jul 03 '20

Homework Have some questions about basic Hello World app

17 Upvotes
package javaapplication1;

 
public class JavaApplication1 {

    public static void main(String[] args) {
      System.out.println("Hello world!");
    }
}

The code as it is runs fine but if I remove the "package" line I get this error "Could not find or load main class javaapplication1.JavaApplication1".

Similarly if I change the name of the public class I get the same error. Does that mean every Java program has to start with this "package" line and why exactly is it needed? I'm guessing it just points to the file on the computer that it has to find and run. And the other thing I was wondering is why can't the public class be any random name why does it have to be the name of the file?


r/javahelp • • Jun 16 '20

Solved How do I print the contents of an array object?

17 Upvotes

I'm writing a program which utilizes OOP. The program I am creating is supposed to recruit applicants to a team. In my Team.java, I created a method which is supposed to accept members and add it to the team.

In my main.java, everything seems to be working fine. The addMember method works, however when my program is asked to print all the members on the team, only the latest added member is printed.

I tried using a for-loop to add elements to the array but it doesn't do as I expect it to do. It instead prints some kind of cryptic code, something like "Library.@483403".

Can anyone assist me into find a solution? Thank you!


r/javahelp • • Jun 09 '20

Where i can find open source spring-boot project?

17 Upvotes

any help , how i can search or where i should search.

i tried to google but find not active project.