Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Looping Question

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Looping Question

    Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

    For example, upByThrees(4) should print:
    1
    4
    7
    10



    My code is:
    int N;
    N = 1;
    while (N < n)
    {
    System.out.println(N);
    N = N+3;
    }



    How do you get it to print out x amount of numbers?


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Looping Question

    Writing x ammount of numbers is easy, just line up commands. What you want to do it write 'n' x ammount of times. Its a good idea to always think the way you want your program to run, helps a lot.

    A good loop to use for these kinds of situations is the for-loop. The for loop can be used to do something "while this variable passes this check, else do this". For example.

    int x = 5;
    for(int i = 0; i < x; i++){
        System.out.println(i);
    }

    every time this is run, x will be printed, then i will become i = i + 1 (i++ does that) and then if i < x, it will run again.

    Basically, the print for this should be:


    0
    1
    2
    3
    4

  3. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Looping Question

    Quote Originally Posted by miss confused View Post
    Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

    For example, upByThrees(4) should print:
    1
    4
    7
    10



    My code is:
    int N;
    N = 1;
    while (N < n)
    {
    System.out.println(N);
    N = N+3;
    }



    How do you get it to print out x amount of numbers?
    OK what are you being asked to do? Where are you setting n and what determines it? upByThrees(4) What does this tell you? Is that your whole code? Why would you want to get it to print out x number of times where is there any mention of x?

    A few quick tips though don't call your only 2 variables n and N it looks bad and you could end up confusing yourself. Also N += 3 will do the same as N = N + 3

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question

    import java.io.*; 
    public class Test 
    {
    	public static void main(String[] args)
    	{
    		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    		PrintWriter stdOut = new PrintWriter(System.out);
    		stdOut.write("Enter n : ");
    		stdOut.flush();
     
    		try
    		{
    			int n = Integer.parseInt(stdIn.readLine());
     
    			//This part (the for loop) is what should be most interesting to you
    			for(int i=1;i<n*3;i+=3)
    				System.out.println(i);
    		}
    		catch(IOException e)
    		{
    			System.out.println("Invalid");
    		}
    		finally
    		{
    			try
    			{
    				stdIn.close();
    				stdOut.close();
    			}
    			catch(IOException e)
    			{
    				System.out.println("Cannot close open streams !");
    			}
    		}
    	}
    }
    Last edited by ArunUOM; June 30th, 2010 at 07:05 AM.
    Programmer - an organism that turns coffee into software.

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question

    Quote Originally Posted by Faz View Post
    OK what are you being asked to do? Where are you setting n and what determines it? upByThrees(4) What does this tell you? Is that your whole code? Why would you want to get it to print out x number of times where is there any mention of x?

    A few quick tips though don't call your only 2 variables n and N it looks bad and you could end up confusing yourself. Also N += 3 will do the same as N = N + 3

    I'm being asked to print out in lets just say upByThree(4) four of the numbers in the following sequence of my code. With my code, I can either write that code up to a certain number or have it less than n (which I understand is more confusing, but I can change that later).

    The code:
    public void upByThrees(int n) {

    is already given by the problem. The solution is proven right or wrong by inserting a number to variable n.

    So with my equation:
    int N;
    N = 1;
    while (N < n)
    {
      System.out.println(N);
      N += 3;
    }

    Feedback:

    The input value, n:
    6

    Your code produced:
    1
    4

    The correct solution produced:
    1
    4
    7
    10
    13
    16

  6. #6
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Looping Question

    The reason you only got 1 4 is because you are setting the loop to run until N > n while what you want to to is run it n times I would recommend you use a for loop.


    for(int i = 1; i <= n; i++)
    That will run n times and you just need to put the code in there to do what needs to be done(increment by 3 and print it out)

    For loops are the way to go when you know how many times you want it to run(while you don't exactly know here you the programme will once n is set so it will work).

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping Question

    for(int i=1; i<=n; i++){
    prints out n amount of

    for(int i=1; i<=n; i+=3){
    prints out until n

    for(int i=1; i<=n; i++){
      System.out.println(i);
      i+=3;
      System.out.println(i);
    }
    prints out something skewed


    i'm just finding everything but the right solution.

  8. #8
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Looping Question

    Did I say print out i?

  9. #9
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Looping Question

    Quote Originally Posted by miss confused View Post
    for(int i=1; i<=n; i++){
    prints out n amount of

    for(int i=1; i<=n; i+=3){
    prints out until n

    for(int i=1; i<=n; i++){
      System.out.println(i);
      i+=3;
      System.out.println(i);
    }
    prints out something skewed


    i'm just finding everything but the right solution.
    Let me hazard a guess as to what they do when n = 6 the first prints 1,2,3,4,5,6 the second prints 1,4 the third prints1,4,5,8 or something like that i is just an integer to keep track not the actuall number. I repeat what are you trying to do? Print out the first n numbers of a sequence not print out the numbers up to n as you keep trying to do.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Looping Question

    Quote Originally Posted by miss confused View Post
    Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

    For example, upByThrees(4) should print:
    1
    4
    7
    10





    My code is:
    int N;
    N = 1;
    while (N < n)
    {
    System.out.println(N);
    N = N+3;
    }



    How do you get it to print out x amount of numbers?
    is upByThree in the same class or in a separate class?

    If same class, put it after main method like this:

    put int x = 0 in main method.
    public static int upByThree( int x)
    {
    x = console.nextInt();
    return x;
    }

    For x times, you, in main method:

    x = upByThree(x);

    int N = 0;
    int otherVariable = 1;

    while (N > x)
    {
    System.out.println(otherVariable);
    N = N +1;
    otherVariable = otherVariable + 3;
    }

    If in a separate class have upByThree but have it take no parameters as you can have it defined in the method. I think you need to have a this if you the same name as a private variable, or something along those lines.

    Then in the main method in your other class:

    separateClass referenceVariable = new seperateClass();

    int x = referenceVariable.upByThreei();

    then do as shown before with while loop.

Similar Threads

  1. Looping object creation
    By Charlie in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2010, 11:12 AM
  2. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  3. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  4. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM