Kevin Simonson

Ranch Hand
+ Follow
since Oct 22, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Kevin Simonson

Class (BigDecimal) doesn't have a square root function, so I wrote Java program (Sr) to calculate the square root of a (BigDecimal) using Newton's Method.

If I run this with operand 4 I get:

Variable (potRoot) starts out as (1), and then (qtnt) and (sum) each get calculated correctly. But when I take the average of (1) and (4) to put in (newRoot), that should be (2.5). Why is this program showing that (newRoot) is (3)? Is it rounding (2.5) up to an integer before displaying it? Is there some way to keep it from rounding it to an integer value, so that it just displays (2.5)? If my variables were (double)s, I'm pretty sure (newRoot) would end up being (2.5), so when I see it as always an integer value that's really confusing me.
5 months ago

Ron McLeod wrote:Other than renaming some variables, I ran your code as-is, and it seemed to work as your wanted:

Windows 10 and Java 17.0.7



Ron, I just changed the name of your class from (MyDir) to (McLeod), giving me:

But I get the same results:

My command "j8" is just an alias for "\Langs\Java\8u40-windows-x64\bin\java".
8 months ago
I'm working on a computer with operating system Windows 10 Enterprise. On my D: drive in directory "Bug" I have two files, "Oops.Txt" and "Qf.Txt", of lengths 6 and 49 respectively. When I type in "D:", it takes me directly to that "\Bug" directory. (In other words, when I was last on my D: drive I was in that directory.) When I type "dir D:" I get a listing of those two files, and when I type in "dir D:\Bug" I get an identical listing of those two files.

Now I've written file "MyDir.java" which consists of:

When I run it I get:

Why am I getting zero lengths for those two files when I execute "j8 MyDir D:"? It's getting the two files that are in my "Bug" directory. Why is it getting the two sizes wrong? Is there a way to fix my code so that I can execute "j8 MyDir D:" and get the correct results?
8 months ago

Carey Brown wrote:


Thanks, Carey! This worked just fine.
1 year ago
I've got two ".pdf" files that are precisely the same size. I suspect they may be identical, but I'm not exactly sure. If you have two files of the same format, and you're not exactly sure what the format is, so the files could have any possible binary values in an arbitrarily large number of bytes, is there a way to open each of the files and compare all the bytes one by one, to find out if the files are identical or not?
1 year ago

Rob Camick wrote:

When you do the rotate the string gets drawn down 250 and to the left 250, so effectively it is painted off the panel.


Thanks! I got my code working the way I like it. I really appreciate the help.
2 years ago

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Actually, let me make it easier. I tried:

And then I executed "java Ritchie", and got a window with a white strip on top with an icon and "Ritchie" on the left, and then underneath it I got an apparently 500 pixel by 500 pixel square that was completely gray; there's no trace of a green "Campbell is brilliant" anywhere near the center of the square where it should have appeared. Once again, what am I doing wrong?
2 years ago

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Campbell, using what you said I wrote:

Then when I ran it with

I got a large (JFrame) window with "DsgRot Cube_15_10.0" in the upper left corner, but nothing actually appeared in the window; it's all completely white. Any idea what I'm doing wrong?
2 years ago

Carey Brown wrote:Kevin, can you provide a screen snapshot as to how you expect it to look. From your description I'm guessing (?) you want:
A
B
C

and not just ABC rotated 90 degrees.


I don't know how to provide a screen snapshot; I was hoping on using the Java interpreter to display it, and I can't figure out how to do that; I don't know any other way to display it. But in response to your guess, I actually do not want
A
B
C
instead I do want ABC rotated 90 degrees.
2 years ago
If I extend (JPanel) and in my (paintComponent()) method I call (drawString()) on a (Graphics) object, I can pass in a (String) object as my first parameter and cause that (String) object to be printed horizontally in my (JPanel) window. Is there a way to print that (String) object vertically instead of horizontally? I need to switch each of the characters in the (String) object so that they occur one on top of the other instead of horizontally next to each other, and I also need to rotate each of the characters ninety degrees. Does anybody know how to do that?
2 years ago
If I do a Google search on "java escape sequences", I get a number of pages like "https://www.golinuxcloud.com/escape-sequence-in-java" that seem to indicate that there are only eight escape sequences, namely "\t", "\b", "\n", "\r", "\f", "\'", "\"", and "\\". But when I wrote the following code:

it became obvious that I can also have "\uxxxx", where (xxxx) are four hexadecimal digits, and I can also have "\0", "\01", "\012", and "\7"; so the list on that website appears to be incomplete. Can anyone tell me where I can find the complete rules on which escape sequences I can use?
2 years ago
I have successfully written a Java program using a {javax.sound.midi.Sequencer} object to make musical sounds. What I want to do now is make a group of sounds (in this case three), and let the user select which of the (three) sounds that user wants to hear, and then have the program play that particular sound. The code I tried is:

When I run this I get:

Why is it telling me this? I called {open()} on each of the three {Sequencer} objects. I don't understand why it's telling me that the sequencer is not open. Can anyone tell me what I'm doing wrong?
2 years ago
This somewhat artificial example shows the problem I'm having. I'd like to throw my custom-made
exception with a string and then have that string returned when I call {getMessage()} in my
exception handler. Here's my code:

When I go to compile it I get:

Does anybody know what I'm doing wrong?
2 years ago

Carey Brown wrote:Have you tried closing the sequencer?


I get the warning before the user provides any input, and I use the sequencer if the user enters an empty string. So by the time I would be able to close the sequencer, I've already gotten the warning.
3 years ago

Ron McLeod wrote:Sounds like this issue: StackOverflow: Java could not open/create prefs error.  The work-around seems to be either run the application once as a user with Administrator rights, or manually create the registry key.


I followed the link you provided. It said: "I was faced this issue on Windows 10 64-bit and was able to resolve the problem by manually creating the following registry key. / HKEY_LOCAL_MACHINE\Software\JavaSoft\Prefs / Hope this helps for the Windows users." I'm not entirely sure what that means. What do I have to do to manually create that key?
3 years ago