James Allen A.

Greenhorn
+ Follow
since Mar 10, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by James Allen A.

Thanks for the explanation Wayan and Fred. Very clear now.
11 years ago
I'm doing an exercise out of the Head First Java book and it decides to take the users input as a String rather than an Int and parse it later. First, is this a general convention? If so, why?
11 years ago
I'm doing the same exercise. These 'Pool Puzzles' are pretty tough for me. I've had to cheat and look at the end of the chapter twice now. I understand now how it works but even entering the solutions code doesn't get me a working program. This is exactly what the solution is (pretty confident):

It compiles fine but when I go to run it, it throws

I see that your code was changed throughout your posts on this thread. Was this to fix this issue?
11 years ago
Scratch that. I had a misunderstanding of what ArrayList.size() returns. My mistake was that I called on a position after the place where the final position in the array occurs. Hence the "out of range" exception. Got it now.
11 years ago
The code is pretty messy at this point so my apologies, but I'm running into an IndexOutOfBounds Exception:

Here's my code. It references line 65 where I'm trying to create a loop to go through each position in ArrayList coins.

11 years ago
Just felt like I had my hands full enough with the rest of the code. I suppose I could give it a shot. Maybe integers aren't the best choice. I'm writing a coin calculation program and hadn't considered that the user would enter a large number for a coin but I suppose I should account for that.
11 years ago
I suppose my question is broader than for just integers, but I really mean for all numbers in the ArrayList. Is there a method for returning the product for all numbers within the ArrayList? If my ArrayList values are [2, 5, 10] I would like it to return 100. I'm not sure it will solve the road block I've hit in my program but I want to give it a crack.
11 years ago
I see. I must have tried CTRL + O before I verified they were removed. They were probably minimized then I inadvertently removed them. I just tried to reproduce my problem and couldn't do it so all is well. Thanks!
I found other related threads but none that specifically address removed unused imports. I added several unused imports that I would like to later use. After saving and compiling my project, I find that some that are used are consolidated to one line, while others that aren't used are removed completely. Can I tell eclipse to never remove imports?
Ahh I see. I didn't expect it to be that simple of a fix. I, it looks like incorrectly, remember reading that Boolean values could be true, false, yes, no, y, n, etc..

I changed my variable to a String and now use the .equals() function to ask the user for more input. I get the feeling this isn't the best way though.

11 years ago
I think I matched up all my data types correctly but I'm getting the following error.

Here is my code:

I can't figure this out.
11 years ago
Okay I re-wrote the program using boolean variables like Winston suggested. Is this what you're suggesting? It seems to work.



Perhaps because I'm a beginner -- or because I'm me -- but I'm not seeing how this clearly translates to the objective of my challenge. Maybe this will become more second nature to me as I understand how to program in Java more efficiently?
11 years ago
Okay so I pulled out a notebook and you're right, much easier to make sense of things. I was struggling with how to discern whether a number is prime or not while keeping the for loop going to try all divisors. I'm not sure if this is the most efficient way, but I created a counter variable for number of divisors and then added my if statement after the for loop to print the prime number as long as numDivisors == 0. This seems to print the desired numbers.


1
I'm interpreting the wording of the challenge to mean "print all multiples of 3 and all multiples of 8, omit numbers that are multiples of 3 and 8" I believe the code does this. Adding: was an experiment but it seems to work. I don't totally understand why though. Is it because the || operator is exclusive from the && operator? Does that make sense?
11 years ago
So I just did the Sq. Root of 15 on a calculator and I get ~3.8. Then "(int)" forces it to the integer 3. Isn't this the optimal way to test for primes? Aside from using the isPrime function? I have yet to use this function because I saw this method and wanted to attempt this first.

Good point about the order of my checks. Was caught on this prime check and didn't see that.

As for the limiting for loop (not sure the technical name for this), I just brought it down to 100 to minimize the output while test running it.

As for being divisible by 3 AND 8, I don't the wording of the challenge suggests that, saying

Are either: Prime, or Multiples of 3, or 8, but not both 3 and 8, less than 2500

.
11 years ago
I'm attempting to create a program that does the following:

1. Use loops to complete the tasks below
2. Print out a list of all the numbers that meet the following criteria, one item per line.
3. Are either: Prime, or Multiples of 3, or 8, but not both 3 and 8, less than 2500umber

My code prints the multiples correctly but prints a bunch of erroneous prime numbers.

Here's my code:


My guess is that the problem is with my nested 'if' statement. I was reassured by finding an example online that looks a lot like the one I came up with, but mine doesn't seem to be working. Instead my result is stuff like this:

11 years ago