2014년 9월 18일 목요일

java. java.lang.Scanner() methods

java lib '간단' 정리에 맛들렸다 ㅋㅋ
이번엔 최근 가장 많이 쓰는 것 중 하나인 Scanner().

- close(): 다 쓰고 나서 꼭 쓰자!
- next() / nextLine() / nextInt() 등등: 알지?
  - nextBigInteger() / hasNextBigInteger(): BigInteger()도됨
- radix() / useRadix(): 10진수인지 16진수인지 지수 return; / Radix 설정
- findInLine(pattern): 패턴에 맞는거 return. delimiter 무시함. - findWithinHorizon() = findWithinHorizon(Pattern.compile(pattern, horizon))
- locale() / useLocale(): input의 언어정보 출력 /  언어 설정
추가로.. 설명이 필요한 몇 개.
- delimiter() / useDelimiter() / skip(): 구분자(패턴) 설정. / skip은 무시하는 구분자 설정.
     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
     System.out.println(s.nextInt());
     System.out.println(s.nextInt());
     System.out.println(s.next());
     System.out.println(s.next());
     s.close(); 
- match(): MatchResult 출력
public class ScannerDemo {

   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6 ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // check if next token is "Hello"
      System.out.println("" + scanner.hasNext("Hello"));

      // find the last match and print it
      System.out.println("" + scanner.match());

      // print the line
      System.out.println("" + scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}
결과:
 
true
java.util.regex.Matcher[pattern=Hello region=0,25 lastmatch=Hello]
Hello World! 3 + 3.0 = 6 

-

댓글 없음: