Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ivantod
Mar 27, 2010

Mahalo, fuckers.

chippy posted:

All working now, thanks for that.

edit: Although for the record the files that are in "res" in Eclipse are still in the root of the .jar and not in a folder called "res", any way of rectifying this?

The folder 'res' does not exist in your JAR file for the same reason that 'src' does not exist either, they are just eclipse source roots, so to speak. If you want a subfolder to appear in the JAR, create a folder in eclipse under 'res' or 'src' and put the required files there instead.

Adbot
ADBOT LOVES YOU

ivantod
Mar 27, 2010

Mahalo, fuckers.

mustermark posted:

I've only started to get into Java programming for work, and I gotta say that JDBC makes me physically ill and depressed. Are there any good modern guides on it, or am I stuck with having to write twenty lines of ps.setHate(..., "Fury");?

If you really have to use JDBC here's a hint for you: instead of saying
code:
ps.setXXX(1,...);
ps.setXXX(2,...);
...
ps.setXXX(999,...);
do this:
code:
int i=1;


ps.setXXX(i++,...);
ps.setXXX(i++,...);
...
ps.setXXX(i++,...);
because it's going to be much easier when you inevitably have to add a parameter in the middle for whatever reason.

Is there a reason why you have to use just JDBC? Because there are other options, such as Spring SQLTemplates or even Hibernate/JPA, but it all depends on the size and what kind of project you are working on... As said above, JDBC is pretty easy to use, but kind of low-level. The alternatives are a bit higher level, but may require more effort, especially Hibernate.

ivantod
Mar 27, 2010

Mahalo, fuckers.

AlsoD posted:

Ok, so I've gotten myself really confused over how dependencies work in Java - am I correct in thinking that "import org.apache.xxx" gives you absolutely no clue which .jar file actually contains the class you want? I have 15-20 .jar files, is there any way to search through which ones contain which classes easily?

Other than package itself having a name similar to the name of JAR file itself, no, it doesn't give you any kind of clue. As long as the JAR is included in the classpath, the class will normally be found by gthe JVM, regardless of which JAR it physically resides in (so in theory you could pack everything in one big JAR file and it will still work--I am NOT recommending this of course).

I am not aware that Eclipse has any functionality to search arbitrary JAR files for classes--however, if the jar files are included as build path of one of the projects in the workspace, the Ctrl+Shift+T should be able to find where a particular class is based on its name.

There are also tools which can search a whole directory of JARs for a particular package/class, such as this one.

ivantod
Mar 27, 2010

Mahalo, fuckers.

Newf posted:

I don't think I know how to use BufferedReader.

code:
// method which creates a bufferedreader only if it finds the file
// referenced by the user
	public static BufferedReader mkReader(){
		Scanner input = new Scanner(System.in);
		System.out.print("What is the name of the input file? ");
		try{
			return new BufferedReader(new FileReader(input.next()));
		} catch (Exception x){
			System.out.println("File not found!");
			return mkReader();
		}
	}
This is a method in the main class, to be called in the main method. I think maybe it won't work because the main method is outside the scope of the FileReader created in the class? Is this right? Can I rewrite the method to return both the FileReader and the BufferedReader?

First of all, I think that calling the method recursively in case of exception is probably not the best idea.

Second, when you say 'not work', what do you mean? This code will for sure not work for any file names that contain spaces since Scanner.next() will normally give you part of input until the next whitespace character every time it's called, until it consumes the entire input. You can use input.nextLine() to get the whole line of input (not including the line separator character).

ivantod
Mar 27, 2010

Mahalo, fuckers.

Ensign Expendable posted:

Wow, I never read about this feature before. Seems really useful. Too bad my workplace is using ancient versions of Java for no good reason. It's not like we have apps that rely on specific hacks to make them work, one of the devs just refuses to upgrade to new versions.

Well this one is only in Java 7, so that's going to take a while anyway. Even the stable version of Eclipse doesn't support Java 7 yet (although I think various betas do).

ivantod
Mar 27, 2010

Mahalo, fuckers.

rhag posted:

I have quite a few plugins installed on eclipse at home and never had troubles. At work, i have a relatively weak computer, therefore i prefer to have multiple installations, each with a set of plugins needed for a specific task (j2ee, STS - Grails, plain java, a php one, etc.).

Works wonders.

I have found this also. Rather than making one installation of Eclipse with all sorts of stuff in, it's better to have several with fewer plugins each. I used to have one installation with about a million things and it literally takes 5 min until it's ready to be used after it's been started.

More generally, as people have said before, I feel that overall Eclipse is a decent enough basic IDE (especially for a free product), but the issue is that the quality of different parts is extremely variable, depending on how much love they get from the various development teams. Like, the basic Java app development is great, but as soon as you start to move into the Web application territory (WTP and all that), it starts to get a bit dodgy, and so on.

In my previous job I had to use RAD due to Websphere dependency, and yeah... let's not go there.

BTW, check this one out: http://images2.wikia.nocookie.net/__cb20070215172454/inciclopedia/images/e/e7/Error_interno.jpg. It's an old version of Eclipse and the screenshot is in Spanish, but basically it shows Eclipse experiencing an internal error of some kind and then another internal error while trying to display an internal error message. It's pretty funny.

rhag posted:

But, i have to agree, Idea is a drat good IDE. Too bad the free version is quite limited. Netbeans can go die in a fire.

I have been considering this for quite a while, especially since I use PyCharm for my Python work from the same authors. But a couple of (halfhearted) attempts to try out Idea have not ended successfully for me. I'm kind of trying to find time for a proper trial, rather than giving up when I can't find the appropriate shortcut, especially since now I have experience with PyCharm which I like very much. How is the plugin ecosystem on Idea? I'm guessing probably more limited compared to Eclipse? Or not?

ivantod
Mar 27, 2010

Mahalo, fuckers.

krooj posted:

What do you do with technical leadership that wants to use modern Spring MVC (3+), but doesn't allow annotations? No @Autowired, @RequestMapping, etc... I am literally implementing Controller#handleRequest. :ughh:

I am curious, what is their justification for this decision?

Have you suggested using Struts 1.1 instead?

ivantod
Mar 27, 2010

Mahalo, fuckers.

FateFree posted:

Generics in Java are notoriously horrible, don't take any warnings as a sign you are doing the wrong thing. In some cases, suppressing warnings are the only thing you can do. But in your case, I don't see how it wouldn't work..if its complaining about raw types that means you arent declaring SubClass<Subclass.Options> everywhere you use it.

Unfortunately, suppressing the warnings partially defeats the purpose of using generics (such as it is) in the first place. Because you are only guaranteed no class cast exceptions at runtime if there are no generics-related warnings during compilation. Note that I am not saying here that generics in Java actually make sense, just stating simple facts of life. :v:

ivantod
Mar 27, 2010

Mahalo, fuckers.

Lamont Cranston posted:

So I’m moving into a new position at work where I’m going to be writing mostly Java (coming from a position where I’ve been writing Go almost exclusively for about two years and then perl before that). I haven’t written a line of Java since college when Java 5 was the new hotness. Should I just pick up a copy of Effective Java and go from there? I’m also not super familiar with the intricacies of maven or the tooling around Java in general (not what they were teaching back in Algorithms & Data Structures a decade ago), is there a decent reference/primer for that kind of stuff?

As for books, I would recommend this one: https://www.manning.com/books/modern-java-in-action

It should be coming out in the next few days hopefully, but if you buy it now you can already read the work in progress version (which should be 99% finished anyway) so you wouldn't have to wait.

Disclaimer: I did some technical review work on the previous edition of this book.

hooah posted:

I've had to go back to using Eclipse at work the last few weeks after months of IntelliJ, and it just doesn't compare. It's crashed/frozen a few times, so the loooong startup time is really noticeable. The whole "perspective" thing is kind of annoying, especially when debugging and fixing code. It's pretty ugly.

Honestly, if there is one thing I wish they could go back and revisit a decision that was made early on in Eclipse's life, it's perspectives. It probably seemed a good idea at the time, but it just hinders instead of helps most of the time as far as I am concerned. I feel the way it's done in Idea works a lot smoother, esp. with debug.

ivantod fucked around with this message at 19:42 on Sep 3, 2018

ivantod
Mar 27, 2010

Mahalo, fuckers.

Sindai posted:

Remember that all lambdas and anonymous objects are just instances of anonymous inner classes.

Saying that a lambda is just an instance of an anonymous inner class is oversimplifying things by quite a bit.

Here is a talk by Brian Goetz that goes into some detail:

https://www.youtube.com/watch?v=MLksirK9nnE

ivantod
Mar 27, 2010

Mahalo, fuckers.

Objective Action posted:

Java has a million excellent web client libraries but given you are already in Spring Boot land the two quickest options are probably RestTemplate and the WebFlux WebClient.
Also since Java 11, there is actually a proper HttpClient included in the JDK itself, no need for external libraries.

ivantod
Mar 27, 2010

Mahalo, fuckers.

adaz posted:

ah yeah you're right. I thought it was every 18 months but it literally says it righ there - every 3 years is an LTS.

I wish it were 18 months, so I wouldn't have to wait until Java 17 for multiline strings... because that was added in Java 13 and the current LTS version is 11.

ivantod
Mar 27, 2010

Mahalo, fuckers.

Splinter posted:

I'm a big fan of Pebble (which is a port of Twig, Symfony's templating engine). Recommend it as an alternative for those that don't gel with the "everything is a tag attribute" approach of Thymeleaf and some others.

A hearty recommendation of Pebble from my side also. We use it at work and i'm very happy with it. It seems to give you enough power to do things, but without ending in JSP territory where suddenly you find that your entire application code is now inside the JSP templates. Also I like the Twig/Pebble template inheritance/block override concept as I feel it really gives a lot of flexibility for not a huge amount of effort.

ivantod
Mar 27, 2010

Mahalo, fuckers.

M31 posted:

Another option would be BigDecimal (which also has unexpected behavior when it comes to currency amounts, like 1 != 1.00).

Everybody learns quickly to use compareTo() or stripTrailingZeros() when comparing BigDecimals. The trailing zeros also count when calling getScale(), so you better remember to do stripTrailingZeros() also there if you want to check e.g. that the scale is correct against the currency that you're using or stuff like that.

But to add to the pile for the original question, definitely never use double/float for money amounts, because that just doesn't work. Either integers and work in cents or arbitrary precistion decimal numbers like BigDecimal.

ivantod
Mar 27, 2010

Mahalo, fuckers.

smackfu posted:

I ran into some real library code that had an interface that looked like this:

Then in the implementation cast the value to T before returning it.

So you could do:
int i = entryAt(0);
Or
String s = entryAt(0);

No cast required, no compile time type checking, it just failed at runtime with a class cast exception if you picked wrong.

Is this as terrible as I think it is?

So what is this, some kind of collection that can contain elements of different types and it's up to the caller to know what is the type at a particular index (since the generic type T is only on the method and not on the whole class as far as your example shows)?

If so, then it's not great, no.

ivantod
Mar 27, 2010

Mahalo, fuckers.
Please, for god's sake, never ever ever code like this in real life! This is insanely bad and I can't believe this is how they are teaching you to do it.

For a moment I thought I confused this thread with the other one where we post people's bad code!

ivantod
Mar 27, 2010

Mahalo, fuckers.

Jabor posted:

I think ivantod is mainly talking about the unnecessary inheritance you're being asked to implement. Using inheritance like this is an approach that is often tantalizingly easy to begin with, but ultimately makes programs very inflexible and difficult to maintain, and has fallen out of favour in many professional circles for that reason.

It's challenging to offer criticism here because a lot of the bigger issues are likely to have been dictated to you by the assignment rather than something you have control over.

For example, two fundamental isues are:
- You wouldn't really write a CarLot class to begin with. You'd just have a List<Car>, and some utility methods that accept a List<Car>. Some of the methods you've been asked to implement you wouldn't even write in the first place - you'd just use some of the more generic collections methods that accomplish the same thing.
- You wouldn't write a mutable Car class with getters and setters. You'd write an immutable (can never be changed) Car class, that only has getters. For state that is expected to change, such as whether the car has been sold, you might track that in some other location rather than as a field in the Car class.

But it's likely that both of those decisions aren't ones that you made - they're things dictated to you, that you need to do even though they're not modern best practice.

Correct.

Sorry for not being 100% clear, but I was upset with the fact that the people who are supposed to be teaching the OP are telling them that is a good way to do things (e.g. by inheriting from ArrayList and stuff). The criticism was not intended against the OP, who I assume is doing their best to try to pass this class as is not at all to blame here for the stupid assignment they were given.

OP, I'm sorry, I absolutely was not indending to criticise you, sorry that it came across that way! I'm just angry at your teachers!

ivantod fucked around with this message at 15:23 on Dec 1, 2021

ivantod
Mar 27, 2010

Mahalo, fuckers.

Mycroft Holmes posted:

Getting only two errors, one in SetInventory and one in loadfromDisk. Both refer to my use of "this" and say "The left hand side of an assignment must be a variable"

"This" is like a pointer to your current instance, so you can't assign a new value to "this" from within a class instance itself, because that would mean that you lose the reference to that current instance whose method is just now executing. But ArrayList has methods like clear and addAll which you can use to update the contents of "yourself". Possibly look into using one of both those in the places where you are trying to assign something to "this", which are giving you errors.

ivantod
Mar 27, 2010

Mahalo, fuckers.

Clockwerk posted:

If you’re doing Swift, I think your only choice is Xcode, but I could be wrong.

You're not wrong. The only real competitor, JetBrains AppCode has just been discontinued a few days ago: https://blog.jetbrains.com/appcode/2022/12/appcode-2022-3-release-and-end-of-sales-and-support/

Adbot
ADBOT LOVES YOU

ivantod
Mar 27, 2010

Mahalo, fuckers.

imnotinsane posted:

I can't seem to wrap my head around how I am meant to solve this issue, I think I am coming from the wrong angle.

If I have an class object that contains 3 values, for example Student() and it holds firstName, lastName, studentId.

I have then created an array of Student[] objects, so now i am holding something like
code:
Student[] list = new Student("John", "Smith", 123), new Student("George", "Citizen", 567), new Student("Alan", "Bates", 789);
Now if i wanted to sort them by say student id i can't really loop through the array list because all three values are held in the same object. I was thinking maybe I am meant to use a 2d array of objects, so that i have list[0][1-3] but then that would still leave me with the same problem as I am not really storing the actual value just the object it self that contains those three associated variables.

The other way I was thinking was to make a method that returned the values explicitly as array, for example, Student.getArrayValues() and I guess create a new 2d array list on the fly and then manually sort that through a loop and print the results but it feels like I'm doubling up my work for nothing and there should be a simpler method.

Any help would be appreciated

Maybe this will help you: https://www.baeldung.com/java-8-comparator-comparing.

By using the Arrays.sort(...) method you can give it a lambda function telling it how to compare the objects. This allows you to compare by one (or more) chosen fields (e.g. in your case student id).

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply