June is Combung
문자열 숫자검사 & 단어 검색 본문
package week3;
import java.util.Scanner;
public class Day12_6 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// char test = '0';
// char test2 = '9'; //1byte 0 -256
// int num = (int)test;
char num[] = new char[10];
int code = 48; // 48 == '0'
for (int i = 0; i < num.length; i++) {
num[i] = (char) code;
code++;
System.out.print(num[i] + " ");
}
System.out.println();
// 문제 1
/*
* # 문자열 속 숫자검사 예) adklajsiod 문자만 있다. 예) 123123 숫자만 있다. 예) dasd12312asd 문자와 숫자가
* 섞여있다.
*/
System.out.print("입력 : ");
String text = scan.next();
int cnt = 0;
for (int i = 0; i < num.length; i++) {
for (int k = 0; k < text.length(); k++) {
if (num[i] == text.charAt(k)) {
cnt++;
}
}
}
System.out.println();
System.out.println("text = " + text);
if (cnt == 0) {
System.out.println("문자만 있다 ");
} else if (cnt == text.length()) {
System.out.println("숫자만 있다 ");
} else {
System.out.println("문자와 숫자가 섞여있다 ");
}
// 문제 2
/*
* # 단어 검색 1. 단어를 입력받아 text변수 문장 속에 해당 단어가 있는지 검색한다. 2. 단어가 존재하면 true 단어가 없으면
* false를 출력한다.
*/
String text1 = "Life is too short.";
System.out.println(text1);
System.out.print("검색할 단어를 입력하세요 : ");
String word = scan.next();
// char[] arr = new char[text1.length()];
String[] wordData = text1.split(" ");
int check = 0;
for (int i = 0; i < wordData.length; i++) {
if (i == wordData.length - 1) {
String[] temp = wordData[wordData.length - 1].split(".");
if (word.equalsIgnoreCase(temp[0])) {
check = 1;
}
}
if (word.equalsIgnoreCase(wordData[i])) {
check = 1;
}
}
if (check == 1) {
System.out.print("단어있음");
} else {
System.out.print("단어없음");
}
/*
String text1 = "Life is too short.";
* 자바에서 split(.) 에서 . 이 안되는 이유 정규표현식을 사용하는데 거기서 . 이 표현식에서 불특정한 문자 한개를 의미하기 때문에
* 안된다 그래서 그 특수한 표현 식을 없애주기 위해서 \ 역슬래쉬를 쓰면 n 과같은 평범한 문자가 \n 특별한 기능을 가지게 되고 . 처럼
* 특수한 기능이 있는 애는 특수한 기능을 없애준다 특수한 기능을 없애줄려면 \. -> .은 특별한 기능이야 를 먼저 호출한 뒤에 그 기능을
* 다시 \을 붙여서 없애주면 기능이 사라진다 그래서 \\. 이렇게 사용해야함
*/
/*
System.out.println(text1);
String[] test = text1.split("\\.");
System.out.println(test.length);
System.out.print("검색할 단어를 입력하세요 : ");
String word = scan.next();
String[] wordData = test[0].split(" ");
int check = 0;
for (int i = 0; i < wordData.length; i++) {
if (word.equalsIgnoreCase(wordData[i])) {
check = 1;
}
}
if (check == 1) {
System.out.print("단어있음");
} else {
System.out.print("단어없음");
}
*/
}
}
Comments