0
스캐너와 파일 판독기를 사용하여 파일을 읽으려고하고 있는데 코드가 HashBiMap에 문자를 넣지 않는 이유를 모르겠습니다.파일을 HashBiMap에 읽는 방법
public class MappedCharCoder implements CharCoder {
private HashBiMap<Character, Character> codeKey = HashBiMap.create(26);
/**
* The constructor takes a FQFN and reads from the file placing the contents
* into a HashBiMap for the purpose of encryption and decryption.
*
* @param map
* The variable map is the file name
* @throws FileNotFoundException
* If the file does not exist an exception will be thrown
*/
public MappedCharCoder(String map) throws FileNotFoundException {
HashBiMap<Character, Character> code = this.codeKey;
FileReader reader = new FileReader(map);
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
int x = 0;
int y = 1;
String cmd = scanner.next();
cmd.split(",");
char[] charArray = cmd.toCharArray();
char key = charArray[x];
if (code.containsKey(key)) {
throw new IllegalArgumentException(
"There are duplicate keys in the map.");
}
char value = charArray[y];
if (code.containsValue(value)) {
throw new IllegalArgumentException(
"There are duplicate values in the map.");
}
code.forcePut(key, value);
x+=2;
y+=2;
}
scanner.close();
if (code.size() > 26) {
throw new IllegalStateException(
"The hashBiMap has more than 26 elements.");
}
}
@Override
public char encode(char charToEncode) {
char encodedChar = ' ';
if (this.codeKey.containsKey(charToEncode)) {
encodedChar = this.codeKey.get(charToEncode);
return encodedChar;
} else {
return charToEncode;
}
}
@Override
public char decode(char charToDecode) {
char decodedChar = ' ';
if (this.codeKey.containsValue(charToDecode)) {
this.codeKey.inverse();
decodedChar = this.codeKey.get(charToDecode);
return decodedChar;
} else {
return charToDecode;
}
}
}
코드에서 알 수 있듯이 대체 암호를 만들려고합니다. 너희들이 내가이 수업을 듣는 것을 도울 수 있다면 잘하면 다른 수업을 듣게 될거야. 정말 고마워.
'cmd.split (",")'행에서 무엇을 기대합니까? (대답이 "아무것도 아님"이라면 좋음 : 기대치가 실제와 일치 함) –
응답 해 주셔서 감사합니다. 파일의 행은 A, K 등을 읽습니다. 첫 번째 문자는 쉼표로 구분 된 키와 값입니다. split()은 String을 두 개의 문자열로 분리한다고 생각했습니다. 이 문제를 해결하는 방법을 알려주시겠습니까? – Ashton
'cmd.split (",")'은'String' 배열을 반환합니다. 반환하는 배열에 아무 것도하지 않으면 아무 것도하지 않는 것과 같습니다. –