0
我讀Java中的文件,該文件包含以下內容:如何讀取Java中txt.file中的第一個字母?
.- ... ... .. --. -. -- . -. - ....-
.. ...
..-. .. -. .- .-.. .-.. -.--
-.. --- -. .
的事情是它讀取整個文件,除了從第一行的前兩個字符,只打印:
。 .. ... - 。 - 。 - 。 - 。 - ....-
,而不是
.- ...... - 。 - 。 - 。 - ....-
如何讀取此txt文件的第一個字符?
下面是代碼:
String line = "";
String []split;
Scanner scanner = new Scanner (System.in);
System.out.print("Input file:");
String inputFile = scanner.nextLine();
File file = new File(inputFile);
Scanner input = new Scanner(file);
String [] alphabet = {
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","X","Y","Z"
,"0","1","2","3","4","5","6","7","8","9"};
String [] morse = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
if(input.hasNext())
input.next();
while(input.hasNextLine()){
line = input.nextLine();
split = line.split("\t");
System.out.println(split[0]);
for (int i = 0; i < split.length; i++){
}
}
}}
'if(input.hasNext())input.next();'你爲什麼要這樣做?它讀取的第一個字符串是「.-」。 – rafid059
哦菜鳥的錯誤。那不是假設在那裏,謝謝。 – ryosshii