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.


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

Results 1 to 2 of 2

Thread: For loop, the first command in the loop does not get executed the 2nd time..

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default For loop, the first command in the loop does not get executed the 2nd time..

    Here is my code:
    import java.util.Scanner;
    public class BankAccount1
    	public static void main(String[] args)
    		Scanner input = new Scanner(System.in);
    		char pickup;
    		char size;
    		char type;
    		String name;
    		double cost=0.0;
    		int i;
    	    for (i=0; i<2; i++)
    		System.out.println("What is your name?");
    		name = input.nextLine();
    		System.out.println("size of pizza: ");
    		size =  input.next().charAt(0);
    		System.out.println("type of pizza: ");
    		type = input.next().charAt(0);
    		System.out.println("pick up or delivery?");
    		pickup = input.next().charAt(0);
    		if(size == 'S' || type == 'V') {cost = 10.0;}
    		else if(size == 'M' || type == 'V') {cost = 12.25;}
    		else if(size == 'L' || type == 'V') {cost = 14.50;}
    		else if(size == 'S' || type == 'C') {cost = 7.0;}
    		else if(size == 'M' || type == 'C') {cost = 8.0;}
    		else if(size == 'L' || type == 'C') {cost = 9.0;}
    		if(pickup == 'D'){
    		System.out.println("Name: "+name+". Your cost is: "+cost);}
    		else { 
    		System.out.println("Name: "+name+". Your cost is: "+cost);}

    I have a for loop in my code, and it asks the name, size, type and pickup. The first time it is run, it asks all fields to be entered, but for the 2nd time, the name field does not let me input the name for some reason, it prints the "What is your name?" but it does not let me enter the name...? can someone tell me why?

    Click on the picture for more details:

    Last edited by lina_inverse; October 16th, 2012 at 07:58 PM.

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: For loop, the first command in the loop does not get executed the 2nd time..


    Duplicate post and answered.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 6
    Last Post: October 3rd, 2012, 06:39 PM
  2. Loop only runs one time?
    By Purple01 in forum Loops & Control Statements
    Replies: 6
    Last Post: September 14th, 2012, 06:57 AM
  3. While Loop Confounds JDK's Command Line Debugger (jdb)
    By christerlin in forum Loops & Control Statements
    Replies: 4
    Last Post: August 16th, 2012, 02:10 PM
  4. 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
  5. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM

Tags for this Thread