How to use/check for String variables in the Scanner using Java Neon Eclipse

Опубликовано: 31 Октябрь 2024
на канале: Minute Coding
788
5

The purpose of this video is to demonstrate the method and variables required to check/validate a string the user entered using the Scanner. Additionally, it covers how we can check for integers.

Please note: I did not optimize the questionnaire (i.e. There isn't a looping function to go back if there is incorrect data).

Here is the code if you wish to copy and paste it:

import java.util.Scanner;

public class Random_Questions {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);

System.out.println("What's your name?");

String name = input.nextLine();
if (name.equals("Claire"))
{
System.out.println("How old are you?");

int age = input.nextInt();
if (age !=22)
{
System.out.println("Thank you for your time.");
}

else
{
System.out.println("This was a pointless questionaire, thank you.");
}



}
else
{
System.out.println("Leave");
}
}
}