Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post

    my program is not asking me what my fav color is

    i get no error but my program should be asking me what my favorite color is
    Code:
    import java.util.Scanner;
    
    public class Assignment2
    {
    	public static void main (String [] args)
    	{
    	String firstName,	middleName,	lastName;
    	int age, luckyNumber;
    	String color;
    	
    	Scanner keyboard = new Scanner(System.in);
    	System.out.println("What is your first name?");
    	firstName = keyboard.nextLine();
    	System.out.println("What is your middle name?");
    	middleName = keyboard.nextLine();
    	System.out.println("What is your last name?");
    	lastName = keyboard.nextLine();
    	System.out.println("How old are you?");
    	age = keyboard.nextInt();
    	System.out.println("What is your lucky number?");
    	luckyNumber = keyboard.nextInt();
    	System.out.println("What is your favorite color?");
    	color = keyboard.nextLine();
    	
    	String fullName = firstName + " " + middleName + " " + lastName;
    		System.out.println("A story about " + fullName + ":");
    	String fullNameCaps = fullName.toUpperCase();
    	char firstInitial = firstName.charAt(0);
    	char middleInitial = middleName.charAt(0);
    	char lastInitial = lastName.charAt(0);
    		System.out.println("\t" + fullNameCaps + " is " + firstInitial + middleInitial + lastInitial);
    		System.out.println("\t" + firstInitial + middleInitial + lastInitial + "'s favorite color is " + color + ", and " + firstName + " " + lastInitial + ". is " + luckyNumber);
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This tends to be a poorly documented part of scanner.
    Its been awhile since I've used java cmd, but I believe you simply need to clear out the left whitespaces on the buffer after the nextInt calls. This is because nextInt ignores whitespace separator and is left on the scanner buffer. When you're call to nextLine processes, it has an available whitespace character which is removed and assumed as you're input line. By 'removed', I mean its still on the scanner stack, but the scanner pointer has stepped to a location past the whitespace as opposed to before it.
    After you're lucky number nextInt call, add a keyboard.nextLine(), this will remove the trailing whitespace separator and allow you're next call to keyboard.next* to request input.

    This is why I always stick with nextline and use parseint where necessary. I hate having to worry about the scanner buffer.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •