0

The code works for entering wife's name and age, but fails to print the names of sons, though it displays their ages properly.

import java.util.Scanner;

public class Check2
{
    public static void main(String[] args)
    {
    String wife;
    String son1;
    String son2;
    int wifeAge;
    int son1Age;
    int son2Age;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Wife's name? ");
    wife = keyboard.nextLine();
    System.out.println("Her age? ");
    wifeAge = keyboard.nextInt();
    System.out.println();

    System.out.println("First son's name? ");
    son1 = keyboard.nextLine();
    keyboard.nextLine();
    System.out.println("His age? ");
    son1Age = keyboard.nextInt();
    System.out.println();

    System.out.println("Second son's name? ");
    son2 = keyboard.nextLine();
    keyboard.nextLine();
    System.out.println("His age? ");
    son2Age = keyboard.nextInt();
    System.out.println();

    keyboard.nextLine();

    System.out.println("My wife's name is " + wife + ". She is " +
                       wifeAge + " years old.\nOur first son is " +
                       son1 + ". He is " + son1Age + ".\nOur " +
                       "second son is " + son2 + ". He is " +
                       son2Age + ".");
    }
}
clearlight
  • 10,772
  • 11
  • 48
  • 65
jpan784
  • 1
  • 1
  • 1
    read this ,will solve your issue , Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Pavneet_Singh Dec 29 '16 at 07:06
  • Learn how to use a debugger and debug your program – Jeroen Heier Dec 29 '16 at 07:07
  • I've provided a working example. Please up-vote the answer and click √ to accept, if you find the solution solves the problem well for you. Thanks – clearlight Jan 01 '17 at 17:24

3 Answers3

1

You have the extra keyboard.nextLine(); in the wrong place in your code.

son1 = keyboard.nextLine();
keyboard.nextLine(); // you don't need this here.

son2 = keyboard.nextLine();
keyboard.nextLine(); // nor here

Keep the extra keyboard.nextLine(); line after each keyboard.nextInt(); and your program should run fine.

wifeAge = keyboard.nextInt();
keyboard.nextLine(); // put it here
...
son1Age = keyboard.nextInt();
keyboard.nextLine(); // and here

The nextInt() reads only an integer value and not a new line. If you need to read a new line, you will need to put the keyboard.nextLine(); every time you read integer values from the keyboard.

Hope this helps!

anacron
  • 5,733
  • 2
  • 21
  • 31
0
import java.util.Scanner;

public class Check2
{
    public static void main(String[] args)
    {
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;

Scanner keyboard = new Scanner(System.in);

System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();

System.out.println("First son's name? ");
keyboard.nextLine();
son1 = keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();

System.out.println("Second son's name? ");
keyboard.nextLine();    
son2 = keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();

keyboard.nextLine();

System.out.println("My wife's name is " + wife + ". She is " +
                   wifeAge + " years old.\nOur first son is " +
                   son1 + ". He is " + son1Age + ".\nOur " +
                   "second son is " + son2 + ". He is " +
                   son2Age + ".");
    }
}
Aniket
  • 167
  • 12
0

nextLine() isn't the method you want. You want to use next() instead, as shown in the following corrected and simplified code. You don't really want to have to manage the newlines, just let the scanner treat all the input as a string of tokens separated by whitespace, and read them in sequence. Use the next() method for inputting strings and nextInt() for inputting integers...

import java.util.Scanner;

public class Check2
{
    public static void main(String[] args)
    {
        String wife;
        String son1;
        String son2;
        int wifeAge;
        int son1Age;
        int son2Age;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Wife's name? ");
        wife = keyboard.next();
        System.out.println("Her age? ");
        wifeAge = keyboard.nextInt();
        System.out.println();

        System.out.println("First son's name? ");
        son1 = keyboard.next();
        System.out.println("His age? ");
        son1Age = keyboard.nextInt();
        System.out.println();

        System.out.println("Second son's name? ");
        son2 = keyboard.next();
        System.out.println("His age? ");
        son2Age = keyboard.nextInt();
        System.out.println();

        System.out.println("My wife's name is " + wife + ". She is " +
                   wifeAge + " years old.\nOur first son is " +
                   son1 + ". He is " + son1Age + ".\nOur " +
                   "second son is " + son2 + ". He is " +
                   son2Age + ".");
        }
   }
clearlight
  • 10,772
  • 11
  • 48
  • 65