Q:

How to use scanner hasNext() to loop through a line of keyboard text and validate the user input of interger and string

I am having a problem trying to understand how I can loop through a keyboard input line of text the user will give ex: 
Anika 14 Dan 16 
I want to read each token and assign to String name, Int, age, String name, int age. in that order.
This is easy however, if the user enters 
Anika Anika Anika Anika 13 13 13 Dan 16
Then I want the program to: 
Anika, 
Integer needed got String, 
Integer needed got String, 
Integer needed got String, 
13, 
String needed got Integer, 
String needed got Integer, 
Dan, 
16 

So first one will always be a string which is a word EDIT: "word", second an int and thrid string which is a "word" and fourth int. 
However, I can not simulate this. 
Scanner scan = new Scanner(System.in);
String name = null;
int age = 0;
String name2= null;
int age2= 0;
                if(scan.hasNext() == true)
        name = scan.next();
            age = scan.nextInt();
        name2= scan.next();
            age2= scan.nextInt();

I know if I do the top I get the right order, but it is the extra inputs that I would like to ignore but write a statement expression why it's wrong and then continue to search for the next int, or third string and so on.
 boolean isstring = false;
  boolean isnumber = false;
    do {
    if (scan.hasNext())
    {
        name = scan.next();
        isstring = true;
    }
    else {

        System.out.println("Need String got Integer");
        isstring = false;
        scan.next();

    }
    } while (!isstring);

    do {
    if (scan.hasNextInt())
    {
        age = scan.nextInt();
        isnumber=true;
    }
    else {

        System.out.println("Need Integer got String");
        isnumber=false;
        scan.nextInt();

    }
    } while (!isnumber);

    do {
    if (scan.hasNext())
    {
        name2 = scan.next();
        isstring = true;
    }
    else {

        System.out.println("Need String got Integer");
        isstring = false;
        scan.next();

    }
    } while (!isstring);

    do {
    if (scan.hasNextInt())
    {
        age2 = scan.nextInt();
        isnumber = true;
    }
    else {

        System.out.println("Need Integer got String");
        isnumber=false;
        scan.nextInt();

    }
    } while (!isnumber);

    }

I tried to use do while with ifs and It didnt work. My logic is wrong somewhere, and I think it might be the has.next() method. 
Any help will be appreciated!!

A:

If the input is a word while waiting for an Integer, it will throw InputMismatchException. nextInt() first read the value as a String an then parse it as a Integer, so if you ignore the value using nextInt, if the value is a word, it will trow the aforementioned Exception.
Using the same logic of your program
The changes should be:

Ignore the input with scan.next()
Check if a String can be or not an Integer (using scan.hasNextInt()), not if is a String, because any Integer can be expressed as a String.

  boolean isstring = false;
  boolean isnumber = false;

  do {
   if (!scan.hasNextInt()) {
    isstring = true;
    name = scan.next();
   } else {
    isstring = false;
    System.out.println("Need String got Integer");
    scan.next();
   }
  } while (!isstring);

  do {
   if (scan.hasNextInt()) {
    isnumber = true;
    age = scan.nextInt();
   } else {
    isnumber = false;
    System.out.println("Need Integer got String");
    scan.next();
   }
  } while (!isnumber);

  do {
   if (!scan.hasNextInt()) {
    isstring = true;
    name2 = scan.next();
   } else {
    isstring = false;
    System.out.println("Need String got Integer");
    scan.next();
   }
  } while (!isstring);

  do {
   if (scan.hasNextInt()) {
    isnumber = true;
    age2 = scan.nextInt();
   } else {
    isnumber = false;
    System.out.println("Need Integer got String");
    scan.next();
   }
  } while (!isnumber);

Using try/catch and one loop
A naive solution using try/catch can be the following
public static void main (String[]args)
  {
    String name = null;
    String name2 = null;
    Integer age = null;
    Integer age2 = null;
    Scanner scan = new Scanner(System.in);
    while (scan.hasNext())
      {
    try
    {
      if (name == null)
        {
          System.out.println("Please provide name: ");
          name = getNameOrFail(scan);
          System.out.println("Name set: " + name);
        }
      if (age == null)
        {
          System.out.println("Please provide age: ");
          age = getAgeOrFail(scan);
          System.out.println("Age set: " + age);
        }
      if (name2 == null)
        {
          System.out.println("Please provide name2: ");
          name2 = getNameOrFail(scan);
          System.out.println("Name2 set: " + name2);
        }
      if (age2 == null)
        {
          System.out.println ("Please provide age2: ");
          age2 = getAgeOrFail (scan);
          System.out.println ("Age2 set: " + age2);
        }
    }
    catch (Exception e)
    {
      System.out.println(e.getMessage ()); // Print the message put int Exception(message) constructor
      scan.nextLine(); // Flush the Scanner cache
    }
      }
  }

  public static String getNameOrFail(Scanner scan) throws Exception
  {
    if (scan.hasNextInt())
      throw new Exception("Need String got Integer");
    return scan.next();
  }

  public static Integer getAgeOrFail(Scanner scan) throws Exception
  {
    if (!scan.hasNextInt())
      throw new Exception("Need Integer got String");
    return scan.nextInt();
  }

Pay attention to the scan.newLine() in the catch clause, this is needed because the Scanner use a cache with the last input, so if is not re-read you enter in a infinite loop condition.
Good luck!

