File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print n numbers

 
Jack Bolton
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    colin shuker
    Ranch Hand
    Posts: 750
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8791
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Jack Bolton
    Ranch Hand
    Posts: 65
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47232
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 750
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 89
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic