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

Thread: How to loop this??

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to loop this??

    How do you loop this code so that if they entered the wrong digits the second time it will go back to "enter number again and start the "if" and "else" statement again"

    public class Assign1 {
     
    	/**
    	 * @param args
    	 */
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input = new Scanner (System.in);
    int number1;
     
     
    { System.out.print ("\nEnter a five digit number: ");
     number1 = input.nextInt();
     
    if (number1>=10000 && number1<=99999) {
    	System.out.print("The number is \n");
    	for(char c : String.valueOf(number1).toCharArray()) {
     
    	    System.out.print(c + "\n");}}
     
    	    else 
    	    	System.out.print ("\nPlease enter 5 right digit number: ");
    number1 = input.nextInt();
    System.out.print("The number is \n");
    for(char c : String.valueOf(number1).toCharArray()) {
     
        System.out.print(c + "\n");}
     
    }
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,532
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: How to loop this??

    Recommended reading: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 03: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