This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!

Daniel Andres

Ranch Hand
+ Follow
since May 15, 2015
Daniel likes ...
AngularJS C++ Chrome Eclipse IDE Oracle Windows
Cows and Likes
Cows
Total received
3
In last 30 days
0
Total given
0
Likes
Total received
10
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Daniel Andres

If I have in a file some random numbers, let's say 61254 and I want to read each number as an int how can I do that?



Right now, this program outputs:

Index 0: 31416
Index 1: 0
Index 2: 0
Index 3: 0
Index 4: 0
Index 5: 0
Index 6: 0
Index 7: 0
Index 8: 0
Index 9: 0


But I want to have:

Index 0: 3
Index 1: 1
Index 2: 4
Index 3: 1
Index 4: 6
Index 5: 0
Index 6: 0
Index 7: 0
Index 8: 0
Index 9: 0


I have done a few homework programs involving reading from a file but I am not too comfortable with it yet. I know that if the numbers have space in the text file 6 1 2 5 4 then it will work but I don't want that. This is not homework BTW 
Don't worry about the fact that my class is named Main or that I am working in Main instead of using a different class. I like to create programs in my spare time and since I ran into this issue, I created a generic class Main just for the purpose of my question.
1 year ago
Agree. I meant to say that instead of posting in beginners java it was supposed to be under general.
Thank you for posting your code @Stephan van Hulst. I will test it myself.

@Campbell I meant to post this under general java.
Wow, I can't really follow up. I am not familiar with Maps or many of those terms you have used. Now that I'm on spring break I'll read ahead on the chapter on Maps ADT. What do you mean by

which should give you an indication of the amount of memory it uses

When we get the final results for n, is that an indication of memory use? If you have already explained it throughout the code sorry for asking about it. We had to program Ackermann for my CSC class computing the value, doing tracing like you did and also doing table look-up that stores already found values in a 2D array that way before we go into the if statements first we see if the previous recursive call is in the table already. If it is, then we can return it thus optimizing the program. But I am not that advanced yet in Java which is why I am lost in the second code you posted.

Thanks for the amazing detailed explanation. I'll get back to it when I'm ready!

For those unfamiliar with Ackerman watch this video on YouTube; Professor Brailsford explains what it is. There were certain things in the computing world that were so big that just had to be done recursively. Some people argued that they can just be done with for loops instead. Ackermann, a research student of mathematician David Hilbert was apparently the first to realize this and wondered if there actually was a problem that could only be done recursively. Very insightful video.


1 year ago
I apologize. I wrongfully assumed everyone would know what Ackermann. I just got caught up in the moment that I did not even think of explaining what it is. I will post a link and more details about it after class.

Thanks
1 year ago
How much RAM do we need to compute this one? I watched a video on youtube from computerphile where a professor explained how he used a very old computer and while it took him a long time to get the answer there was no overflow problems. If RAM plays a very important role, which I believe it does since we are abusing the heck out of the stack, someone please tell me how much so that I am able to run it on my computer
1 year ago
Junilu Lacar, thanks for the dedication and time you put into this forum. All of the accolades you have received are very well deserved.

Your detailed responses have helped me think differently when I code. Kudos!!
1 year ago
Ohhh never mind! It is more elaborate than that. I want to program this now... you're on the right track with taking the square root of the number and then dividing by the prime numbers but then again.. If you're dealing with a big number the square root could be a 3 digit number. How to ensure that all the preceding numbers are prime numbers???

Sneaky little program

1 year ago
Well at least you didn't get prime and odd confused :|

You could use modulus for instance to determine if the number is divisible by 2 then, with the exception of the number 2 itself, you'll know it is not a prime number.
This program is fun to code. By doing a few calculations with modulus (or regular division for that matter) it is very easy to determine the answer. I really don't know how close of a hint we can give in here since we can't provide solutions. But if you know that a prime number can only be divided by 1 and itself then it shouldn't take you too long to figure it out!

1 year ago
My apologies. I read odd and not prime.
1 year ago
If you know that modulus gives the remainder of a division then it is really easy from there. It is the same process as if you were doing math on a piece of paper. What determines a number being odd? Likewise, when is it an even number?

1 year ago
I think you are simply too obsessed with this program that you want to get the solution immediately instead of restarting. Again, go back and read all the helpful comments already posted. Between work and school I have been following this and other threads( as I always do because this ranch has become my second home) and I always learn from just reading the advices from experienced programmers and engraving them in my head for a later use. I have many threads bookmarked/watched and books that have been suggested. As well as links.

Any advice I could give you at this point may have probably already been given to you throughout this thread.
You mentioned earlier, page 2 to be exact, that you have been working for over a month in this. Someone asked if this was a homework assignment and you said no. Are you learning Java on your own then? Have you ever taken a programming class? If not, then it's not impossible to learn but jeez that would be very hard in my opinion. Unless you are James gosling perhaps

Have you at least finished an introductory book? If you're just an 'aficionado' and programming will not be your career then I think that may be the reason why you immediately post code and ask for advice time after time.
1 year ago
The people that have commented on here to help have a lot of patience as you stated Junilu! This thread should not have gotten so long. 5 pages for a program that is not too complex.
I say this as humble as I can be.
For a program like this I don't believe you are a beginner just starting out programming. You must have had exercises involving loops and using methods to distribute the workload and be able to easily debug in case something fails.
But I don't see that in your way of coding.

To be a great programmer you really need to love to program and read carefully each chapter of the book you may be currently studying from (everyone needs to start somewhere), not just reading but analyzing each line and code and trying out several exercises to see for yourself why the code behaves like it does. As you progress you won't need to analyze each line anymore but if you skip this part then as things get complicated you don't have the basics down. Loving to program is  not just sitting down and coding. It's the behind the scenes and what's happening in your brain as you encounter bugs:

But why? Why is it doing this? If its laid down on paper and you have a clear visualization of what it needs to do then what is the part that is missing in the code? Use sysouts, scan.next() or whatever would help to dissect each part of the code and identify the working code until you get to the part that's broken. Junilu posted an excellent bite size approach of each part that you should be doing. Yet instead of digesting that and put effort from your part you posted complaining about the scanner class when his code wouldn't work on your program.
My advice would be as others have already pointed out. Go back to the beginning and look at all the helpful posts they have given you. Although with the last solutions provided should be more than enough!
1 year ago
Sorry can't help out more I have to go to work. I'm sure someone will guide you towards enlightenment soon enough.
I wish we were able to edit our own posts.
1 year ago