Ken Whitestone

+ Follow
since Jul 22, 2013
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ken Whitestone

Well, I assumed (silly me) that since the Java application was being executed from the "src" directory, then any binaries in that directory would be in it's relative path. Clearly, I was mistaken as I specifically had to define the working directory in Eclipse by going to the run configurations, selecting the arguments tab, and choosing "other" to select the correct directory. Looking back, I could have just edited the command to be relative to what was actually the current working directory instead of "src" (I think I'll do that anyway).

Thanks for your help
5 years ago
I have a shell script that I wrote a while back and I want to try and use what I've learned in Java to translate it into a Java application. The shell script relied in some part on an open source binary that I included with the script. The script would just use relative file paths to point to the binary and all was well. However, I'm having some trouble doing the same thing in Java. I know I can use the following code to execute an external command from within the Java application:

The problem with that is, it seems to require the full path to the command (binary) being used. Every time I try to use a relative file path (./<command>), the program throws and exception and says the file can't be found, even though there is a copy in both the "bin" and "src" directories of that project. Is it possible for me to use a relative file path so I can package the binary with the Java application and still have it work on any compatible system?

[Edit:] I'm using Eclipse, if that matters.
5 years ago

Keith Lynn wrote:I ran your programs and got false on both

Grrr!! I found the problem...

In my original copy of Version 1, I missed the fact that I had accidentally added a semicolon at the end of the if statement:

I've spent hours breaking down every aspect of the code and trying to understand why it wasn't working as it was supposed to. I've never run into that problem before, but it makes perfect sense why it wouldn't work the way I wanted it to.

Thank you for your help. It helped me to identify the problem and probably saved me from more hours spent looking in the wrong direction.

Sorry if I wasted your time.
5 years ago
I have a program written two different ways, and it seems to me that the final output should be exactly the same. I was wondering if someone could help me understand why the two versions aren't "logically" the same.

Version 1:

Version 2:

Version 1 returns true regardless of what values are stored in the "nums[]" array. Version 2, however, works exactly as intended. The two versions seem to evaluate the same way, but this is clearly not the case. Why is that?
5 years ago

Junilu Lacar wrote:Tip: You can use the % operator to check if a number is odd or even. If x % 2 == 0, then x is even, otherwise x is odd.

Yep, I'm already ahead of you...thanks!
5 years ago
Thank you, both, for pointing me in the right direction.

...this works a lot better. It still drops the last character from Strings that have an odd number of characters, but at least this is a step in the right direction.
5 years ago

Ivan Jozsef Balazs wrote:
String s gets longer with every iteration, s.length() increases all the time - how should this loop end?

Ah, yes...I see it now.
5 years ago
I was wondering if someone could help me understand a problem I've run into while trying to get a method to return a string.

I'm working on a exercise that is supposed to return a string made of the first two, third two, fifth two, (and so on, depending on the length of the string), characters of a given string. I have a list of arguments saved and ready to be passed to my main method using the IDE.

Now, regardless of whether or not the code in the altPairs() method will produce the desired output (i.e. kittens -> kien), I need help understanding why there is no output at all, and why it seems as though I have an infinite loop as the program never terminates itself.
5 years ago

Joel Christophel wrote:Your for loop syntax is off. You need to replace the commas with semicolons. The code you posted doesn't even compile for me.

Yeah, that's just a typo...sorry about that.

Joel Christophel wrote:Once you fix that, your first example should work. For example,"cat".substring(2, 3) returns "t" even though there is no index of 3, so you shouldn't run into errors using substring(int, int) the whole time.

Strange. Something else must have been off with my original code. It kept throwing the "StringIndexOutOfBoundsException" and point to that line ( strArray[i] = str.substring(i, i + 1); ) as the culprit. But you're right. I fixed the example code and it works exactly as I though it should. Thank you.

Joel Christophel wrote:Also, the String class has a built in method called toCharArray that converts a String into an array of characters. If you really need an array of Strings, though, you could use toCharArray and then convert that to a String array.

Thank you for this. I imagine it will come in very handy.
5 years ago
I have a string that I want to break up into it's constituent parts and store them in a string array.

For example, if the value of the String variable "str" is "My string", I want the resulting array to be like this:

I'm thinking, in order to do this, I need a loop to iterate over each letter of the string and assign it to the next field of the array, which I imagine would look something like this:

Clearly, that doesn't work and will always throw a "StringIndexOutOfBoundsException" once it gets to the end of the string because you can't add 1 to the end of that field.

I imagine I can do something similar to the following:

...but is there a better way to use substring() and extract a single letter based on the current value of "index", but ensure that I don't run out of bounds in the process?

If there's another way of doing it, that's great. I would love to see it. But I'm mostly concerned with making sure I understand the logic behind substring().
5 years ago
Yes, thank you. I knew there was something wrong because it wouldn't let me add an "else" to that "if" statement, which is what I really needed to begin with. Problem solved.
5 years ago
I'm having trouble with the following code:

For some strange reason, once the expression is no longer true, instead of printing a new line, then resetting the count and continuing with the remaining iterations of the loop, it continues adding 1 to the count throughout the remaining iterations of the loop, but doesn't print "args[i] ". What am I missing?
5 years ago
Oh, I see now. I clearly misunderstood. I didn't realize that the name was being passed as an argument and could therefore be any name.

Apparently, I have to figure how to write a program that accepts arguments from the command line. Thank you for your response.

[Edit:] Scratch that...I've got it now. Thanks again.
5 years ago
Regarding: Assignment Java-1b (Hundred)

Is the purpose of this exercise only to keep from splitting up the words, or should the format match that of the example? What I mean is, the example shown has the word "Gertrude" printed eight times per line, with four occurrences on the last line. But my example has 9 occurrences per line (11 lines), with one occurrence on the last line, on an 80 character width console, without splitting, and using no more than 5 lines of code in the main method.

Is this acceptable or am I supposed to match the formatting?
5 years ago