[자바JAVA]1011 : [기초-입출력] 문자 한 개 입력받아 그대로 출력하기

💡 핵심 요약 (TL;DR)

'1011 : [기초-입출력] 문자 한 개 입력받아 그대로 출력하기' 알고리즘 문제의 요구사항을 분석하고 최적의 자료구조와 연산 방식을 적용해 구현한 풀이다. > Scanner class in Java supports nextInt(), nextLong(), nextDouble() etc. But there is no nextChar()을 확인했다.

문제 1011 : [기초-입출력] 문자 한 개 입력받아 그대로 출력하기

문자형(char)으로 변수를 하나 선언하고, 변수에 문자를 저장한 후 변수에 저장되어 있는 문자를 그대로 출력해보자.

코드

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;

class Main {
public static void main(String[] args) {
char x;
Scanner sc = new Scanner(System.in);
x = sc.next().charAt(0);
sc.close(); //효율을 위해 꼭 자원해제
System.out.print(x);
}
}

배운 지식

Scanner class in Java supports nextInt(), nextLong(), nextDouble() etc. But there is no nextChar()

  • To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
  • To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
  • To read strings, we use nextLine().
  • To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.
    출처 : https://www.geeksforgeeks.org/scanner-class-in-java/

다른 문제 풀이가 보고싶다면?

#Codeup100

[자바JAVA]1011 : [기초-입출력] 문자 한 개 입력받아 그대로 출력하기

https://sowon-dev.github.io/2020/09/28/200930al-1/

Author

sowon-dev

Posted on

2020-09-29

Updated on

2026-10-13

Licensed under

CC BY 4.0

댓글