์๋
! ์ค๋ ์ฐ๋ฆฌ์ ์์
์ ํน๋ณํ ๊ฒ์
๋๋ค! ์ค๋ ์์
์์๋ Java ์ค์บ๋ ํด๋์ค์ ๋ํด ์ด์ผ๊ธฐํ๊ฒ ์ต๋๋ค. ์ด์ ์๋ ์์
์ ์๋ฃํ๊ณ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ ๊ณผ์ ์ด ๊ฐ๋จํ์ต๋๋ค. ์ฝ๋๋ฅผ ์์ฑํ๊ณ main ()๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ํ๋ก๊ทธ๋จ์ด ํ์ํ ์์
์ ์ํํ๊ณ ์์
์ด ์๋ฃ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด์ ๋ชจ๋ ๊ฒ์ด ๋ฐ๋ ๊ฒ์
๋๋ค! ์ค๋ ์ฐ๋ฆฌ๋ ํ๋ก๊ทธ๋จ๊ณผ ์ค์ ๋ก ์ํธ ์์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ๊ฒ์
๋๋ค. ์ฐ๋ฆฌ์ ํ๋์ ๋ฐ์ํ๋ ๋ฐฉ๋ฒ์ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฅด์น ๊ฒ์
๋๋ค! ์ฝ๋ ๋ถ์์ ์์ํ๊ธฐ ์ ์ ์ค์บ๋์ ๊ฐ์ ์ฅ์น๋ฅผ ๋ค๋ฃจ์ด์ผ ํ๋ ์ ์ด ์์ต๋๊น? ์๋ง. ์ค์บ๋์ ๋ด๋ถ๋ ๋งค์ฐ ๋ณต์กํ์ง๋ง ์๋ ๋ฐฉ์์ ๋ํ ๊ธฐ๋ณธ ์์ด๋์ด๋ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ๋ฐ์ดํฐ(์: ์ฌ๊ถ ๋๋ ๋ณดํ ์ฆ์)๋ฅผ ์ฝ๊ณ ์ด ์ ๋ณด๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํฉ๋๋ค(์: ์ด๋ฏธ์ง๋ก). ). ์ค๋์ ๋๋ง์ ์ค์บ๋๋ฅผ ๋ง๋ค ๊ฒ์
๋๋ค! ๋ฌผ๋ก ์ข
์ด ๋ฌธ์๋ ์ฒ๋ฆฌํ ์ ์์ง๋ง ํ
์คํธ๋ ๋ฌธ์ ์์ต๋๋ค :) Let's go!
์๋ฐ์ ์ค์บ๋ ํด๋์ค
๋ฌด์๋ณด๋ค ๋จผ์ java.util.Scanner ํด๋์ค ์ ์ต์ํด์ ธ์ผ ํฉ๋๋ค . ๊ทธ ๊ธฐ๋ฅ์ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์ค์ ์ค์บ๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ง์ ํ ์์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ฌธ์์ด, ํ์ผ, ์ฝ์์ ๋๋ค. ๋ค์์ผ๋ก ์ ๋ณด๋ฅผ ์ธ์ํ๊ณ ์ ์ ํ๊ฒ ์ฒ๋ฆฌํฉ๋๋ค. ๊ฐ์ฅ ๊ฐ๋จํ ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("It matters not how strait the gate,\n" +
"How charged with punishments the scroll,\n" +
"I am the master of my fate,\n" +
"I am the captain of my soul");
String s = scanner.nextLine();
System.out.println(s);
}
}
์ค์บ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฐ์ดํฐ ์์ค(ํ
์คํธ ๋ฌธ์์ด)๋ฅผ ์ง์ ํ์ต๋๋ค. nextLine () ๋ฉ์๋๋ ๋ฐ์ดํฐ ์์ค(4ํ๋ ฌ์ด ์๋ ํ
์คํธ)์ ์ก์ธ์คํ๊ณ ์ฝ์ง ์์ ๋ค์ ์ค(์ด ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์ค)์ ์ฐพ์ ๋ฐํํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ฝ์์ ํ์ํฉ๋๋ค. ์ฝ์ ์ถ๋ ฅ:
It matters not how strait the gate,
nextLine() ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ์ฌ ์ ์ฒด ์ ๋ฐ์ท๋ฌธ์ ํ์ํ ์ ์์ต๋๋ค.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("It matters not how strait the gate,\n" +
"How charged with punishments the scroll,\n" +
"I am the master of my fate,\n" +
"I am the captain of my soul");
String s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
}
}
๋งค๋ฒ ์ฐ๋ฆฌ ์ค์บ๋๋ ํ ๊ฑธ์ ์์ผ๋ก ๋์๊ฐ ๋ค์ ์ค์ ์ฝ์ต๋๋ค. ํ๋ก๊ทธ๋จ์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
It matters not how strait the gate,
How charged with punishments the scroll,
I am the master of my fate,
I am the captain of my soul
์ด๋ฏธ ๋งํ๋ฏ์ด ์ค์บ๋์ ๋ฐ์ดํฐ ์์ค๋ ๋ฌธ์์ด์ผ ํ์๊ฐ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ฝ์์ด ๋ ์ ์์ต๋๋ค. ๋ช ๊ฐ์ง ํฅ๋ฏธ๋ก์ด ์์์ด ์์ต๋๋ค. ์ด์ ์๋ ๋ฐ์ดํฐ๋ฅผ ํ์ํ๊ธฐ๋ง ํ์ง๋ง ์ด์ ๋ ํค๋ณด๋์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ต๋๋ค! ์ค์บ๋ ํด๋์ค๊ฐ ์ํํ๋ ๋ค๋ฅธ ์์
์ ์ดํด๋ณด๊ฒ ์ต๋๋ค .
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int number = sc.nextInt();
System.out.println("Thanks! You entered the number " + number);
}
}
nextInt () ๋ฉ์๋๋ ์
๋ ฅ๋ ์ซ์๋ฅผ ์ฝ๊ณ ๋ฐํํฉ๋๋ค. ์ฐ๋ฆฌ ํ๋ก๊ทธ๋จ์์๋ ์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์ ๋ฒํธ ์ ๊ฐ์ ํ ๋นํฉ๋๋ค . ์ด๋ฏธ ์ค์ ์ค์บ๋์ ๊ฐ์ต๋๋ค! ํ๋ก๊ทธ๋จ์ ์ฌ์ฉ์์๊ฒ ์ซ์๋ฅผ ์
๋ ฅํ๋๋ก ์์ฒญํฉ๋๋ค. ์ฌ์ฉ์๊ฐ ์ด ์์
์ ์ํํ ํ ํ๋ก๊ทธ๋จ์ ์ฌ์ฉ์์๊ฒ ๊ฐ์ฌ๋ฅผ ํ์ํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ ๋ค์ ์ข
๋ฃํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ ์ฌ๊ฐํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ฌ์ฉ์๊ฐ ์ค์๋ฅผ ํ๊ณ ์๋ชป๋ ๊ฒ์ ์
๋ ฅํ ์ ์์ต๋๋ค. ๋ค์์ ํ์ฌ ํ๋ก๊ทธ๋จ์ด ์๋์ ๋ฉ์ถ๋ ์์
๋๋ค.
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int number = sc.nextInt();
System.out.println("Thanks! You entered the number " + number);
}
}
์ซ์ ๋์ ๋ฌธ์์ด "CodeGym"์ ์
๋ ฅํด ๋ณด๊ฒ ์ต๋๋ค. ์ฝ์ ์ถ๋ ฅ:
Enter a number:
CodeGym
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:10) Process finished with exit code 1
์ด ์ค. ํฐ์ผ ๋ฌ์ต๋๋ค -_- ์ด๋ฐ ์ํฉ์ ํผํ๊ธฐ ์ํด์๋ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ ์ ์๋ ๋ฐฉ๋ฒ์ด ํ์ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ์ฌ์ฉ์๊ฐ ์ซ์ ์ด์ธ์ ๊ฒ์ ์
๋ ฅํ๋ฉด ์
๋ ฅํ ์ ๋ณด๊ฐ ์ซ์๊ฐ ์๋๋ผ๋ ๊ฒฝ๊ณ ๋ฅผ ํ์ํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ ๋ณด๊ฐ ๊ด์ฐฎ๋ค๋ฉด ํ์ธํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ ์ฐ๋ฆฌ์ ํ๋ฆ์ ๋ฌด์์ด ์ฌ์ง ๋ณด๊ธฐ ์ํด "๋ฏธ๋๋ฅผ ๋ด๋ค๋ณด์์ผ" ํฉ๋๋ค. ์ค์บ๋๊ฐ ์ด ์์
์ ์ํ ํ ์ ์์ต๋๊น ? ๊ทธ๋ฆฌ๊ณ ์ด๋ป๊ฒ! ๊ทธ๋ฆฌ๊ณ ์ด๋ฅผ ์ํํ๊ธฐ ์ํ ๋ง์ ๋ฉ์๋๊ฐ ์์ต๋๋ค. hasNextInt() โ ์ด ๋ฉ์๋๋ ์
๋ ฅ ๋ฐ์ดํฐ์ ๋ค์ ์ฒญํฌ๊ฐ ์ซ์์ธ์ง ํ์ธํฉ๋๋ค(์ ์ ํ๊ฒ true ๋๋ false ๋ฐํ). hasNextLine() โ ์ด ๋ฉ์๋๋ ๋ค์ ์
๋ ฅ ์ฒญํฌ๊ฐ ๋ฌธ์์ด์ธ์ง ํ์ธํฉ๋๋ค. hasNextByte() ,hasNextShort() , hasNextLong() , hasNextFloat() , hasNextDouble() โ ์ด ๋ชจ๋ ๋ฉ์๋๋ ๋๋จธ์ง ๋ฐ์ดํฐ ์ ํ์ ๋ํด ์ ์ฌํ ๊ฒ์ฌ๋ฅผ ์ํํฉ๋๋ค. ์ซ์ ์ฝ๊ธฐ ํ๋ก๊ทธ๋จ์ ๋ณ๊ฒฝํด ๋ด
์๋ค.
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
if (sc.hasNextInt()) {
int number = sc.nextInt();
System.out.println("Thanks! You entered the number " + number);
} else {
System.out.println("Sorry, but this is clearly not a number. Restart the program and try again!");
}
}
}
์ด์ ์ฐ๋ฆฌ ํ๋ก๊ทธ๋จ์ ์
๋ ฅ๋ ๋ค์ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ํ์ธํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ๋ด ๊ฒฝ์ฐ์๋ง ํ์ธ์ ํ์ํฉ๋๋ค. ์
๋ ฅ์ด ๊ฒ์ฌ๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ฉด ํ๋ก๊ทธ๋จ์ ๊ธฐ๋กํ๊ณ ์ฌ์ฉ์์๊ฒ ๋ค์ ์๋ํ๋๋ก ์์ฒญํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์บ๋ ๊ฐ์ฒด์ ํต์ ํ์ฌ ์ด๋ค ๋ฐ์ดํฐ ์ ํ์ด ๊ธฐ๋ค๋ฆฌ๊ณ ์๋์ง ๋ฏธ๋ฆฌ ํ์ธํ ์ ์์ต๋๋ค. ์ซ์, ๋ฌธ์์ด ๋๋ ๋ค๋ฅธ ๊ฒ? ์ซ์? ๊ทธ๋ฆฌ๊ณ ์ด๋ค ์ข
๋ฅ? int , short , long ?" ์ด ์ ์ฐ์ฑ์ ์ฌ์ฉ์์ ํ๋์ ๋ฐ๋ผ ํ๋ก๊ทธ๋จ ๋ก์ง์ ๊ตฌ์ถํ ์ ์๋ ๊ธฐํ๋ฅผ ์ ๊ณตํฉ๋๋ค. ๋ ๋ค๋ฅธ ์ค์ํ ๋ฉ์๋์ธ useDelimiter() ๋ฅผ ๊ธฐ๋กํด ๋์ด์ผ ํฉ๋๋ค. ์ด ๋ฉ์๋์ ๋ฌธ์์ด์ ์ ๋ฌํฉ๋๋ค. ๋ฌธ์์ด์๋ ๋ค์์ด ํฌํจ๋ฉ๋๋ค. ๊ตฌ๋ถ ๊ธฐํธ ๋๋ ๊ตฌ๋ถ ๊ธฐํธ๋ก ์ฌ์ฉํ๋ ค๋ ๋ฌธ์. ์๋ฅผ ๋ค์ด, ์ฐ๋ฆฌ๊ฐ ๊ฐ์๊ธฐ ์ผ๋ณธ ์์ ๊ด์ฌ์ ๊ฐ๊ฒ ๋์๊ณ ์๋ํ ์์ธ Matsuo Bashล๊ฐ ์ด ํ์ด์ฟ ๋ฅผ ์ฝ๊ธฐ ์ํด ์ค์บ๋๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ์ธ ๊ฐ์ ๋ค๋ฅธ ๊ตฌ์ ์ด ํ๋์ ์ด์ํ ๋ฌธ์์ด๋ก ์ ๋ฌ๋๋๋ผ๋ ์ฝ๊ฒ ๋ถํ ํ์ฌ ์๋ฆ๋ต๊ฒ ๋ ๋๋งํ ์ ์์ต๋๋ค.
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner("On a withered branch'" +
"A crow has alighted.'" +
"Nightfall in autumn." +
"''***''" +
"Such a moon above,'" +
"Like a tree cut at the root:'" +
"he fresh cut is white." +
"''***''" +
"How the river floods!'" +
"A heron wanders on short legs,'" +
"Knee-deep in the water.");
scan.useDelimiter("'");
while (scan.hasNext()) {
System.out.println(scan.next());
}
scan.close();
}
}
๊ตฌ๋ถ ๊ธฐํธ๋ก "\ n /*/*/*" (๊ฐํ ๋ฌธ์ ๋ฐ ์ธ ๊ฐ์ ๋ณํ)๋ฅผ ์ฌ์ฉํฉ๋๋ค . ๊ฒฐ๊ณผ์ ์ผ๋ก ์ฑ
์์์ ๊ฐ์ด ์๋ฆ๋ค์ด ์ฝ์ ์ถ๋ ฅ์ ์ป์ ์ ์์ต๋๋ค.
On a withered branch
A crow has alighted.
Nightfall in autumn.
***
Such a moon above,
Like a tree cut at the root:
The fresh cut is white.
***
How the river floods!
A heron wanders on short legs,
Knee-deep in the water.
์ด ์์ ์๋ ์ฐ๋ฆฌ๊ฐ ์ ๋์ ์ผ๋ก ์ง์ ํด์ผ ํ ๋ฉ์๋๊ฐ ํ๋ ๋ ์์ต๋๋ค: close() . I/O ์คํธ๋ฆผ์ผ๋ก ์์
ํ๋ ๋ชจ๋ ๊ฐ์ฒด์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ค์บ๋๋ ์์
์ด ์๋ฃ๋๋ฉด ๋ซ์์ผ ์ปดํจํฐ ๋ฆฌ์์ค๋ฅผ ๋ ์ด์ ์ฌ์ฉํ์ง ์์ต๋๋ค. close() ๋ฉ์๋๋ฅผ ์์ง ๋ง์ธ์ !
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int number = sc.nextInt();
System.out.println("Thanks! You entered the number " + number);
sc.close(); // Now we've done everything right!
}
}
๊ทธ๊ฒ ๋ค์ผ! ๋ณด์๋ค์ํผ ์ค์บ๋ ํด๋์ค๋ ๋งค์ฐ ์ ์ฉํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ๊ธฐ๊ฐ ๋งค์ฐ ์ฝ์ต๋๋ค! :) ๋ฐฐ์ด ๋ด์ฉ์ ๋ณด๊ฐํ๋ ค๋ฉด Java ๊ณผ์ ์์ ๋น๋์ค ๊ฐ์๋ฅผ ์์ฒญํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๋ ์ฝ์ด๋ณด๊ธฐ: |
---|
GO TO FULL VERSION