ජාවා ලෝකය











Java SE Downloads
ජාවා ලෝකය. Powered by Blogger.

Tuesday, August 16, 2016

User Input ලබාදීම (Scanner class එක භාවිතය)



අද කියලා දෙන්න යන්නේ ජාවා ක්‍රමලේඛන භාෂාව තුල user input භාවිතා කරන ආකාරයයි. මුලින්ම අපි බලමු මොකක්ද මේ user input එකක් කියන්නේ කියලා.

හිතන්න අපි මෙච්චර කල් කරපු පාඩම් වලදී කලේ ක්‍රමලේඛනය ලිවීමේදී ලියන ලද විචල්‍යයක් භාවිතයෙන් යම් කිසි ප්‍රතිපලයක් output එකක් ලෙස ලබාගැනීම. දැන් අපි කියලා දෙන්න යන user input එක නිසා ඉදිරියට කරන පාඩම් වලදී යම් කිසි විචල්‍යයකට කලින් අගයක් ලබා නොදී එම වැඩ සටහන ක්‍රියාත්මක වන විටදීම එයට අගයක් ලබාදීමට පුළුවන්.

මේ විදිහට user input ලබාදෙන ක්‍රම කීපයක්ම ජාවා වල තිබෙනවා. අද අපි බලමු java හි util package එකේ ඇති Scanner class එක භාවිතා කරමින් User Input ලබාදෙන ආකාරය. තවද මෙය ජාවා වල භාවිතා වන object oriented කොටසට අයත් වෙනවා.  

පහත උදාහරණය බලන්න. මෙහිදී ඔබගේ නම Enter your name here යනුවෙන් ඔබේ නම අසන අතර ඉන් පසුව නම ඇතුලත් කල විට HELLO යනුවෙන් ඔබව අමතයි.

මෙහි Class Name එක UserInput වන අතර x නමින් string (Variable)එකක් යොදාගෙන ඇත.

 import java.util.Scanner;  
 public class UserInput {  
   public static void main(String args[]) {  
     Scanner myScanner = new Scanner(System.in);  
     String x;  
     System.out.print("" + "Enter your name here: ");  
     x = myScanner.nextLine();  
     System.out.println("HELLO, " + x);  
   }  
 }  

මෙසේ user input එකක් භාවිතයෙදී import java.util.Scanner; යන කොටස අනිවාර්යෙන්ම යෙදිය යුතුය.  මෙහිදී සිදු කරන්නේ util package එකෙ ඇති  Scanner class එක අපගේ ජාවා වැඩසටහන තුලට import කරගැනීමයි. ඉන්පසුව එම Scanner class එක භාවිතා කොටගෙන object එකක් සාදාගැනීම සිදුකරනු ලබනවා. ඉන්පසුව එම සාදාගත් object එකට System.in ලෙස parameter එකක් pass කිරීම සිදුකරනු ලබනවා.
Scanner myScanner = new Scanner(System.in);
මෙහිදී  myScanner යනු අප සාදාගත් object එකේ නමයි.   තවද Scanner එක භාවිතා කොට  String එකක් input  එක ලෙස   ලබා ගත යුතු නම්  next()  හෝ nextLine() යන  method  දෙකෙන් එකක් භාවිත කල යුතුය. අප මෙහිදී භාවිතා කොට ඇත්තේ nextLine() යන method එකයි.

පහත ඇනිමේෂන් එකේ තිබෙන්නේ මෙම වැඩසටහන ක්‍රියාත්මක වන ආකාරයයි.


තවද අපි මේ ආකාරයට input කරනු ලබන්නේ integer එකක් නම් nextLine() වෙනුවට nexInt() භාවිතාකල යුතු අතර double එකක් භාවිතා කරයි නම් nextDouble() යන්න භාවිතා කල යුතුය.(ඉදිරි පාඩම් වලදී මෙය තවත් අවබෝධ වේවි.) පහත තිබෙන්නේ ඒ විදිහට Scanner class එක තුල භාවිතා කල හැකි method list එකයි.


Method Description
void close() Closes the scanner object.
Pattern delimiter() Returns the Pattern the Scanner object is currently using to match delimiters.
String findInLine(Pattern pattern) This method returns a String object that satisfies the Pattern object specified as method argument.
String findInLine(String pattern) Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
String findWithinHorizon(Pattern pattern, int horizon) Attempts to find the next occurrence of the specified pattern.
String findWithinHorizon(String pattern, int horizon) This method simply attempts to find the next occurrence of a pattern input ignoring delimiter
boolean hasNext() Returns true if this scanner has another token in its input.
boolean hasNext(Pattern pattern) Returns true if the next complete token matches the specified pattern.
boolean hasNext(String pattern) Returns true if the next token matches the pattern constructed from the specified string.
boolean hasNextBigDecimal() Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method.
boolean hasNextBigInteger() Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method.
boolean hasNextBigInteger(int radix) Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the nextBigInteger() method.
boolean hasNextBoolean() This method checks if the Scanner object has boolean data type on its buffer.
boolean hasNextByte() This method returns true if the next byte on the scanner buffer can be translated to byte data type otherwise false.
boolean hasNextByte(int radix) Returns true if the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method.
boolean hasNextDouble() Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method.
boolean hasNextFloat() Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.
boolean hasNextInt() Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
boolean hasNextInt(int radix) This method returns boolean, true if  the token can be interpreted as int data type with respect to the radix used by the scanner object otherwise false.
boolean hasNextLine() This method returns a boolean data type which corresponds to the existence of new line on the String tokens which the Scanner object holds.
boolean hasNextLong() Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method.
boolean hasNextLong(int radix) Returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the nextLong() method.
boolean hasNextShort() Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method.
boolean hasNextShort(int radix) This method returns boolean, true if  the token can be interpreted as short data type with respect to the radix used by the scanner object otherwise false.
IOException ioException() Returns the IOException last thrown by this Scanner's underlying Readable.
Locale locale() This method returns a Locale which the Scanner class is using.
MatchResult match() This method returns a MatchResult object which corresponds to the result of the last operation by the scanner object.
String next() Finds and returns the next complete token from this scanner.
String next(Pattern pattern) Returns the next token if it matches the specified pattern.
String next(String pattern) Returns the next token if it matches the pattern constructed from the specified string.
BigDecimal nextBigDecimal() Scans the next token of the input as a BigDecimal.
BigInteger nextBigInteger() Scans the next token of the input as a BigInteger.
BigInteger nextBigInteger(int radix) Scans the next token of the input as a BigInteger.
boolean nextBoolean() Scans the next token of the input into a boolean value and returns that value.
byte nextByte() Scans the next token of the input as a byte.
byte nextByte(int radix) Scans the next token of the input as a byte.
double nextDouble() Scans the next token of the input as a double.
float nextFloat() Scans the next token of the input as a float.
int nextInt() Scans the next token of the input as an int.
int nextInt(int radix) Scans the next token of the input as an int.
String nextLine() Advances this scanner past the current line and returns the input that was skipped.
long nextLong() Scans the next token of the input as a long.
long nextLong(int radix) Scans the next token of the input as a long.
short nextShort() Scans the next token of the input as a short.
short nextShort(int radix) Scans the next token of the input as a short.
int radix() Returns this scanner's default radix.
void remove() The remove operation is not supported by this implementation of Iterator.
Scanner reset() Resets this scanner.
Scanner skip(Pattern pattern) Skips input that matches the specified pattern, ignoring delimiters.
Scanner skip(String pattern) Skips input that matches a pattern constructed from the specified string.
String toString() Returns the string representation of this Scanner.
Scanner useDelimiter(Pattern pattern) Sets this scanner's delimiting pattern to the specified pattern.
Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
Scanner useLocale(Locale locale) Sets this scanner's locale to the specified locale.
Scanner useRadix(int radix) Sets this scanner's default radix to the specified radix.

ඊලග පාඩමෙන් නැවත හමුවෙමු.

0 comments:

Post a Comment