스터디사이트 : https://www.hackerrank.com


JAVA Basic : Scanner클래스의 useDelimiter() 메소드.


입력

12

4.0  

is the best place to learn and practice coding!  


출력

16
8.0
HackerRank is the best place to learn and practice coding!



코딩연습

Scanner클래스의 메소드가 참 많더라..

특히 has로 시작하는 메소드가 많았다..

우쨌든

int 데이터타입을 뽑아오는 메소드는 nextInt()이고,

double 데이터타입을 뽑아오는 메소드는 nextDouble()이고,

String 데이터타입을 뽑아오는 메소드는 nextString()인 줄 알았는데 next()다...; ㅋ

그치만 Scanner 클래스의 기본 Delimiter (구분자) 가 space (사이띄개)이기 때문에 한 절만 불러오게 된다..


System.in 의 리턴타입은 InputStream이다 참고..


public static void main(String[] args) {
      int i = 4;

double d = 4.0;       

String s = "Hackerrank";

Scanner scan = new Scanner(System.in);


/* Declare second integer, double, and String variables. */
int ii;
double dd;
String ss;


/* Read and save an integer, double, and String to your variables.*/
ii = i + scan.nextInt();
dd = d + scan.nextDouble();     
ss = s + scan.next();
       
/* Print the sum of both integer variables on a new line. */
System.out.println(ii);
/* Print the sum of the double variables on a new line. */
System.out.println(dd);
/* Concatenate and print the String variables on a new line;
        the 's' variable above should be printed first. */
System.out.println(ss);

scan.close();

}

}

결과 :

16
8.0
HackerRank is




코딩연습 - 2 & 결과

Scanner 클래스에서 useDelimiter() 를 사용하면 사이띄개에서 다른 걸로 바꿀 수 있다.

필자는 Escape 문자열인 "\n" 으로 지정하였다. 엔터를 쳐야지만 한 단위로 인식할 수 있게..

그러니까 잘 나오는 것을 확인 할 수 있다.

public static void main(String[] args) {
       int i = 4;

double d = 4.0;       

String s = "Hackerrank";


Scanner scan = new Scanner(System.in);

/* Declare second integer, double, and String variables. */
int ii;
double dd;
String ss;


/* Read and save an integer, double, and String to your variables.*/
ii = i + scan.nextInt();
dd = d + scan.nextDouble();
scan.useDelimiter("\n");       
ss = s + scan.next();
       
/* Print the sum of both integer variables on a new line. */
System.out.println(ii);
/* Print the sum of the double variables on a new line. */
System.out.println(dd);
/* Concatenate and print the String variables on a new line;
        the 's' variable above should be printed first. */
System.out.println(ss);

scan.close();

}

}

결과 :

16
8.0
HackerRank is the best place to learn and practice coding!


+ Recent posts