Adam Nace

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

Recent posts by Adam Nace

We're going to need some more context than that, if we're to know what's gone wrong. Here's what I suggest. Executed the following command:



in cygwin, and paste the results, here, and we might be able to tell you what's wrong.


In general, in cygwin, you want to put something like this into your bashrc:



Note, that is a colon ( not a semicolon ( . If you're used to working in DOS (or the Windows Command Prompt), this might surprise you.

Also, note that you might have to use the cygdirve path, from cygwin:



If you choose not to use your bashrc (or rc file for whatever shell you're using), you could do this:



Anyway, try echoing out your path, and find out what it looks like, and if it looks the way you expect it to.

- Adam
10 years ago
It gives you zero because you are performing integer division. When the operand on the left of the division AND the operand on the right of the division are both integers, java will perform integer division, EVEN if the result is being assigned to a floating point variable.

Integer division means that it does the division, and then truncates the decimal places (i.e. removes the entire fractional part of the number), so that the result is still an integer.

Sooooo...

If you want to perform floating point division, you need to convert at least one of the operands to a floating point number. The 100 looks like a good place to start.

Change the 100 (which is the integer 100) to 100.0 (which is a double precision floating point number 100), and your code should work correctly.

- Adam
10 years ago
Not necessarily. You still need to make sure its using Java 1.5.

The more relevent question is this: Will your teacher accept a program written in 1.5. If the computers at your school are only equipped with version 1.4, then your teacher may require you to use version 1.4.

However, there are still intelligent ways to strip out the zeros using 1.4.

The code you had before, although not terribly efficient (in fact, terribly inefficient), would have worked. BUT, you need to convert the number to a string FIRST, then check if the character at each position of the string is equal to '0'.

However, notice that the substring method is SLOW. What you might want to consider doing instead is to use an integer variable to store the last location that is not a zero (same if condition as you're using, just instead of calling substring, assign the value of g to another variable).

Then, when you've finished the loop, you make a single substring call using the value stored in the variable. This will be much more efficient.

- Adam
10 years ago
The stripTrailingZeros() method has only been in java since Java 5. Do you know what version of java you are working with, in BlueJ?

- Adam
10 years ago
What you have to watch out for is that the | has a special meaning in a regex, so you need to escape it. Furthermore, the \ has a special meaning in java, so you have to escape it. So what you should get is this:

10 years ago
Looks like it ought to display something. Are you sure its the applet thats the problem, or is it possible that the applet isn't loading at all.

i.e., is the button being displayed?

- Adam
10 years ago
With regard to your second question, I would wait to see if the fix to your first problem gets things working for you. I'm not entirely sure that your counter doesn't reset itself. It may have to do with forgetting to deal with capitals.

- Adam
10 years ago
For your first problem, there are a couple of solutions, but you're going to have to be intelligent about your use of capitals.

The first option is to check to see if the character code is larger than 'z'. If it is, subtract 26 from it. BUT notice that Z + B doesn't give you a letter, but it also isn't greater than 'z'.

The second option is to use the modulo operation. Subtract 'A' from both the input and the keyword, so that all of the characters are between 0 and 25. Then, when you get your result, do this: result = result % 26. Then, add 'A' back into the picture. This will give you the wrap around that you want. You need to be careful about capitals, again, here, though. If you have a capital letter you want to subtract and re-add 'A', if you have a lowercase letter, you want to subtract and re-add 'a'.

- Adam
10 years ago
Basically, you want to return a list. You want something like this:



I leave it to you to define the matches() method.

- Adam
10 years ago
I think I would recommend making an enum, and using an enum map. Although 50 is awefully big for an enum, it'll make your code cleaner, because if you find a name isn't a state, it'll get caught by the valueOf method. This should be relatively safe, because the fifty states aren't likely to change anytime soon.

- Adam
10 years ago
Nothing's jumping out at me that looks obviously wrong. What behavior are you seeing?

- Adam
10 years ago
Personally, I'm more in favor of variable.equals("literal"), because it seems more logical to me when I read it.

The intent is rarely to see if two strings are the same, but rather, to see if the variable holds some specific significant value (if that makes sense to any of you). To do that, we check to see if they are the same.

But to me, when I see "literal".equals(value), I think to myself, why am I checking the value of "literal". I know its value. My intention is to check the value of variable. So I prefer to see variable to the left, because it reads as 'if variable is equal to "literal"', which is what I want.

Having said that, "literal".equals(value) means we don't have to null check. Some people think this is a good thing. Personally, I'd rather see the null check, because it tells me that it is possible that this variable is null. If I don't see the null check, I don't know if there's a possibility that the variable is null.

- Adam
10 years ago

Originally posted by Tony Smith:

Parent p = new Parent();
Child c = new Child();

c = (Child)p;



Note that your code, as written, is not valid. We know that the run-time type of p is Parent, not Child (because we can see that you instantiated it that way). Casting is appropriate where the run-time type is actually a child, and it is not, here.

Also, note that you are creating a new Child object and assigning it to c, only to be overwritten in the next line. Are you sure you understand the difference between defining a variable, and initializing it?

- Adam
10 years ago
You want to start looking into SWING. If you go to Google, and type in SWING Tutorial, you will get a link for the SWING tutorial hosted by Sun Microsystems. This gives you more than enough info to write a good GUI application.

- Adam
10 years ago

Originally posted by marc weber:
The enhanced for statement ("for-each") can be used either with an array or with any container that implements the Iterable interface. (See JLS 14.14.2 The enhanced for statement.)



Interestingly, Iterator does not implement Iterable. So if you have an iterator, you can't use it in a for-each loop.

- Adam
10 years ago