i need help please i cant input the name of pet in 2nd to the 5th it wont me input the name and skip into age. i dont get any error i just dont get it why it skp the name.

public class PetRecordTESTER

{
    public static void main(String [] args)

    {
        Scanner keyboard = new Scanner(System.in);


        double averageWeight;
        int averageAge;
        String smallest, oldest,youngest,largest;
        PetRecord dog1 = new PetRecord();
        PetRecord dog2 = new PetRecord();
        PetRecord dog3 = new PetRecord();
        PetRecord dog4 = new PetRecord();
        PetRecord dog5 = new PetRecord();



        System.out.println("Enter your 1st pet name: ");
        String name1 = keyboard.nextLine();
        System.out.println("Enter your 1st pet age: ");
        int age1 = keyboard.nextInt();
        System.out.println("Enter your 1st pet weight: ");
        double weight1 = keyboard.nextDouble();

        System.out.println("");
        System.out.println("Enter your 2nd pet name: " );
        String name2 = keyboard.nextLine();
        System.out.println("Enter your 2nd pet age: ");
        int age2 = keyboard.nextInt();
        System.out.println("Enter your 2nd pet weight: ");
        double weight2 = keyboard.nextDouble();

        System.out.println("");
        System.out.println("Enter your 3rd pet name: ");
        String name3 = keyboard.nextLine();
        System.out.println("Enter your 3rd pet age: ");
        int age3 = keyboard.nextInt();
        System.out.println("Enter your 3rd pet weight: ");
        double weight3 = keyboard.nextDouble();

        System.out.println("");
        System.out.println("Enter your 4th pet name: ");
        String name4 = keyboard.nextLine();
        System.out.println("Enter your 4th pet age: ");
        int age4 = keyboard.nextInt();
        System.out.println("Enter your 4th pet weight: ");
        double weight4 = keyboard.nextDouble();

        System.out.println("");
        System.out.println("Enter your 5th pet name: ");
        String name5 = keyboard.nextLine();
        System.out.println("Enter your 5th pet age: ");
        int age5 = keyboard.nextInt();
        System.out.println("Enter your 5th pet weight: ");
        double weight5 = keyboard.nextDouble();

        System.out.println("________________________________________");
        System.out.println("Name 1st pet: " + name1);
        System.out.println("Age 1st pet: " + age1);
        System.out.println("Weight 1st pet: " +weight1);
        System.out.println("");

        System.out.println("Name 22nd pet: " + name2);
        System.out.println("Age 2nd pet: " + age2);
        System.out.println("Weight 2nd pet: " +weight2);
        System.out.println("");

        System.out.println("Name 3rd pet: " + name3);
        System.out.println("Age 3rd pet: " + age3);
        System.out.println("Weight 3rd pet: " +weight3);
        System.out.println("");

        System.out.println("Name 4th pet: " + name4);
        System.out.println("Age 4th pet: " + age4);
        System.out.println("Weight 4th pet: " +weight4);
        System.out.println("");

        System.out.println("Name 5th pet: " + name5);
        System.out.println("Age 5th pet: " + age5);
        System.out.println("Weight 5th pet: " +weight5);
        System.out.println("________________________________________");



    /*  name1=dog1.getName();
        name2=dog2.getName();
        name3=dog3.getName();
        name4=dog4.getName();
        name5=dog5.getName();

        age1=dog1.getAge();
        age2=dog2.getAge();
        age3=dog3.getAge();
        age4=dog4.getAge();
        age5=dog5.getAge();

        weight1=dog1.getWeight();
        weight2=dog2.getWeight();
        weight3=dog3.getWeight();
        weight4=dog4.getWeight();
        weight5=dog5.getWeight();

    */
        if(weight1<weight2&&weight1<weight3&&weight1<weight4&&weight1<weight5)

        {
            smallest= name1;
        }


        else if(weight2<weight1&&weight2<weight3&&weight2<weight4&&weight2<weight5)
        {
            smallest= name2;
        }

        else if(weight3<weight1&&weight3<weight2&&weight3<weight4&&weight3<weight5)
        {
            smallest= name3;
        }
        else if(weight4<weight1&&weight4<weight2&&weight4<weight3&&weight4<weight5)
        {
            smallest=name4;
        }
        else 
        {
            smallest= name5;
        }
        System.out.println("Name of the smallest pet is: " +smallest);

        if(weight1>weight2&&weight1>weight3&&weight1>weight4&&weight1>weight5)
        {
            largest= name1;
        }
        else if(weight2>weight1&&weight2>weight3&&weight2>weight4&&weight2>weight5)
        {
            largest= name2;
        }

        else if(weight3>weight1&&weight3>weight2&&weight3>weight4&&weight3>weight5)
        {
            largest= name3;
        }
        else if(weight4>weight1&&weight4>weight2&&weight4>weight3&&weight4>weight5)
        {
            largest=name4;
        }
        else 
        {
            largest= name5;
        }
        System.out.println("Name of the largest pet is: " +largest);

        if(age1<age2&&age1<age3&&age1<age4&&age1<age5)
        {
            youngest= name1;
        }
        else if(age2<age1&&age2<age3&&age2<age4&&age2<age5)
        {
            youngest= name2;
        }
        else if(age3<age1&&age3<age2&&age3<age4&&age3<age5)
        {
            youngest= name3;
        }
        else if(age4<age1&&age4<age2&&age4<age3&&age2<age5)
        {
            youngest= name4;
        }
        else 
        {
            youngest= name5;
        }
        System.out.println("The youngest pet is: "+youngest);

        if(age1>age2&&age1>age3&&age1>age4&&age1>age5)
        {
            oldest= name1;
        }
        else if(age2>age1&&age2>age3&&age2>age4&&age2>age5)
        {
            oldest= name2;
        }
        else if(age3>age1&&age3>age2&&age3>age4&&age3>age5)
        {
            oldest= name3;
        }
        else if(age4>age1&&age4>age2&&age4>age3&&age2>age5)
        {
            oldest= name4;
        }
        else 
        {
            oldest= name5;
        }
        System.out.println("The oldest pet is: "+oldest);

        averageWeight=(weight1+weight2+weight3+weight4+weight5)/5;
        averageAge=(age1+age2+age3+age4+age5)/5;

        if(dog1==dog2)
        {
            System.out.println(name1+ "and" +name2+ " are equal" );
        }
        else if(dog2==dog3)
        {
            System.out.println(name2+ "and" +name3+ " are equal" );
        }
        else if(dog3==dog4)
        {
            System.out.println(name3+ "and" +name4+ " are equal" );
        }
        else [ICODE][/ICODE]if(dog4==dog5)
        {
            System.out.println(name4+ "and" +name5+ " are equal" );
        }

        }


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I'm not a Scanner expert, but I think...
after you get the weight with a nextDouble() the following carriage-return is still there (it's not part of the double). Then you do a nextLine() which gives you all the chars up to the next CR - which is a zero-length String because there are no chars left before the CR. You need to skip over the CR that's left after the nextDouble(), the the following nextLine() will have to wait for you to type something in.

Scanner class can be tricky to use. Scanner reads and saves what you enter in a buffer and then returns tokens to the next... calls except for the nextLine call which returns it all.
Write a simple test program to test the different next... and has... methods to get a feel for how it works. Also try inputing different input values in different ways like more than one token on a line for next... calls.

This article has been dead for over six months. Start a new discussion instead.