[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() : ์—”ํ„ฐ ์ „๊นŒ์ง€ ์ฝ์–ด๊ฐ

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ