✔️ 내 생각 -> runtime error
import java.util.*;
public class Day1 {
public static void main(String[] args){
Scanner input_=new Scanner(System.in);
Scanner in_= new Scanner(System.in);
String input=(input_.nextLine()).toLowerCase();
String in = (in_.nextLine()).toLowerCase();
String[] arr = input.split("");
int num =0;
for(int i=0;i<arr.length;i++){
if(in.equals(arr[i]))
num++;
}
System.out.println(num);
}
}
고치기
import java.util.*;
public class Day1 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String input=input.next().toLowerCase();
char in = Character.toLowerCase(input_.next().charAt(0));
char[] arr = input.toCharArray();
int num =0;
for(int i=0;i<arr.length;i++){
if(in==arr[i])
num++;
}
System.out.println(num);
}
}
✔️ 정답
import java.util.*;
class Main{
public int solution(String str, char t) {
int answer =0;
str=str.toLowerCase();
t=Character.toLowerCase(t);
/*for(int i=0;i<str.length();i++)
if(str.charAt(i)==t) answer++;*/
for(char x: str.toCharArray())
if(x==t) answer++;
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
String input = kb.next();
char in = kb.next().charAt(0);
System.out.println(Main.solution(input,in));
}
}
중요 포인트
Scanner kb = new Scanner(System.in);
String input = kb.next();
char in = kb.next().charAt(0);