Here you can see that 28 is the first triangle number to have over five divisors.
I need to write a program that takes a single argument on the command line. This argument must be a file name, which contains the input data. The program should output to standard out the value of the first triangle number to have a number of divisors over the value given in the input file.
I need help. Code is needed and if you can explain with an algorithm then it would be great.
One of the first things to learn is that programming jobs like this can be broken down into individual, discrete tasks. As Rob has implied, you need to figure out how to do each part by itself first, before you try and solve the whole problem.
So...work on each part individually. Write a program that opens a file, reads the value, and converts it to an int. PROVE to yourself that it works by printing out that int.
Write another program that generates triangle numbers. For starters, have it print the first 10 or so. Write it in such a way that you can change how many to generate rather easily...possibly by having some kind of condition like "while i've not generated 10, generate the next". then later, you can change that to 20, or 100, or even 'while current number has less than 5 factors' and then 'while current number has less than X factors'...and perhaps X could be acquired from somewhere...like user input, or even from a file you read...
Write another program that finds all the factors of a number, and possibly counts them. Remember that you need to count DISTINCT factors...For example, 25 only has 3 factors: 1, 5, and 25. You don't want to count 5 twice. This may or may not be a problem, depending on your algorithm for finding factors.
Once you have all these pieces, you can start wiring them together.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
The first thing to do is to identify the line of code which threw the exception. At the end of your exception's text you see the number "13"... that means the error occurred at line 13. Which is this line, right?
So you can't access element 0 of the "args" array because it doesn't have any elements at all. Which would mean you forgot to pass any parameters when you ran the class from the command line.
I request the moderators not to delete the code until the thread starter posts some reply. this is beginning java section. lnfact we learn a lot by reading code, writing a lot of code. Be it anyone. beginner or an expert.
Harsha Smith wrote:I request the moderators not to delete the code until the thread starter posts some reply. this is beginning java section. lnfact we learn a lot by reading code, writing a lot of code. Be it anyone. beginner or an expert.
I disagree. We learn little by reading code. we learn by writing our own, not by being given it.
I was busy at the time, otherwise I would have deleted it.
Biman, I think you should take Fred's advice to heart. It looks as though you are approaching this like a procedural program. Java is not procedural. Use classes and methods to break your problem into manageable chunks.