Skip to content

Chamupathi Gigara's personal Blog

Just another Giga Network site

Menu
  • Blog
  • About Me
Menu

Java – Input of Data

Posted on July 4, 2018July 4, 2018 by gigara

Reading Data from Keyboard

කලින් ලිපියේදී අපි output ගැන සාකච්චා කලා.

System.out.println() // produce output to the screen

 

දැන් අපි බලමු කොහොමද inputs අරගන්නේ කියලා.

System.in – to get input from the keyboard

මෙහිදී අපි Scanner Class එක භාවිතා කල යුතයි. Scanner class එක java හි තිබෙන class එකක් නිසා අපි එය භාවිතයට ගැනීමට පෙර එය අපේ programme එකට import කර ගත යුතුයි.

import java.util.Scanner;

මෙම code එක අපේ programme එකට add කිරීම මගින් Scanner class එක අපේ programme එකට add කර ගත හැක.

දැන් අපි බලමු කොහොමද scanner එක use කරන්නේ කියලා.

Scanner sc = new Scanner(System.in);

මෙම code එක මගින් සිදුවන්නේ new scanner object එකක් sc යන නමින්  initialize වෙන එක.

දැන් අපි බලමු කොහොමද අපේ scanner එකට data input කරන්නේ කියලා.

int marks;                               //initialize variable
Scanner input = new Scanner(System.in);  //initialize Scanner
marks = input.nextInt();                 //save user value into the variable

marks = input.nextInt(); මෙහිදී අපි nextInt යොදාගත්තේ අපි ලබා දෙන input එක integer එකක් නිසා. අපි ලබා දෙන input එක String (text) එකක නම් අපි nextInt() වෙනුවට nextLine යන්න යොදාගත යුතුය.

දැන් අප ලබාදුන්නු input එක marks කියන variable එකේ තාවකාලිකව ගබඩා වී ඇත. දන් අපිට එය අපේ programme එකේ භාවිත කල හැක.

name=sc.nextLine();  
System.out.println("Hello "+name);  //Print Hello + User input

 

Full example code

import java.util.Scanner;  
class input{  
 public static void main(String args[]){  
   Scanner sc=new Scanner(System.in);  
   
   String name;
   System.out.println("Enter your name");  
   name=sc.nextLine();  
   
   System.out.println("Hello "+name);  
   sc.close();  
 }  
}

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • දේවාරූඩයෙන් Code කරන්න
  • මොනවද මේ Malware?
  • WhatsApp සදහටම නැති වෙයි?
  • Ballerina ගැන සිංහලෙන් – Ballerina in Sinhala
  • VPN ගැන ඔබ දැනුවත් ද?

Recent Comments

  • දේවාරූඩයෙන් Code කරන්න | Chamupathi Gigara's personal Blog on Ballerina ගැන සිංහලෙන් – Ballerina in Sinhala
  • Aruna Danuddara on මොනවද මේ Malware?
  • Nishiki Yapa on මොනවද මේ Malware?
  • Papaya on මොනවද මේ Malware?
  • Vidushika Ruwanpathirana on WhatsApp සදහටම නැති වෙයි?

Archives

  • February 2022
  • April 2020
  • December 2019
  • July 2019
  • April 2019
  • March 2019
  • February 2019
  • July 2018
  • March 2018
  • February 2018

Categories

  • Ballerina
  • Cyber Security
  • Java
  • Python
  • Uncategorized
© 2022 Chamupathi Gigara's personal Blog | Powered by Superbs Personal Blog theme