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 7 of 7

Thread: Do while loop help

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Do while loop help

    Well basically what I'm trying to do is write a program that will prompt the user to enter an integer, then take that number and Square it as well as print it out and continue in this loop until 0 is entered. Here is what I have done and failed to try to do so far, thanks for the help guys.

    #import java.util.Scanner;
    public class twelve{
    public static void main(String[] args){

    Scanner in = new Scanner(System.in);
    int i = in.nextInt;
    do {
    if (i == 0) System.out.print("0 cannot be used ");
    else {
    System.out.print(i * i + ",");
    }

    } while( i != 0);
    System.out.print(i*i + ",");
    }
    }


    Problems im really struggling with most is how to get the program to square the number and then at this point im not even sure if this will run back through the loop again. Thanks for the help guys 3rd week of programming ><


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop help

    Another thing was I am not entirely sure on the formatting of Do while loops as well so any and all help appreciated to get me going on the right direction.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Do while loop help

    Quote Originally Posted by rbonner19 View Post
    Well basically what I'm trying to do is write a program that will prompt the user to enter an integer, then take that number and Square it as well as print it out and continue in this loop until 0 is entered. Here is what I have done and failed to try to do so far, thanks for the help guys.

    #import java.util.Scanner;
    public class twelve{
    public static void main(String[] args){

    Scanner in = new Scanner(System.in);
    int i = in.nextInt;
    do {
    if (i == 0) System.out.print("0 cannot be used ");
    else {
    System.out.print(i * i + ",");
    }

    } while( i != 0);
    System.out.print(i*i + ",");
    }
    }


    Problems im really struggling with most is how to get the program to square the number and then at this point im not even sure if this will run back through the loop again. Thanks for the help guys 3rd week of programming ><
    I am not sure if I understand your do-while loop correctly. The loop is "repeated" only if the the first read int i = in.nextInt; returned a value unequal 0.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop help

    Yea as long as 0 isnt the inputted answer/number then the loop should keep going.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop help

    import java.util.Scanner;
    public class twelve{
    public static void main(String[] args){
    int data;

    Scanner in = new Scanner(System.in);
    do {
    System.out.print(" Enter an integer value ( program exits if it is 0); ");
    data = input.nextInt();
    }
    while (data != 0);
    data *=data;
    System.out.print(data + ",");
    }
    }
    I'm continually working on it, this is where I'm at now and yea it all diff etc but its a working progress anythign seen will be greatly appreciated.

  6. #6
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Do while loop help

    Correct me if I am wrong, but you want the loop to continue until the user enters 0 and you want each iteration to prompt the user for an input, square it and display it? If so, this would be more to what you are looking for:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
     
    public class twelve
    {
         public static void main(String[] args)
         {
              BufferedReader brInput = new BufferedReader(new InputStreamReader(System.in));
              int iInput = 1;
     
              do
              {
                   System.out.print("Enter a number: ");
                   try
                   {
                       iInput = Integer.valueOf(brInput.readLine());                   
                   }
                   catch(Exception e)
                   {
                       System.err.println(e.getMessage());
                   }
                   if(iInput == 0)
                   {
                       System.out.println("0 cannot be used.");
                   }
                   else
                   {
                       System.out.println((iInput * iInput));
                   }
              }while(iInput != 0);
         }
    }
    Last edited by ShadeDarkan; July 8th, 2012 at 06:44 AM.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Do while loop help

    Your logic was correct but you have made an error in your program.
    The statement,i = in.nextInt();should be inside the do-while loop.
    import java.util.Scanner;
     
    public class twelve{
    public static void main(String[] args){
     
    Scanner in = new Scanner(System.in);
    int i;
    do {
    		i = in.nextInt();
    		if (i == 0)
    		{
     
    			System.out.print("0 cannot be used ");
    		}
    		else {
    				System.out.print(i * i + ",");
    			 }
     
    } while( i != 0);
    System.out.print(i*i + ",");
    }
    }

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM