r/javahelp • • Mar 16 '21

I wrote a simple card deck with a shuffle capability, can anyone review it? I’m newer to java and don’t want to build bad habits.

16 Upvotes

Github link in comments


r/javahelp • • Feb 20 '21

How to remember user's detail to avoid asking him to sign in everytime in JavaFx Desktop Application

16 Upvotes

I am trying to build an application that requires users to log in. When they register I store their information in the MySQL database. (For now, I am just using localhost) I don't want users to write their login details every time they open up the application. How can I avoid this? I am totally clueless. I will be very thankful if someone helps.

Reference to my same question in stack-overflow


r/javahelp • • Dec 11 '20

Questions About Making Games in Java

16 Upvotes

Hi everyone,

I would like to learn how to make games with Java. However, I don't know where to start. I have some prior experience with JavaFX and Java Swing so I think that is good. My main question is: What library do I use, and where can I find good information/tutorials/docs about that library?

Thanks


r/javahelp • • Dec 02 '20

Hibernate: Does it eventually use SQL somewhere that I can see?

16 Upvotes

I'm debugging a massive application that uses Hibernate, which I am not familiar with.

I'm trying to find out why the application ( many confusing layers above Hibernate ) is sending out error messages about there being no data, when I can see the data in the database ( Postgres ).

Does Hibernate eventually somewhere deep down make SQL queries that I can see? Some other commands I can see such that I can learn what it is asking for so I can tell why it isn't finding it?


r/javahelp • • Sep 19 '20

Remove a List of items from a List?

16 Upvotes

Let's say you have

OrderA { int id; ..... }

OrderB { int id; ..... }

(orderA & orderB are different types)

List<OrderA> ordersA... List<OrderB> ordersB...

Is it possible to take out ordersB from ordersA where the id's match in an elegant way with streams? Something like:

ordersA.stream().filter(...some magic...).collect(...


r/javahelp • • Aug 21 '20

Is it better to use Enums or normal final variables when working with Constants.

16 Upvotes

So I know that when using Constants you can declare a constant like this in a normal class.

final int CONSTANT_VARIABLE = 0xff

or in an Enum like this

CONSTANT, CONSTANT2, CONSTANT3

At first glance I thought that the class variable is better since u can assign it to a number unlike the enum. But then i remembered enums can have constructors and thats an advantage because I can form actions with those numbers like a normal class. So the question is, which is better?


r/javahelp • • Aug 21 '20

Skipping javac

16 Upvotes

I haven't really used the command line java and javac in a while since I use Ant or some other build tool. When did they make it so you could run the following:

java MyApp.java 

Instead of having to do

javac MyApp.java java MyApp 

I just updated from JDK11 to 14, but I guess this also worked in JDK11, just never tried it I guess.


r/javahelp • • Jun 02 '20

Setting Maven-compiler-version to Amazon Corretto?

16 Upvotes

I hope this question is still appropriate for this forum. Not really sure where I can ask for help regarding this.

I'm trying to learn Spring and SpringBoot . I have a mentor who has me learning by working on a small project and one task I was given has confused me but I won't meet with him until Friday.  I can email but I just want to run this by someone before I bring it up to him because I think I may have missed something.

I need to change the application's Java version from 1.8 to Amazon Corretto 8.
Maven currently has the maven-compiler-plugin configuration set with source = 1.8 and target = 1.8.

When I was Googling for information about this it looks like this is just set in the project's IDE or I guess the machine it's being built on?  So it's not a POM change ?

https://unix.stackexchange.com/questions/548172/maven-compiler-version-for-amazon-corretto

 <plugin>
               <groupId>org.apache.maven.plugins</groupId>
 
               <artifactId>maven-compiler-plugin</artifactId>
 
               <configuration>
 
                   <source>1.8</source>
 
                   <target>1.8</target>
 
               </configuration>
 
           </plugin>

r/javahelp • • May 15 '20

how reference variables are passed as method arguments in Java

16 Upvotes

Hello, I am struggling to understand what this question is asking for, I know in C++ for pass by reference the address of the variable is passed and altered before returning unlike pass-by value(correct me if Im wrong) but in java I thought all values were automatically pass by reference? struggling to make sense of the question so any help at all will be greatly appreciated:)

Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java. A good answer will include a simple example of each


r/javahelp • • Apr 13 '20

JavaFX & JOGL(OpenGL) - Best approach to having two updating canvases such that they do not block eachothers render loop?

16 Upvotes

Hello, I am attempting to build a JavaFX application which is a tool for modifying maps in a 3d game. I want one window to contain my 3d JOGL canvas (using NewtCanvasJFX) and another window to show a 2d representation of it.

See: https://i.imgur.com/p7SB1h9.png

The problem is that I can't seem to get the second window to render without causing my JOGL to skip frames or drop heavily in fps.

What I have tried is:

  1. Starting a new Animator in my minimap window and looping over each tile drawing to a Canvas - very slow, drops to 1fps.

  2. Creating NxN Rectangles in a Group on the Minimap window and only changing the rectangle fill color if the tile has changed - works okay but drops NewtCanvasJFX frames <50 if any Rectangles need to update.

What is the best approach here? Can I get my second window running on it's own Thread?

Both windows pull from the same NxN grid of tile definitions (basically 2d array of colors). And I have a Camera class with the camera's x,y position as well so when the camera moves I want the 2d representation to highlight which tile the camera is on.


r/javahelp • • Jan 18 '20

Solved Do-while does not end if I use Scanner Nextline

16 Upvotes

SOLVED: I forgot that I have to use equals() for Strings.

I can't find out why this program does not end when I choose "h". What is the problem with it?

try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world","root","1234")) {  
            do {
                System.out.println("");
                menuDisplay();
                System.out.print("Your choice: ");
                menuChoice = sc.nextLine();
                PreparedStatement pstmt;
                ResultSet rs;
                switch (menuChoice) {
                    case "a":
                        break;
                    case "b":
                        break;
                    case "c":
                        break;
                }
            } while (menuChoice != "h");
            conn.close();
            
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }

r/javahelp • • Jan 14 '20

Recursion with Lambda Expressions

16 Upvotes

Hello everyone!

I'm having a bit of trouble understanding the lambda expressions. My code snippet should be using a position of a tree and do the traversal with the code. I think I might be looking it at the wrong way. Maybe logic or lack of syntax knowledge of the lambda expressions is what's stopping me of doing what I want.

Any help would be greatly appreciated. Below my code snippet:

@Override
 public void traverseInOrder(Consumer<Position<E>> visit) {
                         visit = p -> {
                        inOrder(p);
          };
           visit.accept(root);
 }

 private  void inOrder(Position<E> p) throws InvalidPositionException {

     BinaryTreeNode<E> t = (BinaryTreeNode<E>) p;
     pt ->  {
                 inOrder(t.left);
                 System.out.println(t.element());
                 inOrder(t.right));
            };
}

r/javahelp • • Oct 13 '19

Homework How to format a String into rows and columns using a single JOptionPane.showMessageDialog window?

16 Upvotes

Hello! I am learning Java at university and am having trouble with formatting the output in the way that my lecturer wants. In the question below, I have no problems with parts 1 and 2 but am completely stuck on how I should get my output into 5 columns using JOptionPane.showMessageDialog (here is a link to how he wants it to look).

Question:

  1. Write a program that will calculate and output multiples of a particular value up to a particular limit.

  2. Your program must firstly ask the user to enter a positive integer value followed by an upper limit (in separate input dialog boxes).

  3. It should be noted that the format of your output must correspond exactly to the format of the output of mine (note that the multiples appear in 5 columns).

My lecturer suggested making a new variable 'output', setting it to an empty string, and then putting my output from the for loop into this variable. I have tried using ...

  • "\n"

  • "\n\t\t\t"

  • " " + "\t\t\t" + " " + "\t\t\t" + "\n" //etc, etc

... to no avail. We must use a for loop for this exercise. Could anyone offer me any suggestions? Thank you.

import javax.swing.JOptionPane;
public class Exercise3
{
    public static void main( String[] args )
    {
        String output = "";
        String posInt = JOptionPane.showInputDialog( "Please enter a positive whole number:" );
        int pos = Integer.parseInt( posInt );
    
        String upperLim = JOptionPane.showInputDialog( "Please enter an upper limit:" );
        int uLim = Integer.parseInt( upperLim );
        
        for( int i = pos; i <= uLim; i += pos )
        {
            output += i;
        }

        JOptionPane.showMessageDialog( null, "The multiples of " + pos + " between " + pos + " and " + uLim + " are: " + output );
    }
}

r/javahelp • • Jul 17 '19

Looking for good tutorials/online classes for UI and software design principles

16 Upvotes

Hi javahelp,

I'm a current student working on a side project (as the necessity for them in this job market seems growingly apparent), and I've just recently finished working on most of the primary functions, but it's still only a command-line interface. I want to start working on developing a solid user interface (and then ultimately add more functions) but I keep finding myself overwhelmed with all of the possible UI frameworks and how to organize what I imagine will be a rapidly expanding source code in a way that won't hinder me in the future.

I'm having a lot of trouble getting started with the actual coding for this stage of the project because I don't want to start making something that will be very difficult to manage and add features later on. A lot of advice I've read was to just "start coding and focus on pushing a prototype and worry about that stuff later", which I think is decent advice, but I want least have a good template to follow or something along those lines.

I'm not a super experienced programmer, but I can solve most isolated programming problems and 'fill in' the functions from the assignments I get in class. What I'm finding though is that my classes aren't teaching me how to organize functions or design a solid program, as all the assignments I'm doing right now have structures already laid out for me. I'm looking for somewhere where I can learn to properly *design* a program's structure on a larger scale.

If anyone has any suggestions, please let me know.

PS. The software I'm working on is only going to be a local .exe program. I figured that's a good place to start.


r/javahelp • • Jul 07 '19

My very first Java / Raspberry Pi project is almost complete!

Thumbnail
self.learnjava
16 Upvotes

r/javahelp • • Jan 16 '19

[Humor] Interview with Object Reference[source: HeadFirst Java]

16 Upvotes

HeadFirst: So, tell us, what's life like for an object reference?

Reference: Pretty simple, really. I'm a remote control and i can be programmed to control different objects.

HeadFirst: Do you mean different objects even while you're running? Like, can you refer to a Dog and then five minutes later refer to a Car?

Reference: Of course not. Once I'm declared, that's it. If I'm a Dog remote control then I'll never be able to point(oops - my bad, we're not supposed to say point) I mean refer to anything but a Dog.

HeadFirst: Does that mean you can only refer to only one Dog?

Reference: No. I can be referring to one Dog, and five minutes later I can refer to some other Dog. As long as it's a Dog, I can be directed(like reprogramming your remote to different TV) to it. Unless... never mind.

HeadFirst: No, tell me. What were you gonna say?

Reference: I don't think you want to get into this now, but I'll just give you the short version - If I'm marked as final, then once I'm assigned a Dog, I can never be reprogrammed to anything else but that one and only Dog. In other words, no other object can be assigned to me.

HeadFirst: You're right, we don't want to talk into this now. OK, so unless you're final, then you can refer to one Dog and then refer to a different Dog later. Can you ever refer to nothing at all? Is it possible to not be programmed to anything?

Reference: Yes, but it disturbs me to talk about it.

HeadFirst: Why is that?

Reference: Because it means I'm null, and that's upsetting to me.

HeadFirst: You mean, because you have no value?

Reference: Oh, null is value. I'm still a remote control, but it's like your brought home a new universal remote control and you don't have a TV. I'm not programmed to control anything. They can press my buttons all day long, but nothing good happens. I just feel so...useless. A waste of bits. Granted, not that many bits, but still. And that's not the worst part. If i am the only reference to a particular object, and then i'm set to null(deprogrammed), it means that now nobody can get to that object i had been referring to.

HeadFirst: And that's because...

Reference: You have to ask? Here i've developed a relationship with this object, an intimate connection, and then the tie is suddenly, cruelly, severed. And i will never see the object again, because now it's eligible for[producer, cue tragic music] garbage collection. Sniff. But do you think programmers ever consider that? Sniff. Why, why can't i be a primitive? I hate being a reference. The responsibility, all the broken attachments...


r/javahelp • • Mar 20 '18

JDK10 and Maven: found issues and how to solve them

16 Upvotes

Hi there all,

I just downloaded the new JDK and I wanted to try it with a silly-test-project with Maven immediately. However I found some issues in the maven-compiler-plugin that I want to share and how to solve them.

First of all: plain compilation. If you're going to use the default maven-compiler-plugin (that is the version 3.1 even if you use Maven 3.5.3) you have to specify a different target version from the source version:

<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>

with JDK9 was the same (target 1.9). After that the project compiles correctly.

The second issue was a little bit more complicated to understand, since after I upgraded the maven-compiler-plugin to version 3.7.0 the project was compilable, but when I tried to require a module (for instance java.xml), maven throws an exception «IllegalArgumentException». If you try to downgrade the Java version, from 10 to 9, everything is fine, but not vice-versa. After some investigation (by enabling the maven debug log) I found that during the testCompile phase, maven-compiler-plugin uses cglib and ASM (I didn't know that) and since I found a similar issue on reading the module definition (https://gitlab.ow2.org/asm/asm/issues/317794) I tried to upgrade the plugin-dependency.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <jdkToolchain>
          <version>${java.version}</version>
        </jdkToolchain>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.ow2.asm</groupId>
          <artifactId>asm</artifactId>
          <version>6.1</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

And it worked! :)

I hope it helps someone! Enjoy!


r/javahelp • • Mar 01 '16

[META] To all submitters: Please read and follow these simple rules!

16 Upvotes

We have a few simple rules that we have to ask you to follow when making a post:

  • Read the information in the sidebar

  • Tell us What have you tried?

  • Read the Learn to help yourself guide.

  • Use a proper title:

    • "I need help" or "Help with homework" are never appropriate titles. "Need help with comparing Strings" is a better title.

    • The title should not describe your problem in full - that's what the post body is for.

    • No links in the titles - they are not clickable.

    • Nothing is "URGENT", nor "ASAP" - if you have waited too long, this is your problem, not ours. Such posts will be removed without warning.

  • Ask specific questions.

  • Describe what your program should do - in the main part of the post, not in the title

  • Describe what your program does/doesn't instead - "Is not working" is not an appropriate problem description.

  • Post any error messages you get in full as the compiler gives them

  • Do not ask for complete solutions - we will not do your homework

  • Do not ask for or offer help via PM - post your questions here so that others can also learn

  • When asking specific questions, post your code - we cannot help you without seeing your code.

  • No screenshots of code they make it unnecessarily difficult for us to help because we might need to execute the code to find errors. With screenshots we'd have to retype the code and this is asking too much.

  • Format your code or use a code hoster

    • one empty line before the code block

    • each line indented by 4 leading spaces

    • Use pastebin for longer (> 50 lines) code in a single file

    • Use Github gist for longer (> 50 lines) code in multiple classes as gist allows multiple files

    • See the Code posting guide for full instructions

  • When posting code, make sure that the line numbers in the code match the line numbers of your errors.

  • Code only posts are not allowed - you need to describe your problem

  • No executable code - We cannot check whether your compiled code is malicious or not, so we will not execute it and thus you will not get help.

  • No "fix my code" posts. You need to describe your errors/problems

  • When posting your assignments, please, try to post the original assignments as you have been given. Re-telling the assignments often leads to confusion and misunderstanding.

  • Include as much information as you can. We should not have to keep asking for bits of information.

  • No links to your stackoverflow questions - we are not a second opinion to stackoverflow, nor are you going to get answers here when you didn't get satisfying ones there.

  • Demonstrate that you have spent some effort - we will not do your work.

  • When helping, please do not provide a complete solution, rather try to guide OP towards the solution.

  • If your problem is solved, please use the "Solved" flair, upvote the user(s) who helped solving your problem, and leave a short "Thank you" note. This will encourage users to keep helping.

The quality of help that we will be willing to give/able to give is directly proportional to the quality of your post. The better you explain your problem, the better we can help you.


To all new account posters:

Reddit is currently under massive spam attacks and therefore we have a minimum account age rule in place. Your account must be older than 2 hours to be able to post. Should you receive a notification that your post was removed because it came from a new account, please wait until your account is over 2 hours old and then repost.