210914 - 백준 11654

letsbebrave·2021년 9월 19일
0

codingtest

목록 보기
1/146
post-thumbnail

코딩테스트가 알고 보니 IDE와 같은 것들을 못 쓰고 단축키도 못 쓴다는 것을 알게 되었다. 그래서 기존에 해왔던 이클립스로 푸는 방식을 바꿔서 백준이나 프로그래머스와 같은 사이트에서만 풀어야겠다.

그 첫번째 문제로

[백준 11654]

처음 작성했던 코드

import java.util.*;

public class Main{
    
    Scanner sc = new Scanner(System.in);
    
    String word = sc.nextString();
    Int num = word.parseInt();
    
    System.out.println(num);
    
    
}

문제점

  1. String을 Scanner로 입력받을 땐
  • next(); - 공백문자까지 입력받음
  • nextLine(); - 줄 단위로 입력받음
  1. Int가 아니라 int임;;
  2. String을 int로 변환할 때
  • String -> int : Integer.parseInt(str);
  • int -> String : Integer.toString(int);

공부할 개념

  1. charAt(i) -> String문자열의 i번째 문자를 char형으로 바꿔줌
  2. char형 문자를 int 변수로 저장하여 ASCII코드를 사용

수정된 코드

import java.util.Scanner;

public class Main {
	
  public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);

      String word = sc.next();

      int num = word.charAt(0);
      
      System.out.println(num);


  }
	    

}
profile
그게, 할 수 있다고 믿어야 해

0개의 댓글