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 does not execute the first command after 2nd time

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

    Default For loop does not execute the first command after 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:

    Attachment 1479

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

    Default Re: For loop does not execute the first command after 2nd time

    The Scanner.next method reads and returns the next word only. It does not read an EOL. So the next time around the loop when you call nextLine it does read the EOL and all it returns is an empty String (ie nothing between the last thing you typed and the EOL).
    Improving the world one idiot at a time!

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. 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
  3. execute a method over a maximum period of time
    By PedroCosta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 24th, 2011, 02:06 PM
  4. Java Child process cannot execute properly for telnet command
    By mamunbu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 07:43 AM
  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