-1
앞에 위치한 문자를 얻으려면 어떻게예를 들어 다른 문자
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
는 이제 가정하자이 사용자는 abctd
에 들어갔다.
getCharcterBeforeT(example)
-이 부분에서 도움이 필요는
앞에 위치한 문자를 얻으려면 어떻게예를 들어 다른 문자
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
는 이제 가정하자이 사용자는 abctd
에 들어갔다.
getCharcterBeforeT(example)
-이 부분에서 도움이 필요는
이런 일에 대해 어떻게 :
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string:");
String intialString = scan.nextLine();
System.out.print("What is the character you would like to get the character before:");
String character = "";
while(true){
character = scan.nextLine();
if(character.length()==1)
break;
else
System.out.print("Please enter only 1 character:");
}
System.out.println(getCharcterBeforeT(intialString, character.charAt(0)));
}
public static char getCharcterBeforeT(String str, char c){
char returnChar = ' ';
if (str.indexOf(c) == -1){
System.out.println("Character '" + c + "' not found");
} else if (str.indexOf(c) == 0){
System.out.println("Character '" + c + "' is at start of string");
} else {
returnChar = str.charAt(str.indexOf(c) - 1);
}
return returnChar;
}
}
콘솔 :
Enter a string: abctd
What is the character you would like to get the character before: a
Character 'a' is at start of string
Enter a string: abctd
What is the character you would like to get the character before: b
a
Enter a string: abctd
What is the character you would like to get the character before: q
Character 'q' not found
그것을 here!
시도