나는 끝내기가 거의 끝났지 만, 모든 것을 묶는 방법을 알아낼 수는 없습니다. 나는 그들의 특별한 임무에 대한 책임이있는 별도의 방법을 가지고 있지만, 나의 결속이 실제로 나쁘다고 느낀다. 어떻게 그들이 함께 묶고 무엇이 메인에서 호출되어야하는지 확실하지 않습니다. 여기서 목표는 명령 줄에서 텍스트 파일을 읽고 스토리의 사전 단어를 사전 식으로 나열하는 것입니다. 여기 리스트 정렬에 대한 할당
% java Ten < story.txt
Word Occurs
==== ======
a 21
animal 3
.
.
.
zebra 1
%
는 지금까지 내 코드입니다 :
import java.util.Scanner;
public class Ten2
{
public static void main(String [] arg)
{
Scanner input = new Scanner(System.in);
String word;
List sortedList = new List();
word = nextWord(input);
while (word!=null) {
sortedList.insert(word);
word = nextWord(input);
}
sortedList.printWords();
}
private static String nextWord(Scanner input)
{
// return null if there are no more words
if (input.hasNext() == false)
return null;
// take next element, convert to lowercase, store in s
else {
String s = input.next().toLowerCase() ;
// empty string
String token = "";
// loop through s and concatonate chars onto token
for (int i =0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)) == true)
token = token + s.charAt(i);
else if (s.charAt(i) == '\'')
token = token + s.charAt(i);
else if (s.charAt(i) == '-')
token = token + s.charAt(i);
}
return token;
}
}
}
class List
{
/*
* Internally, the list of strings is represented by a linked chain
* of nodes belonging to the class ListNode. The strings are stored
* in lexicographical order.
*/
private static class ListNode
{
// instance variables for ListNode objects
public String word;
public ListNode next;
public int count;
// Listnode constructor
public ListNode(String w, ListNode nxt)
{
word = w; // token from nextWord()?
next = nxt; // link to next ListNode
count = 1; // number of word occurences
}
}
// instance variables for List object
private ListNode first;
private int numWords;
// constructor postcondition: creates new Listnode storing object
public List()
{
first = null; // pointer to ListNode?
numWords = 0; // counter for nodes in list
}
// Insert a specified word into the list, keeping the list
// in lexicographical order.
public void insert(String word)
{
// check if first is null
if (first == null) {
ListNode newNode;
newNode = addNode(word, null);
first = newNode;
}
// else if (first is not null) check if word matches first word in List
else if (word.equals(first.word)) {
// increase count
first.count++;
}
// else (first is not null && doesn't match first word)
else {
ListNode newNode;
ListNode current;
current = first;
ListNode previous;
previous = null;
int cmp = word.compareTo(current.word);
/*
* Fist two cases of empty list and word already existing
* handled in above if and else statements, now by using compareTo()
* method between the words, the insertion postion can be determined.
* Links between ListNode variables current and previous need to be
* modified in order to maintain the list
*/
// loop as long as value comparing to is positive
// when compareTo() returns positive this means the "word" parameter is greater than the word in the list
while ((cmp >0) && (current.next != null)) {
previous = current;
current = current.next;
cmp = word.compareTo(current.word);
}
// insert after current at end of list
if ((cmp >0 && current.next == null)) {
newNode = addNode(word, null);
current.next = newNode;
}
// increments count when word already exists
else if (cmp==0) {
current.count++;
}
// else (cmp < 0) we insert BEFORE current
else {
newNode = addNode(word, current);
// first node in list comes after new word
if (previous == null) {
first = newNode;
}
else {
// inserting new word in middle of list
previous.next = newNode;
}
}
}
}
// method to add new ListNode and increase counter
private ListNode addNode(String word, ListNode next)
{
ListNode newNode = new ListNode(word, next);
numWords++;
return newNode;
}
// Returns a string array that contains all the words in the list.
public String[] getWords()
{
String[] Words = new String[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Words[i] = current.word;
current = current.next;
i++;
}
return Words;
}
// Returns an int array that contains the number of times
// each word occurs in the list.
public int[] getNumbers()
{
int[] Numbers = new int[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Numbers[i] = current.count;
current = current.next;
i++;
}
return Numbers;
}
// Outputs the string array and int array containing all the
// words in the list and the number of times each occurs.
public void printWords()
{
int[] Numbers = getNumbers();
String[] Words = getWords();
System.out.println("Word \t \t Occurs");
System.out.println("==== \t \t ======");
for (int i =0; i < numWords; i++) {
System.out.println(Words[i] + " \t " + Numbers[i]);
}
}
}
또한 빠른 참조 - 아마도 조금 다르게했을 것입니다. 사용자가 * filename *을 인수로 지정한 다음 BufferedReader를 열면 좋겠지 만 스타일 문제가 더 많을 것으로 생각됩니다. :) – jwir3
도움을 주셔서 감사합니다. 내 인쇄 방법에서 컴파일러 오류가 발생했습니다. getWords() 및 getNumbers()에서 "List"참조를 삭제했습니다. 이제 컴파일되지만, main에서 .txt 파일에 대한 테스트를 실행하는 방법을 정확하게 모르겠습니다. 나는 BlueJ를 사용하고 출력을 보는 방법을 모르고있다. JVM은 계속 실행됩니다. – Bill
흠 ... 전 BlueJ를 IDE로 사용한 적이 없습니다. 나는 Eclipse를 실제로 추천 할 것이다. 당신이 할 수있는 다른 일은 BlueJ를 사용하여 컴파일 한 다음, 터미널을 사용하여 프로젝트 디렉토리에서 클래스/디렉토리로 이동합니다 (사용중인 OS에 따라 Windows cmd 또는 터미널 만 사용). 그런 다음 java Ten2
jwir3