-1

I',m trying the basic of objects in java but I got this problem when I try

I have Dogs class

public class Dogs {
    private String Name;
    private int Age;
    private String Color;
    private String Owner;
    public Dogs() {
        this.Name = "Rex";
        this.Age = 5;
        this.Color = "black";
        this.Owner = "John";
    }

    public Dogs(String name, int age, String color, String owner) {
        this.Name = name;
        this.Age = age;
        this.Color = color;
        this.Owner = owner;
    }

//all the getters and setters

and Main class

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("What is the dog's name? ");
        my_dog.setName(input.next());
        System.out.println("What is the dog's age? ");
        my_dog.setAge(input.nextInt());
        System.out.println("What is the dog's color? ");
        my_dog.setColor(input.nextLine());
        System.out.println("What is the owner's name? ");
        my_dog.setOwner(input.nextLine());
    }
}

when I run it, it prints the two questions fine but than it skip on the next...

this is the resault:

What is the dog's name? 
pil
What is the dog's age? 
7
What is the dog's color? 
What is the owner's name?

how can I fix it?

  • 2
    Hope this will help you https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Sri May 03 '20 at 09:15

2 Answers2

2

Please add input.nextLine() after reading the age i.e integer value.

for details refer this

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("What is the dog's name? ");
        my_dog.setName(input.next());
        System.out.println("What is the dog's age? ");
        my_dog.setAge(input.nextInt());
        // add this line
        input.nextLine()
        System.out.println("What is the dog's color? ");
        my_dog.setColor(input.nextLine());
        System.out.println("What is the owner's name? ");
        my_dog.setOwner(input.nextLine());
    }
}
Sri
  • 344
  • 1
  • 3
  • 13
0

Using input.next() instead of input.nextLine() will fix this problem.

nextLine() method returns the line that was skipped by nextInt(), when you pressed the ENTER button after you introduced the dog's age.

public static void main(String... args) {

    Scanner input = new Scanner(System.in);
    Dogs my_dog = new Dogs();
    System.out.println("What is the dog's name? ");
    my_dog.setName(input.next());
    System.out.println("What is the dog's age? ");
    my_dog.setAge(input.nextInt());
    System.out.println("What is the dog's color? ");
    my_dog.setColor(input.next());
    System.out.println("What is the owner's name? ");
    my_dog.setOwner(input.next());

}
Lungu Daniel
  • 790
  • 2
  • 9
  • 15