Getting Input from User Example Program in Dart


This code is written in the Dart programming language and demonstrates how to get input from the user using the standard input stream in Dart. Here's what's happening in this code:

  • import 'dart:io'; : This line imports the dart:io library, which provides access to I/O operations in Dart.
  • void main(): This line defines the main function for the program.
  • var name, age; : This line declares two variables name and age without specifying their data types.
  • print('Enter Name & Age : ');: This line outputs a message to the console, prompting the user to enter their name and age.
  • name = stdin.readLineSync();: This line reads a line of text from the standard input stream and stores it in the name variable.
  • age = int.parse(stdin.readLineSync());: This line reads a line of text from the standard input stream and parses it as an integer using the int.parse() function, and then stores it in the age variable.
  • print(name.runtimeType);: This line outputs the data type of the name variable to the console.
  • print(age.runtimeType);: This line outputs the data type of the age variable to the console.
  • stdout.write('Name : $name | Age : $age');: This line outputs a message to the console using the standard output stream. The message includes the values of the name and age variables using string interpolation.

Overall, this program demonstrates how to get input from the user using the standard input stream in Dart, and how to output messages to the console using the standard output stream.

Source Code

import 'dart:io';
void main(){
   var name,age;
   print('Enter Name & Age : ');
   name=stdin.readLineSync();
   age=int.parse(stdin.readLineSync());
   print(name.runtimeType);
   print(age.runtimeType);
   stdout.write('Name : $name | Age : $age');
}
To download raw file Click Here

Output

Enter Name & Age : 
Tutor Joe's
20
String
int
Name : Tutor Joe's | Age : 20