지식인에 올라온 질문인데..

주말 퇴근시간 전이라 걍 스킵..

그러다가 주말 쉬고 회사와서 심심풀이로 만들어 봤다.

 

- 특징

1) 문자와 문자열을 검색해 준다. 다만 완전히 일치할 경우에만 파인드가 가능하다.

2) 텍스트 파일에서 문자열을 읽어와서 해당 문자열이 있는 열을 표시해 준다.

3) 검색한 단어는 ^~~~^  이런식으로 표시 했다.

 

- 문제점

1) 한개의 열에 여러개의 검색결과가 있으면 같은 라인을 여러개 표시한다.(금방 고치겠지만.. 귀찮다.)

2) 자소 단위로 검색하기 때문에 효율이 떨어진다.(추후 compare나 index, 정규식 등을 이용해서 만들어 볼 예정..)

3) 문자열일 경우 완전히 같은 문자열만 검색이 된다.

4) 그 외 다수..

 

 

1. CLASS

 

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

 

public class t22_003 {
  private static ArrayList<String> lineinList;
  private static Scanner scIn;
  private static Scanner sc;
 
  public static void main(String[] args) throws IOException, NumberFormatException { 
    File fileIn = new File("D:\\work\\test\\test00.txt"); // 읽을 파일과 파일경로.
  
    scIn = new Scanner(fileIn);
    sc = new Scanner(System.in);
    lineinList = new ArrayList<String>();

 

    System.out.print("검색할 단어를 입력 : ");
    String searchM = sc.nextLine();
  
    int countM = searchM.length(); // 입력받은 문자열의 문자개수.
    int lineCount = 0; // 전체 문자열의 라인 수. 

    while (scIn.hasNextLine()) { // 라인별 문자열을 ArrayList에 입력.
      lineinList.add(scIn.nextLine());
      lineCount++;
    }

 

    for(int ai = 0; ai < lineCount; ai++){ // 읽어 온 텍스트의 라인별로 문자열의 문자 비교. 
      String input = lineinList.get(ai); // 각각의 리스트에서 문자열을 라인별로 받아온다. 
      int count = 0; // 라인별로 비교한 문자열의 문자개수.      
      char searchC = 0; // 입력받은 문자열.
      char textC;  // 텍스트의 문자열.
      int k = 0; // 입력받은 문자단위 카운트.
      int subCountS = 0;
      int subCountE = 0;
 
      for(int i = 0; i < input.length(); i++){
        searchC = searchM.charAt(k);
        textC = input.charAt(i);
 
        if(searchC == textC){
          count++;
          k++;
        }else{
          count = 0;
          k = 0;
        }

 

        if(count == countM){
          k = 0;
          count = 0;
        
          if((i + 1) == countM){
            subCountS = i;
          }else{
            subCountS = (i + 1) - countM;
          }
          subCountE = i + 1;


          System.out.printf("%3d. %s^%s^%s\n", ai, input.substring(0, subCountS), searchM, input.substring(subCountE));

        } 
      }
    }
  }
}

 

 

2. RUN

 

검색할 단어를 입력 : company launched
  6. the year that the ^company launched^ The Fantastic Four and other superhero titles

 

 

검색할 단어를 입력 : and
  0. Marvel Comics is the common name ^and^ primary imprint of Marvel Worldwide Inc.,
  1. formerly Marvel Publishing, Inc. ^and^ Marvel Comics Group, is an American publisher
  2. of comic books ^and^ related media. In 2009, The Walt Disney Company acquired
  4. Marvel started in 1939 as Timely Publications, ^and^ by the early 1950s had generally
  6. the year that the company launched The Fantastic Four ^and^ other superhero titles
  7. created by Stan Lee, Jack Kirby, Steve Ditko ^and^ many others.
 10. the Avengers, the Guardians of the Galaxy, the Fantastic Four, the X-Men ^and^
 12. Ultron, Doctor Octopus, Thanos, Magneto ^and^ Loki. Most of Marvel's fictional
 15. Characters such as Spider-Man, the Fantastic Four, the Avengers, Daredevil ^and^
 17. been based in Salem Center, New York ^and^ Hulk's stories often have been

 

 

검색할 단어를 입력 : company
  3. Marvel Entertainment, Marvel Worldwide's parent ^company^.
  6. the year that the ^company^ launched The Fantastic Four and other superhero titles

 

 

3. ORIGINAL[TEXT]

 

Marvel Comics is the common name and primary imprint of Marvel Worldwide Inc.,
formerly Marvel Publishing, Inc. and Marvel Comics Group, is an American publisher
of comic books and related media. In 2009, The Walt Disney Company acquired
Marvel Entertainment, Marvel Worldwide's parent company.
Marvel started in 1939 as Timely Publications, and by the early 1950s had generally
become known as Atlas Comics. Marvel's modern incarnation dates from 1961,
the year that the company launched The Fantastic Four and other superhero titles
created by Stan Lee, Jack Kirby, Steve Ditko and many others.
Marvel counts among its characters such well-known superheroes such as Captain
America, Spider-Man, Thor, Hulk, Iron Man, Wolverine, Ant-Man, such teams as
the Avengers, the Guardians of the Galaxy, the Fantastic Four, the X-Men and
the Inhumans, with antagonists such as Doctor Doom, Red Skull, Green Goblin,
Ultron, Doctor Octopus, Thanos, Magneto and Loki. Most of Marvel's fictional
characters operate in a single reality known as the Marvel Universe, with locations
that mirror real-life cities.
Characters such as Spider-Man, the Fantastic Four, the Avengers, Daredevil and
Doctor Strange are based in New York City whereas the X-Men have historically
been based in Salem Center, New York and Hulk's stories often have been
set in the American Southwest.

 

 

 

모 대충 이런모양...

 

위의 검색기능만 봤을때는 ArrayList같은 배열과 산만한 변수들은 굳이 사용할 필요는 없지만..

원래 다른 여러가지 기능을 넣기위해 고려한 결과였다.

하지만 도중에 업무요청이 있어 처리하고 나니까 급 하기가 싫어져서.. 여기저기 널부러진 다른 기능부분들은 실제 작동을 위한 부분만 제외하고 전부 삭제했다.

 

시간이 나면 알고리즘도 구상해서 넣어보고, 여러모로 손대보겠지만..

아직 로또 생성기도 안하고 있는데.. 이건 언제쯤 해보려나..