Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why it is asking second and third person names

+2 votes
asked Oct 24, 2022 by Gopi Krishna (400 points)
import java.util.Scanner;

public class Main

{

public static void main(String[] args) {   

    Scanner s=new Scanner(System.in);

    int a,b,c;

    String name1;

    String name2;

    String name3;

    System.out.println("enter the first person name");

    name1=s.nextLine();

    System.out.println("ebter his age");

    a=s.nextInt();

    System.out.println("enter the second person");

    name2=s.nextLine();

    System.out.println("enter his age");

    b=s.nextInt();

    System.out.println("enter the third person");

    name3=s.nextLine();

    System.out.println("enter his age");

    c=s.nextInt();

    if(a>b)

    

    System.out.println("first person is older");

    else if(b>c)

    System.out.println("second person is older");

    else

    System.out.println("third person isolder ");

    }

}

1 Answer

0 votes
answered Oct 24, 2022 by Peter Minarik (84,720 points)

See the solution to your problem here.

Also, if you're trying to find the oldest person of the three, then your code is wrong. Review your logic.

commented Oct 24, 2022 by Gopi Krishna (400 points)
actually  my question is the second and third person names are not asking by the compiler
could u please explain me in detail
commented Oct 24, 2022 by Jeet Narayan Chakraborty (150 points)
the nextInt() method of the class only takes input integer but after you press Enter it's a character - '\n" so it's left over and that creates the problem.




import java.util.Scanner;

public class Main

{

public static void main(String[] args) {   

    Scanner s=new Scanner(System.in);

    int a,b,c;

    String name1;

    String name2;

    String name3;

    System.out.println("enter the first person name");

    name1=s.nextLine();

    System.out.println("ebter his age");

    a=s.nextInt();
    name2=s.nextLine();

    System.out.println("enter the second person");

    name2=s.nextLine();

    System.out.println("enter his age");

    b=s.nextInt();
    name3=s.nextLine();

    System.out.println("enter the third person");

    name3=s.nextLine();

    System.out.println("enter his age");

    c=s.nextInt();

    if(a>b && a>c)


    System.out.println(name1 + " is older");

    else if(b>c && b>a)

    System.out.println(name2 + " is older");

    else

    System.out.println(name3 + " is older ");

    }

}
commented Oct 24, 2022 by Peter Minarik (84,720 points)
Exactly as Jeet Narayan Chakraborty said (and as it is explained in the linked forum): nextInt() only reads the digits from the input stream and does not process the the rest, and there is a rest: the new line character. So when you call nextLine() it finds input (new line character) and processes it so it won't wait for the user to enter anything.

The suggested solutions are in the linked article.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...