본문 바로가기
  • Adillete
【스터디노트】/▷TIL

코테 스터디 62일차 TIL + 오늘의 학습 키워드 단어의 개수

by 아딜렛 2025. 9. 10.


- 오늘의 학습 키워드
단어의 개수
- 공부한 내용 본인의 언어로 정리하기
trim(): 앞뒤 공백 제거하기

spilt("\\s+"): 하나 이상의 공백으로 분할(연속 공백 처리)

- 오늘의 회고
  - 어떤 문제가 있었고, 나는 어떤 시도를 했는지

sentence.split(" ")으로 나눈 값을 배열로 저장하려고 했음

그냥 stringtokenizer로 했으면 되었을거같음 st.countToken();

list 에 int로 받기 위해서 노력했음

    Scanner sc= new  Scanner(System.in);
    List<Integer> integerList1 =  Arrays.asList(sc.nextInt());
    List<Integer> integerList2 =  Arrays.asList(sc.nextInt());

    int one = parseInt(integerList1);

    if(integerList1-integerList2>=0){
      System.out.print(integerList1);
    }else (integerList1-integerList2<0){
      System.out.print(integerList2);
    }

두개의 차를 구해서 >0이면 앞에가 큰거고 를 만들려고 했으나 오류로 실패

 

 

 


  - 어떻게 해결했는지

방법1

public class 단어의_개수_정답 {
  public static void main(String[] args) {
    Scanner sc= new  Scanner(System.in);
    String sentence= sc.nextLine();
    StringTokenizer st = new StringTokenizer(sentence);
    System.out.println(st.countTokens());
  }
}

 

방법2 

    Scanner sc= new Scanner(System.in);
    String sentence =sc.nextLine();

    String[] words = sentence.trim().split("\\s+");
    if(sentence.trim().length()==0){
        System.out.println(0);
    }else{
    System.out.println(words.length);
    }

 

  public static void main(String[] args) {
    Scanner sc= new Scanner(System.in);
    String a= sc.next();
    String b= sc.next();

    int revA= Integer.parseInt(new StringBuilder(a).reverse().toString());
    int revB =Integer.parseInt(new StringBuilder(b).reverse().toString());

    System.out.println(Math.max(revA,revB));
    sc.close();
  }


  - 무엇을 새롭게 알았는지

split 으로 나누기

trim으로 나누기

StringBuilder를 사용해서 새로운 인스턴스를 생성

인스턴스의 생성과정 .append()를 통해 연결하고자 하는 문자열을 넣어서 StringBuilder의 객체를

호출해서 사용한다. 연결하고 문자열을 초기화하려고 할때 toString()메서드를 통해 할당한다.

- 오늘의 학습 키워드

단어의 개수

- 공부한 내용 본인의 언어로 정리하기

trim(): 앞뒤 공백 제거하기

 

spilt("\\s+"): 하나 이상의 공백으로 분할(연속 공백 처리)

 

- 오늘의 회고

  - 어떤 문제가 있었고, 나는 어떤 시도를 했는지

 

sentence.split(" ")으로 나눈 값을 배열로 저장하려고 했음

 

그냥 stringtokenizer로 했으면 되었을거같음 st.countToken();

 

list 에 int로 받기 위해서 노력했음

 

    Scanner sc= new Scanner(System.in);

    List<Integer> integerList1 = Arrays.asList(sc.nextInt());

    List<Integer> integerList2 = Arrays.asList(sc.nextInt());

 

    int one = parseInt(integerList1);

 

    if(integerList1-integerList2>=0){

      System.out.print(integerList1);

    }else (integerList1-integerList2<0){

      System.out.print(integerList2);

    }

두개의 차를 구해서 >0이면 앞에가 큰거고 를 만들려고 했으나 오류로 실패

 

 

 

 

 

 

 

 

  - 어떻게 해결했는지

 

방법1

 

public class 단어의_개수_정답 {

  public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    String sentence= sc.nextLine();

    StringTokenizer st = new StringTokenizer(sentence);

    System.out.println(st.countTokens());

  }

}

 

 

방법2 

 

    Scanner sc= new Scanner(System.in);

    String sentence =sc.nextLine();

 

    String[] words = sentence.trim().split("\\s+");

    if(sentence.trim().length()==0){

        System.out.println(0);

    }else{

    System.out.println(words.length);

    }

 

 

  public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    String a= sc.next();

    String b= sc.next();

 

    int revA= Integer.parseInt(new StringBuilder(a).reverse().toString());

    int revB =Integer.parseInt(new StringBuilder(b).reverse().toString());

 

    System.out.println(Math.max(revA,revB));

    sc.close();

  }

 

  - 무엇을 새롭게 알았는지

 

split 으로 나누기

 

trim으로 나누기

 

StringBuilder를 사용해서 문자열 ㅉ
  - 내일 학습할 것은 무엇인지

문자