[Day6] Java 6 [8/16]
1. String ์ธ๋ฑ์ค ์ ๊ทผ
์ ๋ ฅ๋ฐ์ String์์ char ์ป์ด์์ ๊ฐ ํ์ธ
char one;
Scanner scanner = new Scanner(System.in);
System.out.print("> ํ ๋ฌธ์๋ฅผ ์
๋ ฅ ? ");
String strOne = scanner.next(); // "A" -> 'A' ์ํ๋ ์์น์ ํ ๋ฌธ์๋ฅผ ์ป์ด์ค๋ ์์
.
one = strOne.charAt( 0 );
if( ( one >= 'A' && one <= 'Z') || ( one >= 'a' && one <= 'z' ) ) {
System.out.println("์ํ๋ฒณ");
}else if( one >= '๊ฐ' && one <= 'ํฃ' ) { // X
System.out.println("ํ๊ธ");
}else if( one >= '0' && one <= '9' ) {
System.out.println("์ซ์");
}else if( one == '#' || one == '@' || one == '%' || one == '!') { // #, @,%, !
System.out.println("ํน์๋ฌธ์");
}else {
System.out.println("๊ธฐํ...");
}
๋ํผํด๋์ค ํ์ฉํ์ฌ ๊ฐ๋จํ๊ฒ ํํ
char one;
Scanner scanner = new Scanner(System.in);
System.out.print("> ํ ๋ฌธ์๋ฅผ ์
๋ ฅ ? ");
String strOne = scanner.next();
one = strOne.charAt( 0 );
if( Character.isAlphabetic( one ) ) {
System.out.println("์ํ๋ฒณ");
}else if( one >= '๊ฐ' && one <= 'ํฃ' ) { // ์ ๊ทํํ์ [๊ฐ-ํฃ]
System.out.println("ํ๊ธ");
}else if( Character.isDigit( one ) ) {
System.out.println("์ซ์");
}else if( one == '#' || one == '@' || one == '%' || one == '!') { // ์ ๊ทํํ์ [#@%!]
System.out.println("ํน์๋ฌธ์");
}else {
System.out.println("๊ธฐํ...");
}
[ ๋ฌธ์์ด ํจ์ 3๊ฐ์ง]
1. ๋ฌธ์์ด ๊ธธ์ด : int length()
2. ๋ฌธ์์ด ์๋ผ๋ด๊ธฐ : String [] split("๊ตฌ๋ถ์")
3. ๊ณต๋ฐฑ ์ ๊ฑฐ : String trim()
4. ํ๋ฌธ์ : char charAt(int index)
2. ์ฝ๋
1) ํ๊ธ ์ ๋์ฝ๋ ์ถ๋ ฅ
for (int i = '๊ฐ'; i <= 'ํฃ' ; i++) {
System.out.printf("%1$d - [%1$c]\n", i);
}
$ : ํฌ๋งท ๊ฐ๊ฐ ์ ์ฉ
2) ์์คํค ์ฝ๋
// ASCII ์ฝ๋ ๋ชจ๋ ์ถ๋ ฅ. 7๋นํธ(128๊ฐ์ง), 8๋นํธ(256๊ฐ์ง 0 ~ 255 ์ฝ๋)
// 0 ~ 31 ์ ์ด๋ฌธ์
// 32 ~ 127 ์ผ๋ฐ๋ฌธ์ 65~90 ๋๋ฌธ์, 97~122 ์๋ฌธ์, 48~57 ์ซ์
// 128~ 255 ์ฌ์ ๋ฌธ์
for (int i = 0; i <= 255; i++) {
System.out.printf("%d[%c]\n", i, i );
}
+ ์ ๋ ฅ
System.in.read() : https://devyoseph.tistory.com/141
-> ์ํฐ๊น์ง ์์คํค์ฝ๋๋ก ์ ๋ ฅ๋จ
System.in.read() ์ Scanner์ ์ฐจ์ด : https://ranggun.tistory.com/63
nextLine์ง์ ์ scan.nextLine()์ ํ๋ฒ ๋์จ์ ์ ๋ ฅ๋ฒํผ๋ฅผ ๋น์์ค๋ค.
int input = sc.nextInt();
sc.nextLine(); // ์
๋ ฅ๋ฒํผ๋ฅผ ๋น์์ฃผ๋ ์ญํ ์ ํ๋ค.
System.out.println("์ด๋ฆ>");
name =sc.nextLine();
* System.in.read ๋ฉ์ถ๊ธฐ
* System.in.skip(system.in.available()); ์ ๋ ฅ ์คํธ๋ฆผ ๋ชจ๋ ์ ๊ฑฐ
Next() : ๊ณต๋ฐฑ ์ ๊น์ง ์ฝ์ด๊ฐ
Nextline() : ์ํฐ ์ ๊น์ง ์ฝ์ด๊ฐ
'๐จโ๐ป Web Development > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day8] Java 8 - ์ ๊ทํํ์(regex), ๋ฉ์๋, ์ค๋ฒ๋ก๋ฉ, swap (0) | 2022.09.01 |
---|---|
[Day7] Java 7 - ๋ฌ๋ ฅ, ๊ตฌ๊ตฌ๋จ (0) | 2022.08.29 |
[Day5] Java 5 - switch๋ฌธ, Random class (0) | 2022.08.28 |
[Day4] Java 4 - ์ฐ์ฐ์, ๋ฐฐ์ด, ์ ์ด๋ฌธ (0) | 2022.08.23 |
[Day3] Java 3 - ํ๋ณํ, ์ฐ์ฐ์ (0) | 2022.08.23 |
์ต๊ทผ๋๊ธ