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

Thread: Output isn't as expected.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Output isn't as expected.

    I'm in the process of creating a program that will get information from the user on who all is attending a competition, and then the scores of the teams attending and then it will give the 1st place winner, 2nd place winner and 3rd place winner. But im only on the part where the program will ask the user who is attending and im already having a problem with the output.

    school.jpg

    As you see, it skips asking me what the name of school 2 is...

    this is my code

     import java.util.Scanner;
     
     
    public class Competition 
    {
        	static Scanner input = new Scanner(System.in);
        	static School[] Attendee;
        	static int numSchools;
     
        	public static void main(String args[])
        	{
        		System.out.print("How many schools are attending the Competition? ==>  ");
        		numSchools = input.nextInt();
        		Attendee = new School[numSchools];
        		for (int i = 0; i<numSchools; i++)
        		{
        			int realNum = i+1;
        			Attendee[i] = new School(School.getName(realNum),School.getPlatoon());
        		}
        	}
     
     
    }
     
     
    class School
    {
    	static Scanner input = new Scanner(System.in);
    	static String schoolName;
    	static int havePlatoon;
     
    	public School(String n, int p)
    	{
    		schoolName = n;
    		havePlatoon = p;
    	}
     
    	public static String getName(int n)
    	{
    		System.out.println();
    		System.out.println();
    		System.out.print("What is the name of school number "+n+"?  ==>  ");
    		schoolName = input.nextLine();
    		return schoolName;
    	}
     
    	public static int getPlatoon()
    	{
    		System.out.println();
    		System.out.println();
    		System.out.print("Does "+schoolName+" have a platoon? (1 for yes, 2 for no)  ==>  ");
    		havePlatoon = input.nextInt();
    		return havePlatoon;
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Output isn't as expected.

    The answer boils down to the intricacies of how the scanner works. When calling nextInt, a new line char is still present in the buffer. Calling nextLine again will result in the scanner seeing the new line and returning immediately. Simple fix is to change nextInt into a newLine and parse the integer from there.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Output isn't as expected.

    Thank you! it works perfectly now.

Similar Threads

  1. Program not returning expected value.
    By adidez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 05:16 AM
  2. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 02:50 PM
  3. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:33 PM
  4. ';' expected?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2009, 11:55 AM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM