aspose file tools*
The moose likes Beginning Java and the fly likes print n numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print n numbers" Watch "print n numbers" New topic
Author

print n numbers

Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Hi,
I am working in this program that is suppoused to print the firsts numbers from a series of numbers:
1,5,9,13,17...etc..
So, the numbers are jumping from 4 to 4.
for example:
if the user enters "1",the program prints 1.
if the user enters "2", the program prints 1 and 5.
and so on.

how can i program this?

thanks for any response...
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30910
    
158

Jack,
What do you have so far? A good approach to starting is to try solving a similar, but simpler problem. For example, can you have the user enter a number and print it out? Can you print out the numbers from 1 to "n" without skipping?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It often helps to describe the process in English before trying to code in Java. For example, you might say something like...
  • Output a number.
  • Check to see if you're done (if you've output n numbers).
  • If you're done, then stop.
  • If you're not done, then add 4 (or whatever interval you're using) to the previous output.
  • Repeat.
  • Once you have the process (algorithm) described, then you can start writing code to match the steps.

    Give this a try, and let us know what you come up with.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    colin shuker
    Ranch Hand

    Joined: Apr 11, 2005
    Posts: 744
    There are several ways to solve this, and I won't tell you because it is forbidden in java ranch.

    But heres some tips
    You will need a way to enter how many numbers you want.
    JOptionPane is probably best, but you could just do:
    int num=2;
    just for testing.

    Then you are going to need a loop.
    You could use while loop or for loop, I recommend for loop here.

    Next, I guess you need a formula, well the n'th term will be 4n-3 if n is starting at 1, but you might prefer to start at 0 which is the traditional way to start a for loop.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    The hint for the "for loop" is probably just what the original poster needed. I did a quick Google for "java for loop" and hit THIS SITE. Try the example on this page and make sure you know how it works. Follow the "next" link to the "while loop" tip.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Jack Bolton
    Ranch Hand

    Joined: May 08, 2006
    Posts: 65
    This is what I've done so far, but,
    something is missing: this program is suppoused to print the firsts numbers from a series of numbers:
    1,5,9,13,17...etc..(as I said before),
    so, if the user enters, for example: "3", the program should print the first "3" numbers that are: 1,5,9.
    but,
    Right now, if the user enters "3", the program only prints: "1".
    what can i do to solve this?
    thanks for any response...
    the code:

    [ May 15, 2007: Message edited by: Jack Bolton ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    Whoever said the problem is in the "for" loop was correct. Go through your "for" loop with a debugger, or use a pencil and say, "When I start the number i is 1, then i becomes . . . " until you work out exactly what is happening in the "for" loop.
    colin shuker
    Ranch Hand

    Joined: Apr 11, 2005
    Posts: 744
    Thats right, take a look at your for loop.
    There are many ways to solve this, seems you are not going with the formula approach I mentioned a few posts ago, but with a continual incrementing approach.
    But if you use an incrementing approach, then you don't want to be incrementing the loop variable, but you will need some other variable, perhaps initialized to 1 to start with, then you can increment by 4 each time you go through the loop.

    Your code seems a little ironic, in that you are using complicated things like BufferedReader, and InputStreamReader, and that you don't quite grasp how to solve this loop, or perhaps thats just how you were told to grab data.

    Good luck with it anyway.
    Jenn Person
    Ranch Hand

    Joined: Jan 16, 2005
    Posts: 89
    Originally posted by Jack Bolton:



    Check your for loop. Trace through each loop, paying careful attention to the value of your i variable as you go along, and take a look at your loop condition as well. You'll see why it only prints once when you enter 3.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: print n numbers