r/javahelp • • Jun 09 '20

Managing SDKs on dev machine (how to do it, and is it even necessary?)

17 Upvotes

Hi all,

Playing around with Java for the first time in years. I see that there are now lots of different SDKs, and having gotten used to using NVM and Anaconda to manage node and python versions respectively, I reached for the equivalent.

I came across SDKMAN. Maybe there are more.

Do you use a tool to manage this? If not, how do you determine which SDK to use a dev-time?

(PS I hope this doesn't fall foul of the NO INSTALLING JAVA rule, I'm not asking how to install Java, but about dev strategies for managing multiple versions)

Thanks!


r/javahelp • • May 29 '20

Solved Can Someone explain why x = x++; doesn’t change the value of x?

17 Upvotes

This is probably a common ask but it’s been bugging me. I was told that x++ would use its original value in the assignment, then increment to x+1 afterwards.

Let’s say

y = 2 + x++;//x was 3.

y now equals 5 and x equals 4 right?

Why doesn’t x = 2 + x++; work the same? Is it because Java knows that trying to define the same variable as 2 different things on the same line is confusing?

Edit: I understand it now, thanks so much for your help!!


r/javahelp • • May 23 '20

Recommendations on String manipulation

17 Upvotes

A company I work with is replacing an old COBOL process with Java.

Pretty much the business rules are about reading some long lines from a text file, do some snippin and reordering before writing to a new file text.

That part is covered, but I'm doing some heavy use of String.substring. I haven't seen a real input file, but they are supposed to hold near 2 million lines each.

Because of that, I would like to make the new process as memory efficient as possible. (When I first learned Java they always warned us about the perils of String)

The process will run on a server (don't know the exact specs) if that's useful.

Thanks in advance.


r/javahelp • • May 07 '20

How come Strings are not initialized like a normal object?

17 Upvotes

My understanding is the way you initialize any object would be like this:

ClassName objectName = new ClassName();

Yet String is also an object but you don't need the

new

keyword or the call to the constructor to initialize it you just write

String b = "Hello World";

Why is this? And is it possible to recreate such syntax with other classes?


r/javahelp • • May 01 '20

Solved How do I get multiple words of input from a user to store in one variable?

17 Upvotes

So say I need to store their full name, how would i do this?

My code:

System.out.println("What is your full name?");
String name = scnr.nextLine();

but this ends up being faulty as it doesn't let the user input anything before it jumps to the next

System.out.println();

and i cant use next(); because it only gets one word

So if the code is:

System.out.println("What is your full name?");
String name = scnr.nextLine();
System.out.println("What year were you born?");
String year = scnr.nextInt();

The output ends up being:

What is your full name?
What year were you born?
(user input)

Any help is greatly appreciated


r/javahelp • • Apr 30 '20

ArrayList vs LinkedList

17 Upvotes

So I understand that for different situations, either the ArrayList API or the LinkedList API will be more efficient. So what should I look for in terms of design that will tell me which one of those structures is recommended? Is it more related to preference rather than specific uses?

Thanks!


r/javahelp • • Apr 11 '20

Which Java library of graph theory data structures and algorithms would you recommend?

17 Upvotes

I have to perform operations on large graphs (PageRank, DFS,/BFS traversal , etc).
The structural properties are:

  • directed edges: an edge has a source and a target

  • self-loops: whether to allow edges which connect a vertex to itself

  • multiple edges: whether to allow more than one edge between the same vertex pair

  • Both Nodes (Vertex) and Relationsships (Edges) must be able to contain properties.

I don't want to use a graph database such as Neo4J .

I've found JGraphT

Is this a good Java graph library?

Would you recommend others?


r/javahelp • • Apr 10 '20

When to @Override with toString( )?

17 Upvotes

I'm going through MOOC.fi Java 1 part 4. They showed us how to override the toString() method by using

    public String toString() {
        return "something";
    }

Then in the next section the example uses:

    @Override
    public String toString() {
        return "something";
    }

They never explain why @override is used. The test passes the code without it so I'm not able to figure out its function.


r/javahelp • • Mar 31 '20

Solved Can java output text as user?

17 Upvotes

So I'm trying to get java to output certain text on any open window based one the event of certain strings being entered and hitting tab.

It's essentially an autocorrect program that fills in templates.

For example, if I type "wbu" in any text field in any window and follow it with hitting tab, it should replace the previous string "wbu" with "what about you?".

I managed to figure out the logic of reading the keyboard using the native hook library, but I'm completely stuck when it comes to getting any output from my program.

What can I use to have Java output into any text field?


r/javahelp • • Feb 25 '20

How do I modify a character array in place?

17 Upvotes

I am trying to replace the characters in an array with their frequency.

For example, ['a', 'a', 'b'] should become ['a', '2', 'b', '1'].

I'm finding this quite difficult to do, since Java arrays cannot shrink or expand at runtime and this needs to be done in-place, without extra space.


r/javahelp • • Feb 07 '20

Need Help with Abstract Classes!

17 Upvotes
abstract class Employee {

    private int id;

    private String firstName;
    private String lastName;

    public Employee(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    abstract double calculatePay();
    
   
    
    }
  1. Employee class – This class has the following characteristics a. Must be abstract b. Three data fields i. id: int (greater than 0)

ii. firstName: String

iii. lastName: String

c. Only one constructor, namely a 3-arg constructor setting the above

d. Getters and Setters for the above

e. An abstract calculatePay method with no parameters that returns a double

f. Overrides the toString() method of Object making it abstract

Im not able to understand (F) part or last part. Any hints will be greatly appreciated.

Thank You


r/javahelp • • Jan 28 '20

Help with ArrayList and while loop

17 Upvotes

I'm trying to create a simple text-based RPG, and when they are not in danger, the player can inspect any items in their inventory, which I am using ArrayLists to keep track of. However, whenever I run this loop, it gives me a java.lang.IndexOutOfBoundsException. Can someone help?

if (InspectCheck == 1) {
    System.out.println("What item would you like to inspect?");
    int ItemRemaining = 0;
    int ItemToShow = ItemRemaining + 1;
        while (ItemRemaining <= Inventory.size()) {
		System.out.println("Select (" + ItemToShow + ") to inspect [" + 
                     + InventoryStr.get(ItemRemaining));
		ItemRemaining++;
		ItemToShow = ItemRemaining + 1;
	}
    ItemInspect()
}

r/javahelp • • Jan 20 '20

Encrypting messages between clients

17 Upvotes

So we have an assignment in which we have to create 5 clients that can be messaging each other, through the same port and using an interface, the only thing we can't get right, is the encryption part of the assignment. So, the messages need to be encrypted and decrypted after and before sending and recieving them. How can we do this? Any idea?


r/javahelp • • Dec 29 '19

Understanding Maven

17 Upvotes

So i'm recently getting into maven for the first time in my life. I'm a bit confused and i think it's because i don't really know what maven does. I have this POM.XML file in my project. Do i just add a dependency there and maven will magically find the JAR and download it for me, or do i need to somehow also manually download the JAR and the POM just points at it?

Help would be greatly appreciated!


r/javahelp • • Dec 22 '19

Maven and Gradle

17 Upvotes

I've been coding java now for about a good year, where i've always used maven. A few days ago I tried to use gradle, only for dependency management in a very small project.

When looking a different githubs I sometimes see projects that use both maven and gradle, for me they are both just similar build tools. One in XML and the other in groovy(?). What is the value of using both together?


r/javahelp • • Dec 18 '19

Sorting algorithms - Big O

17 Upvotes

Hello,

For my study the Big O and sorting algorithms are important. Unfortunately, these two aren't my strong points. Therefor I am wondering if anyone can suggest me of books/videos/courses that may help me understand and recognize the Big O and be able to find the most efficient sorting algorithm for it's purpose. Explaining it would obviously be very appreciated.

Thanks for reading!

Ps. I am new to Reddit


r/javahelp • • Dec 05 '19

Best way to map xml to xml?

17 Upvotes

Beginner Java dev here that started working on a Spring application that receives an XML request. This XML request has to be sent to another endpoint, but in a slightly different format, so it requires me to remap some fields. What's the best way to do this?

I tried using Mapstruct, but it seems to be catered to mapping between POJO's and DTO's... not so much XML. Or am I missing some functionality? The documentation does not mention XML to XML mapping.


r/javahelp • • Nov 29 '19

What's the best way to store information with my java program ?

17 Upvotes

Hey, I'm currently working on a simple java project as part of a school assignment, it consists of creating a console app where the user (Teachers, clubs, etc) can manage reservations of school facilities, and as you can imagine my program should keep track of the reservations, the facilities and the users of different types... My first thought was to use arraylists, then i thought about using a real database.

What's the best way to implement the storage of information? How to work with a database but with no requirements related to the user's pc (Installing any sort of database etc)?

I essentially want to convert my program to a standalone executable file in the end. Your help will be much appreciated.


r/javahelp • • Nov 08 '19

Can someone help me understand my (correct?) code, finding the sum of the highest two of three numbers?

17 Upvotes

So I have an assignment where I have to make a program that takes in three integers, and I have to call a method, pass those three integers, and inside that method, find the sum of the greatest two numbers and print them in the main program. So after some hard thinking thinking I came up with something, which I thought was a bit too convoluted. After reviewing my code, I can't seem to see how it makes sense. I tested random integers and it still works. The part that confuses me are the nested "if" statements. Also, could I have placed the return statement inside each "if" statement so the program doesn't have to go through checking all statements when it already has the answer?

public static int findsum (int x, int y, int z) {

         int big_sum = 0, sum1, sum2, sum3;
          sum1= x+y;
          sum2=x+z;
          sum3=y+z;    

            if (sum1 > sum2) {
                   if (sum 1 > sum3)
                         big_sum=sum1;
                          }
            if (sum2 > sum3) {
                   if (sum2 > sum1)
                         big_sum=sum2;
                        }
            if (sum3 > sum1) {
                   if (sum3 > sum2)
                          big_sum=sum3;
                       } 
                 return big_sum;

}


r/javahelp • • Oct 30 '19

Codeless How to get into JavaME ?

17 Upvotes

Hello! I recently picked up what has to be my most favorite phone of all time, a sony ericsson w705. It runs what i understand to be some form of java. I really want to make a simple, single station web radio receiver so that i can listen to music on it over wifi. This isnt the only thing, I actually want to learn code, but this is what i want to do right now. Does anybody have any books that could help point me in that direction? Or help with a specific code?


r/javahelp • • Oct 24 '19

How long does it take to be a skilled Java developer?

17 Upvotes

I'm 33 and just started learning. Not exactly old, but no spring chicken either. Started completely from scratch with only a tiny amount of Visual Basic experience back in the day. The plan is to be good enough to make a career out of it within 2 years. Is it feasible to be a skilled developer in that time frame?


r/javahelp • • Sep 12 '19

What is the difference between "[ClassName].this.variable" and simply, "this.variable"

17 Upvotes

So I came across this class called:

AutoCompleteComboBoxListener()

with a node called comboBox.

And inside its methods, such as

selectClosestResultBasedOnTextFieldValue(),

they refer to the comboBox of AutoCompleteComboBoxListener() as

AutoCompleteComboBoxListener.this.comboBox.getItems();

Is it different from:

this.comboBox.getItems();

Thanks a lot everyone.


r/javahelp • • Aug 23 '19

Distributing java app that utilizes JDBC

17 Upvotes

Hi, all

So I've created a application for my fantasy football league that utilizes a database that I've created on my local machine. In my java source code I query the database using JDBC. I know that a jar file is used to distribute java apps to other people, but I am confused as to how to distribute the database portion of the app.


r/javahelp • • Aug 06 '19

Why do arrays in Java need to be the same type?

17 Upvotes

I heard that one of the reasons was to allow for constant time access, but I don't get how that's possible if you have a polymorphic array. In other words, if I had something like Doctor[] = new Doctor[4]; and I stored inside the array subtypes like Surgeon, Cardiologist, etc...how could I ensure constant time access if the subtypes had varying numbers of fields? (Ex...surgeon might add another 30 fields of it's own, and another 20 methods of its own).