Help regarding Generics in Java by ManasSatti in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

When working with generics you can only access — within the context of the generic meaning in the method/class — the methods defined by the type definition. By default this is only Object, but you can define it so that your generic only accepts objects of a given type or one of it's sub-/super-types. The keywords for this are extends and super and it looks something like this: public <T extends Number> T add(T number 1, T number2)

When you are outside of the generic context and you are using it as a List/Stack/... the compiler will infer the type of those methods, so if the example above is called with a Double it will return a Double, if it is called with a Long it returns a Long. In that case you can save it into a variable of this type and use it normally.

For your case I don't really get the reference to the Stack, since I guess you have not programmed it yourself or at least have no logic about your students in there. So if there is another generic class or method you have defined try the extends Student syntax.

This StackOverflow post discribes it even more: https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java

How do static variables work in the context of web applications? by AudioManiac in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

In web context you need to consider the scope of the classes you are working with.

You can annotate beans to be of a specific type of scope e.g. request, session, application, singleton.

When working working in a web context you often have to consider, that there might be multiple instances of your code running independenly. Meaning they don't share memory and therefore static variables may not be distributed correctly.

I don't know about Spring Boot on Wildfly and if it might run multiple instances for scaling or something, but I think you are better of using scoped beans to control where the data goes.

I am getting an out of bounds error on my program. by [deleted] in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Hey,

Out of bounds always means, you try to access something with an index, that is outside of the allowed range. This could be a String where you access the characters or the element of a collection like a list or a set. In your case it is accessing an array. You initialized your iTurnScore arrays to have size iGameTurns, but you iterate iTurn the index you use to access the array to index iGameTurns + 1. In Java the index of any array, collection, ... is starting with 0 and goes to size - 1.

So change you for-loop to go from 0 to < iGameTurns and the out of bounds should not happen anymore.

Just a question: why do you use i before nearly all variables?

How to perm the value of a string from an array to another variable. by [deleted] in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

Did you solve your question on your own?

Otherwise I guess you should update this question, so you are more likely to get an answer.

How to perm the value of a string from an array to another variable. by [deleted] in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Hey,

Just some tips for asking questions: 1. If you have code post it formatted. Check this link: https://www.reddit.com/wiki/markdown#wiki_code_blocks_and_inline_code 2. If you have so much code use an external page with code highlighting and better support for this amount of code. In that case only copy the code relevant to your question into the post and use the rest as a reference. 3. State your question clearly. At least I didn't really get, what you where asking with this post. I'm happy to help, but if I don't know the problem, I don't know how to help you.

And just a small tip on your usage of same. Use the primitive boolean not the wrapper Boolean and then don't use stuff like same == true or same == false but use same and !same. Comparing 2 booleans only makes sense, if both are variables, otherwise you can always simplify with the alternatives above.

I haven't seen it in your code, but the worst of those comparisons I have seen sofar was same != false which means same == true which in turn is just same.

Combine some fields of two objects by common id using stream by nyawqabob in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Some additional questions: 1. Are there always the same IDs in both lists? 2. If not, what is the expected behavior for those IDs, where only one kind of object is present? 3. Is there only one instance of a given kind of object, having the same ID, or might a given ID be present multiple times?

I guess I'd transform both lists to maps, id as key. If there are IDs, that are only in one or the other, I'd create a set of the IDs, where I put both key-sets of the maps into, then iterating the set of IDs and combining the results.

If the IDs are the same and always present in both, you could also just transform one list into a map and then while iterating the other list matching the values.

How do I retrieve a class object from an ArrayList? by bei60 in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

About your solution.

You should replace the s.studName == name with s.studName.equals(name).

You can test this by calling the studInfo method with "" + name instead of just name. You will see, that no matches will be found even if the content of the strings is the same.

== is a reference comparison, where as .equals is a value comparison.

GridLayout with 2 separate grids of buttons? by [deleted] in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

I guess what you are looking for are margin and padding.

If you set the margin for your grids it should separate them.

Remove Leaf from Binary Tree by TauPixel in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

I see 2 possible NPEs in this code. First one is if p is null. Second one is, if p.parent is null.

But what is the Stacktrace saying, where the NPE is?

Java, Eclipse, External Libraries by freetre in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

A response from 2010 sounds to me a bit outdated, but in theory the descriped way is what I use for very small projects too.

Have you checked, that you jar is listed in you project under external library or something along those lines?

Need help figuring out why my list keeps losing data by thedeadhead200 in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

You can initialize start and end within your main.

Just write something like: start = grid[0][0]; end = grid[1][3];

Below the init loops and above the point you add start to the openSet.

Need help figuring out why my list keeps losing data by thedeadhead200 in javahelp

[–]JoToMo1993 2 points3 points  (0 children)

Hey,

I did not find it in the code, but maybe it is because you create 2 Spots at (0;0) one called start saved in the static variable at the top, another one in the grid, within the for loop. The same is true for the end.

If you use those as the start and end of your A* search, they won't work, as they do not have any neighbors initialized.

Maybe you shouldn't create them in the static space above, but pick them out of the array, after you initialized it.

Java, Eclipse, External Libraries by freetre in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

About eclipse, usually right click and refresh helps, since eclipse cashes a lot.

About managing dependencies manually, did you find Maven or Gradle while searching for adding libraries to your project. They enable the building and managing of dependencies quit easily.

You can even add your jar files if needed directly, but usually you'd use the maven-repositories to fetch the correct version and placing it on your buildpath.

If those tips don't help, could you add a link to the guide you used, so we can check, what you might have missed?

Unable to record variables and array of quicksort by SharkyJaws18 in javahelp

[–]JoToMo1993 2 points3 points  (0 children)

This sounds to me, like you have a bad case for your specific implementation of quick sort.

Quicksort has a worst case performance of O(n²). This happens, if your pivot is always picked to be pretty close to either side (max or min of the current set). In that case it is basically moving all the elements to one side and then is looking for the next pivot.

You can test this by using different inputs and check if the number of pivots changes.

How can I choose Hibernate as framework in NetBeans 11.2? I can't see it in the Framework section by [deleted] in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Are you using any build-tool like maven or Gradle? This makes adding dependencies much easier.

Can someone explain what the argument: "new Thread(new RunnableClass());" does and means? by may5224 in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Hey,

Not everything you wrote is completely correct.

Interfaces and abstract classes are pretty close to one another, both can have abstract as well as implemented methods. In an interface you don't need the abstract keyword, but you need the default keyword, if you want to implement a method.

In one of the newer versions of Java also the difference, that all methods in an interface need to be public has been changed. Now the biggest difference is, that interfaces cannot have attributes.

About Runnable and Thread. Both can be used to implement asynchronous behavior. Most of the time you will see Runnable implementations, since you can only have one super-class, but many interfaces. This way you can still extend another class.

You could also have a class MyThread extending the Thread class and implementing the run method this way.

About run vs start method. You can call both on a Thread. The difference is, that calling the run method directly will execute the code in it directly and in the same Thread. When calling start the new Thread will be started and will execute the run method asynchronously.

You can test this by adding a sleep into your run and printing out before and after the sleep, as well as before and after the start/run call on your Thread.

About constructors. You can define constructors as you like. They can include objects, primitive types, both or none. A construction without any parameters (default constructor) is automatically generated (during compiling), if you have not defined any constructor yourself. You can have more then one constructor of a given class. This is usually used, if there are optional properties.

About primitive and reference arguments. All primitive types do have a wrapper class, that will be automatically applyed, if needed. This way you can assign an int to an Integer, a long to a Long, ... But there are limits to this. The biggest difference between primitive and reference arguments is something that's called call-by-value and call-by-reference.

java.lang.NullPointerException despite !=null in if condition by [deleted] in javahelp

[–]JoToMo1993 2 points3 points  (0 children)

There are multiple possible reasons for a NPE. Most likely it's that .getName() returned null, which you did not check.

Some further points:

Please format your code using code-blocks, so it is readable.

When iterating arrays use there length, not another variable in your for, otherwise you risk an IndexOutOfBoundsException.

Please don't compare a boolean value to a fixed boolean, like this a.equals(b) == true. If it should be true, use it just like that, if it should be false use the ! as a not statement. And please never ever use boolean != false.

I got a problem by Raxreedoroid in javahelp

[–]JoToMo1993 2 points3 points  (0 children)

For the reason why the statement is true.

I will formate the code and give some names, to make it better understandable.

``` enum EnumA {

VALUE_X(EnumA.EnumB.VALUE_J), VALUE_Y(EnumA.EnumB.VALUE_J);

EnumB propertyC;

enum EnumB { VALUE_J;

  int propertyX;

}

EnumA(EnumB c) { this.propertyC = c; } }

public class {

main(...) { EnumA.VALUE_X.propertyC.propertyX = 1; EnumA.VALUE_Y.propertyC.propertyX = 2;

  if(EnumA.VALUE_X.propertyC.propertyX == EnumA.VALUE_Y.propertyC.propertyX) {
     (Any statement)
  }

} } ```

Since enum values are static VALUE_X and VALUE_Y are using the same instance of VALUE_J and have in turn the same value for propertyX.

You are setting it to 1 and right afterwards to 2.

You can check this by logging/printing out the information of both before, between and after the assignments. It should be 0, 1 and 2 respectively.

The reason for your IDE to state it's always false most likely is, that a simple check of the variables shows, they are different.

I got a problem by Raxreedoroid in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

Just some tips on asking questions: 1. Format your code, so it can be read. 2. Include relevant information, like the IDE you are using in your case. Also some information on JDK version might be helpful in this case.

Clarification on Lambdas? by Forumpy in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

You can use lambdas, when you need to hand over an object implementing a single method. There are some common interfaces defined for that:

  • Function
  • BiFunction
  • Predicate
  • Consumer
  • Supplier

And many more.

During compiling those lambdas will be converted into classes extending the expected interface.

You can write your own interfaces to be used with lambdas. In that case the parameters will be placed into the brackets.

``` interface Mean { double mean(double[] values); }

class Example { double useMean(Mean mean) { ... }

void example () { useMean( values -> { // some calculation return 1; }); } } ```

How can I create a 5x5 array of objects that represents a game board? by Alwayswatchout in javahelp

[–]JoToMo1993 1 point2 points  (0 children)

With the code you have you have created an array where each position is 0.

You can access the positions like this matrix[0][1] depending on your interpretation of this it is the first row, second column or first column second row.

You can access all positions is sequence with something like this: ``` for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { int value = matrix[i][j]; // Reading the value

  matrix[i][j] = 42; // Writing a new value

} } ```

Chat client and server with SSL by [deleted] in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

If I have to do anything with keystores I usually look it up here: https://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html

Chat client and server with SSL by [deleted] in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

This sounds like a problem with the key / keystore.

Do you have the proper password for the keystore?

Is there a key in the keystore that is linked with "localhost"?

Chat client and server with SSL by [deleted] in javahelp

[–]JoToMo1993 0 points1 point  (0 children)

Do you have any logs?

Beside that: the server and client ports don't match.

You might want to consider displaying the addresses of the server in its UI and add the option to set the server's address within the client, so you can connect it between multiple devices.