CORE
HOME > JAVA > J2SE > CORE
2017.05.22 / 16:44

[Java] Å°º¸µå ÀԷ¹޴ ¹æ¹ý

ducati
Ãßõ ¼ö 209

Java¿¡¼­ Å°º¸µå ¹®ÀÚ ÀÔ·Â ¹æ¹ý¿¡ ´ëÇؼ­ Á¤¸®Çغ¾´Ï´Ù.


C¾ð¾î·Î À̾߱â ÇÏÀÚ¸é scanf() ¿¡ ÇØ´çÇÒ ¼ö ÀÖ½À´Ï´Ù.




ÃÑ 3°¡Áö ÀÖÀ¸¸ç °á·Ð ºÎÅÍ À̾߱â ÇÑ´Ù¸é,


1) Scanner¸¦ »ç¿ëÇϰųª

2) System.in.read() ÇÔ¼ö¿Í BufferedReader¸¦ È¥¿ëÇؼ­ »ç¿ë ÇÏ´Â ¹æ¹ýÀÌ ÀÖÀ» ¼ö ÀÖ°Ú½À´Ï´Ù.

Scanner ¿Í BufferedReader¸¦ Â÷ÀÌÁ¡À» ºñ±³ÇÏÀÚ¸é, »ç¿ëÀÇ ÆíÀǼºÀ» ¸»ÇÒ ¼ö ÀÖ½À´Ï´Ù.
±×¸®°í Scanner°¡ ´õ ÃÖ±Ù¿¡ ³ª¿Â °´Ã¼ÀÔ´Ï´Ù.


¹æ¹ý1. Scanner °´Ã¼ »ç¿ë


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Scanner scn = new Scanner(System.in);
 
byte byt = scn.nextByte();
 
short srt = scn.nextShort();
 
long lng = scn.nextLong();
 
float flt = scn.nextFloat();
 
double dbl = scn.nextDouble();
 
boolean ble = scn.nextBoolean();
 
String strLine = scn.nextLine();
String str = scn.next();
cs


À§¿Í °°Àº ¹æ¹ýÀ¸·Î Scanner °´Ã¼¸¦ »ý¼ºÇÏ°í System.in À» ¿¬°á ½ÃÄÑÁÖ¸é Ç¥ÁØÀԷ½ºÆ®¸²À» ¹Þ¾Æ º¼ ¼ö ÀÖ´Ù.


Enter ÀÔ·Â ¿¹¿Üó¸®

±×·±µ¥, ¿©±â¼­ ÁÖÀÇ ±í°Ô ºÁ¾ßÇÒ Á¡ÀÌ ÀÖ´Ù.  

¹Ù·Î, ¹®ÀÚ¿­À» ÀÔ·Â ¹ÞÀ» ¶§ »ç¿ëÇÏ´Â ÇÔ¼ö°¡ µÎ°³°¡ ÀÖ´Â Á¡ÀÌ´Ù. 


1
2
String str = scn.next();
String strLine = scn.nextLine();
cs


µÎ °³ÀÇ Â÷ÀÌÁ¡Àº ¹«¾ùÀΰ¡?


next()´Â °ø¹é ´ÜÀ§·Î ¹®ÀÚ¿­À» Àд´Ù.


nextLine()´Â Enter ´ÜÀ§·Î ¹®ÀÚ¿­À» Àд´Ù.  (Enter ÀÔ·ÂÀº \r\n À¸·Î ±¸¼ºµÈ µÎ°³ÀÇ ¹®ÀÚÀÌ´Ù.)



±×·¡¼­ next()¿Í nextLine()À» ¼¯¾î¼­ »ç¿ëÇÒ ¶§ ÁÖÀÇ ÇÒ Á¡ÀÌ ÀÖ´Ù.


1
2
3
4
5
6
7
8
9
10
Scanner scn = new Scanner(System.in);
System.out.println("¹è¿­ Å©±â¸¦ ÀÔ·ÂÇϼ¼¿ä");
        
int ArraySize = scn.nextInt();
 
String[] strArray = new String[ArraySize];
 
System.out.println("¹®ÀÚ¿­À» ½ºÆäÀ̽º¹Ù·Î ¶ç¿ö¼­ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
 
String strInput = scn.nextLine();
cs


À§¿Í °°Àº ¿¹Á¦¿¡¼­

next() ½ÇÇà ¶óÀο¡¼­ ¼ýÀÚ 2¿Í Enter¸¦ ÀÔ·Â ½Ã, nextLine()¿¡¼­ ÀÔ·ÂÀ» ±â´Ù¸®Áö ¾Ê°í ¹Ù·Î Åë°ú µÇ¾î ¹ö¸°´Ù.

±× ÀÌÀ¯´Â, nextInt()°¡ ½ÇÇàµÉ¶§ ¼ýÀÚ 2¸¸ Àаí, Enter(\r\n)°ªÀº ÀÔ·Â ½ºÆ®¸² ¹öÆÛ¿¡ ³²¾Æ Àֱ⠶§¹®ÀÌ´Ù. 

µû¶ó¼­, nextLine() ½ÇÇà ½Ã, ³²¾Æ ÀÖ´Â Enter °ªÀ» ÀÐ¾î µéÀÌ°í Åë°úÇÏ°Ô µÈ´Ù. 


µû¶ó¼­, ¿øÇÏ´Â ´ë·Î µ¿ÀÛÇÏ·Á¸é ¾Æ·¡¿Í °°ÀÌ ¼öÁ¤ ÇØ¾ß ÇÑ´Ù.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Scanner scn = new Scanner(System.in);
System.out.println("¹è¿­ Å©±â¸¦ ÀÔ·ÂÇϼ¼¿ä");
        
int ArraySize = scn.nextInt();
 
/*!!!!!!!!!!!!! Enter ÀÔ·Â󸮠!!!!!!!!!!!!!!*/
scn.nextLine();
 
 
String[] strArray = new String[ArraySize];
 
System.out.println("¹®ÀÚ¿­À» ½ºÆäÀ̽º¹Ù·Î ¶ç¿ö¼­ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
 
String strInput = scn.nextLine();
cs



¹æ¹ý2. System.in.read() ÇÔ¼ö »ç¿ë


µÎ ¹ø° ¹æ¹ýÀ¸·Î System Ŭ·¡½º¿¡¼­ Á¦°øÇÏ´Â read() ¶ó´Â ÇÔ¼ö¸¦ ÀÌ¿ëÇÏ¿© Å°º¸µå ÀÔ·ÂÀ» ¹Þ¾Æº¸ÀÚ.

read() ÇÔ¼ö´Â 1byte Å©±â¸¸ ÀԷ½ºÆ®¸²¿¡¼­ Àоî¿Í ASCII Äڵ忡 ÇØ´çÇϴ int °ªÀ» ¸®ÅÏÇÑ´Ù.


±×·¡¼­ ¹®ÀÚ¸¦ ÀԷ¹ÞÀ»¶§ ÁÖ·Î »ç¿ëÇÑ´Ù.


1
2
3
4
5
//¹®ÀÚ a ÀúÀå
char chr = (Char)System.in.read();
 
//¹®ÀÚ a ´ë½Å 97¼ýÀÚ ÀúÀå
int ascii = System.in.read();
cs



Enter ÀÔ·Â ¿¹¿Üó¸®

±×·¯¸é Enter¿¡ ´ëÇÑ ¿¹¿Ü 󸮴 ¾î¶»°Ô ÇÒ °ÍÀΰ¡?
read()ÇÔ¼öÀÇ °æ¿ì ScannerÀÇ next()ÇÔ¼ö¿Í À¯»çÇÏ°Ô µ¿ÀÛÇÑ´Ù. ¼ýÀÚ + Enter ÀÔ·Â ½Ã, Enter(\r\n) °ªÀº ÀԷ½ºÆ®¸² ¹öÆÛ¿¡ ±×´ë·Î ³²¾Æ ÀÖ°Ô µÈ´Ù.

µû¶ó¼­, ¾Æ·¡¿Í °°ÀÌ ¿¹¿Ü 󸮸¦ ÇØÁÖ¾î¾ß ÇÑ´Ù.
EnterÀÇ °æ¿ì µÎ°³ÀÇ ¹®ÀÚ (\r\n) À̹ǷΠread()ÇÔ¼ö¸¦ µÎ ¹ø È£ÃâÇÏ¿© ¹®ÀÚµéÀ» ¹öÆÛ¿¡¼­ Á¦°Å ÇØÁØ´Ù.


1
2
3
4
5
char chr = (Char)System.in.read();
 
/*!!!!!!!!!!!!! Enter ÀÔ·Â󸮠!!!!!!!!!!!!!!*/
System.in.read();
System.in.read();
cs

¶Ç´Â

1
2
3
4
char chr = (Char)System.in.read();
 
/*!!!!!!!!!!!!! Enter ÀÔ·Â󸮠!!!!!!!!!!!!!!*/
System.in.skip(2);
cs


¹æ¹ý3. BufferedReader °´Ã¼ »ç¿ë


BufferedReader °´Ã¼´Â ¹®ÀÚ¸¦ ½ºÆ®¸²¿¡¼­ ÀÐÀ» ¶§ ¹öÆÛ¸¦ »ç¿ëÇÏ¿© Á»´õ ¹®ÀÚ¿­ 󸮸¦ Æí¸®ÇÏ°Ô ÇØÁÖ´Â °´Ã¼ ÀÌ´Ù.

InputStreamReaderÀÇ °æ¿ì ÇÑ ¹®ÀÚ¾¿ ½ºÆ®¸²¿¡¼­ ÀÐ¾î ¿ÀÁö¸¸, BufferedReader´Â ¹Ì¸® ½ºÆ®¸²¿¡ ÀÖ´Â µ¥ÀÌÅ͸¦ ¹öÆÛ¿¡ ¹Þ¾Æ ¿Â´Ù.

1
2
3
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 
String srt = in.readLine();
cs

 
À§¿Í °°ÀÌ readLine() ÇÔ¼ö¸¦ »ç¿ëÇϸé \r\nÀ» ÀνÄÇϸç ÇÑ ÁÙÀ» Àоî¿Â´Ù. ScannerÀÇ nextLine() ÇÔ¼ö¿Í À¯»çÇÏ°Ô µ¿ÀÛÇÑ´Ù.

¹®ÀÚ Çϳª¸¸ ÀÐ°í ½ÍÀ» ¶§´Â ¹æ¹ý2ÀÇ System.in.read()ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù.



Ãâó: http://luyin.tistory.com/417 [Luyin]