私は次のコードを持っています。私は言語を学び始めたばかりであり、そのようなものはかなり簡単な演習を探していることを覚えておいてください。コーディングのエチケットと批評家は大歓迎です。
import java.util.*;
import java.io.*;
public class Tron
{
public static void main(String[] args) throws Exception
{
int x,z,y = 0;
File Tron= new File("C:\\Java\\wordtest.txt");
Scanner word = new Scanner(Tron);
HashMap<String, Integer> Collection = new HashMap<String, Integer>();
//noticed that hasNextLine and hasNext both work.....why one over the other?
while (word.hasNext())
{
String s = word.next();
Collection.get(s);
if (Collection.containsKey(s))
{
Integer n = Collection.get(s);
n = n+1;
Collection.put(s,n);
//why does n++ and n+1 give you different results
}else
{
Collection.put(s,1);
}
}
System.out.println(Collection);
}
}
を使用せずに、useDelimiter()
私が持っているファイルに基づいて希望の出力を取得します。
Far = 2, ran = 4, Frog = 2, Far = 7, fast = 1, etc...
useDelimiter
次のようにメソッドを挿入します
Scanner word = new Scanner(Bible);
word.useDelimiter("\\p{Punct} \\p{Space}");
以下に示すテキストファイルに表示される次の出力を提供します。
の
カエルカエル
走った
走った走った走った
速い、速い速い
遠い、遠い、遠い遠い遠い遠い
useDelimiter
句読点の新しい行などを説明することになっているのに、なぜこのような出力の違いがあるのでしょうか。おそらくかなり単純ですが、プログラムでの最初のショットです。アドバイスをよろしくお願いします。