Damon McNeill

Ranch Hand
+ Follow
since Apr 06, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
11
Received in last 30 days
3
Total given
12
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Damon McNeill

Jesse Silverman wrote:I had the discussion with Campbell in another thread some weeks back, and made almost exactly the same points you did.

I don't even know how one goes about doing the GPU-based stuff in Java, the material I worked with was focused on C/C++/Python and FORTRAN only.

There is some vector API work that has made its way into Java 17 for future progress in this direction.

I agree with Campbell's main point that short of dealing with HUGE numbers of data, there is very little to be bought using float type, at least on the CPU-based and FPU-based instructions that I think Java uses for all normal math.

And yes, this is all way, way beyond the scope of Beginning Java.

Cheers!



Yes I agree, lacking other reason,  using double over float may or may not  avoid some precision issues. I still can't agree it should just be the default though. There is a reason for the different choices of types, after all.
4 days ago

Antonio Moretti wrote:
I know how to parse a CSV. I just used the split() method and put each value in a array and took it from there. I just wondered if there was another way of reading the file in other than line by line, which to me, suggests a lot of disk operations if the file is long. That was my question all along.



No. How else would one read a file other than line by line?

This is why you wrap your input stream with a BufferedInputStream

You could read the entire file contents as a byte array, pass that as the input to a ByteArrayInputStream, create a Scanner on that. But still you have read the entire  file. I don't see you're premature optimization.
4 days ago
Scanner basically is a parser that reads Java objects from a input source (file, or string). What are the Java objects? Integers, Strings, Floats, Doubles, basically the primitive types.

You need to understand the Scanner API.

https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Scanner.html

So basically the API is that you have an input source and you scan to see ifHasNextX, where X is some type. Then if that's true, you get the object with nextX. Say X = "Int".

Unfortunately that doesn't parse CSV. You would need to use ifHasNextInt() -- for example -- and nextInt() with a combination to parse out the separating commas between those items

Given an input file

1,2,3

Your program should then produce an array of objects [1, 2, 3]

Scanner won't help you to distinguish the items in a CSV file and it is not a CSV parser.

It WILL help with parsing basic Java data types such as Int and Double (*NOT* string) but its up to you to parse out the comma-separation.

4 days ago

Campbell Ritchie wrote:Unless you have an assignment reading a CSV file, you are probably better off downloading an app optimised for CSVs.
I wouldn't want to store values read in an array, not if I could put them directly into an object.



No I was saying read the records into an object then append them to an array. At a lower level, yes, you would need to store each individual field somewhere until the end of the line, then aggregate those into a logical object.
4 days ago

Tim Holloway wrote:I'm not sure what this "dependency management system" thing is supposed to be.

Maven is a build system where the dependencies are indicated as standard resources. Maven keeps a local cache (repository) of such resources so that it will automatically pull needed dependencies from central Maven repositories as needed, then keep them in the cache. It will also transitively pull indirect dependencies so that the build is self-contained.

Ant is a build system where you have to pull all of the dependencies manually and store them locally (and where people stored them locally was always one of my biggest headaches). You basically either had to store them in the Ant project directory and end up with myriad duplicate copies if you had lots of projects or put them in a location outside the project which made shipping projects difficult and installing them on remote systems sometimes impossible. Or you could use Ivy, which basically does what Maven does, and, in fact, I'm fairly certain pulls from Maven's repositories (as does Gradle).

As for managing dependencies, I can attest that while Maven can handle pulling, caching, and building with dependencies, if you modify a project where one dependency's upstream version requirements clash with another's, you're in for fun times. If there's a tool that can unsnarl that particular problem automatically a lot of people would love to hear about it.



I hear you. At some point it just becomes a system like yum or apt, no? And neither yum nor apt is the same as make. I like to feel in some control of what my dependencies are and where they come from, after suffering multiple headaches, and with an automated downloader and installer maybe I am out of a job, lol. The where things are installed is super important, as well as configuring the environment variables, paths, and so forth. Most people don't even understand where or what the dependencies are.

if you modify a project where one dependency's upstream version requirements clash with another's, you're in for fun times



This seems to happen quite often. Especially in a dynamic language like Python where your failure occurs at a large distance from the the seemingly innocuous root of the stack trace.

4 days ago

Antonio Moretti wrote:Hi

Can someone please show me how to use StringBuilder with String.Format features, so I can use the format specifiers? Is this a thing? Or should I just use normal concatenation?

Thanks T.



There is a bit of confusion here. StringBuilder and the String.format serve entirely different purposes. If you want format specifiers, ignore StringBuilder.

https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)

You can use the output of String.format as an input to StringBuilder.

For what its worth, I prefer to see a nicely designed format specifier template than a hodge-podge of randomly concatenated + strings. I feel String.format is the way to go and I always use it even when +ing strings might work.
Also its even worse if you hide the +Strings chain behind a nasty long unreadable chain of StringBuilder.appends().
4 days ago
What is the application? Inputs/outputs?

I'm not sure how to read a CSV file line-by-line into an array of Strings (as is easily done in Python) in Java, but one could potentially write a small class that accomplishes that.

You may get away with not having to read the entire file, depending on what output your program should produce. Does the output of the program depend on the entire file contents or can you produce an output for each line?

Either way you're gonna have to read the entire file, right? For a CSV file then its 1 record (object) created per line, with each field in the line corresponding to an element of the record. You could use a simple ArrayList to store each object as you read it in from the file, line by line.

Use the methods of the Scanner class to read in each property (a comma separated value) of each line in the file, into a new object, named after its logical purpose (GenericRecordClass), and store these into an array I suppose.
4 days ago

Tim Holloway wrote:Back to the original topic:

That said, the upside to Ant is that you can build complex build systems in any way you like. In Ant you know exactly what the process steps are and what they do.



Quoted for truth. There is value is knowing what your dependencies are, and a build system is not the same as a dependency management system. I don't understand why people are trying to conflate the two. I suppose it is simply to provide the illusion of a one-size-fits-all-and-solves-all-my-problems solution for devops and pointy-haired managers. At some level of the stack you will have to deal with dependencies and libraries, installations and versions, and no build system can do that for you. The setup and installation is not the same as the build. If, as part of the build, you want to call out to a dependency management tool to ensure all packages are downloaded and installed, fine: but I don't see why it needs to be integrated into a build system itself.

The next big thing would be the tool "doitall" and just set up everything exactly perfectly correct -- eh, it doesn't and cannot exist. /rant

Edit: Please don't think I am disrespecting anyone or knocking anyone's use of these tools in their job, for practical purposes; I am simply ranting my prior frustrations with some of these tools.
4 days ago
I thought Scanner was deprecated?

What is the structure of the file? If you just want the raw bytes I think you could use ByteArrayInputStream.readAllBytes(), perhaps wrapped in a BufferedInputStream.

A suitable structure to read the file into objects in memory depends totally on the structure of the file (unless you just want the raw bytes). I suppose that's why they were using the Scanner.

Eventually you'll have to parse out the contents of the file into a suitable in-memory object structure but until we know the file structure, or even better, the logical structure, its impossible to say.

4 days ago

Campbell Ritchie wrote:

Damon McNeill wrote:. . . a float takes half the space. . . .

On a 64‑bit machine, you might use all 64 bits for floating‑point arithmetic regardless.
You will probably have to have millions upon millions of floating‑point numbers in memory all at once before you notice the difference in memory use.
The default is to use double arithmetic; indeed you either need the terminal f or a cast to turn the arithmetic into a float.
It isn't worth the bother only to lose the already limited precision of double arithmetic.

always use int where a byte would do, no?

As soon as you start doing arithmetic, your byte will be turned into an int anyway, except when using the compound assignment operators. The short and byte datatypes probably also have no real‑life use. The byte[] datatype, on the other hand, is very useful for sending information across networks.



Yes, but in the FPU (as I understand it) all floats, doubles, etc. are actually promoted and stored as 128 bits to avoid loss of precision. Yet this is only temporary, as these are stored in the FPU registers, but when stored in memory (or the local variable stack) they only require the 32 or 64 bits. The same goes for byte vs int. The byte would be converted temporarily to an int in a CPU register for an operation. I guess my point is that yes there is type promotion but that only occurs temporarily within the registers of the machine and does not (at least by my understanding) affect the sizes of these objects in memory, on the stack or on the heap.

Of course, the stack layout of an architecture may just pad everything out to a 64 bit word for efficiency, but in memory, or on disk, there is a valid reason to choose the smallest data type that sufficient for the purpose.

Anyways... all this is off topic for beginning java lol. More like an Assembly language course. I would just add that a lot of the times when you need large arrays of floats (16/32 byte) for a graphics applications, it is sufficient precision and it is faster to upload an array of those guys to the GPU than double precision, where the extra precision is all zeros anyways.
4 days ago
Although this thread had become very technical very fast, including threads and concurrency and such, I would like to provide a simple answer which may not be what the OP was originally asking but may answer the question posed in the title of the post. Maybe someone will find it of some use.

1. The only way to truly ensure avoiding stack overflow using recursion in Java is not to use recursion. This can be done by converting your algorithm to an imperative style using loops instead of recursion.
2. Not all recursive algorithms can be converted to loops, period.
3. If your algorithm is tail-recursive, you can convert to using a loop without using additional space, thereby avoiding the stack overflow issue.
4. Otherwise, you might consider implementing your own stack (in the heap). If you do this, you may still run into the problem of overflowing your own stack (in the heap).
4 days ago

Campbell Ritchie wrote:Welcome to the Ranch

Please avoid floats, unless some other API requires them; they simply lose you precision as against doubles.
Why would you expect that program to print 0?



Sorry to be a nitpick, but what is the rationale for avoiding using float as opposed to double? Depending on the application, you may not require the extra precision of a double, whereas a float takes half the space. So I do not understand the blanket statement to simply avoid float. It's like saying always use int where a byte would do, no?
4 days ago

Tim Holloway wrote:When you see a "no guarantees" warning, it's an indication that you should be treating the functionality in question as a "black box" and not try and design code based on its current implementation.



This seems to line up with the definition of "undefined behavior" in C and C++.

C certainly has many more undefined behaviors than Java. I for one, as we all did (or else we wouldn't be on this forum), appreciate that simplification!
1 month ago

One example, complex tests using || that so obviously meant && and vice versa, another <= in places that obviously should have been >= and vice versa.



Yeah those are the types of errors that are typically encountered IME. Simple errors (akin to typos) once you give some time to think about the logic. Like a bad conversion factor from seconds to milliseconds in a delay/sleep argument that causes tests to run 1000 times as long, lol. Of course that is forgivable if it really is just a typo vs. I don't really know how to convert seconds into milliseconds, understand boolean logic, etc.

I guess this one just weeds out the bottom grass?
1 month ago
It would be nice if there were support for an IndexedIterator<T>, where the IndexedIterator could both provide its value in addition to its position in the sequence. This would seem to obviate the need for the regular C-style indexed for-loop... I don't see why either an array, linked list, or stream based collection could not at least support this, its just a simple counter.
1 month ago