[Java] Å°º¸µå ÀԷ¹޴ ¹æ¹ý
Java¿¡¼ Å°º¸µå ¹®ÀÚ ÀÔ·Â ¹æ¹ý¿¡ ´ëÇؼ Á¤¸®Çغ¾´Ï´Ù.
C¾ð¾î·Î À̾߱â ÇÏÀÚ¸é scanf() ¿¡ ÇØ´çÇÒ ¼ö ÀÖ½À´Ï´Ù.
ÃÑ 3°¡Áö ÀÖÀ¸¸ç °á·Ð ºÎÅÍ À̾߱â ÇÑ´Ù¸é,
¹æ¹ý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 ÀÔ·Â ¿¹¿Üó¸®
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 °´Ã¼ »ç¿ë
1 2 3 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String srt = in.readLine(); | cs |
Ãâó: http://luyin.tistory.com/417 [Luyin]