Need MAJOR help with JAVA

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Xylitol

Diamond Member
Aug 28, 2005
6,617
0
76
Originally posted by: PMChris
Originally posted by: AgaBoogaBoo
Originally posted by: George P Burdell
Originally posted by: Xylitol
Originally posted by: George P Burdell
Originally posted by: AgaBoogaBoo
Start by thinking how you would do it yourself if you had to find it, try to mimic that and then improve from there. Write out in words what you would do in simple english, then try to turn this into a flowchart, and finally into the program. It might seem a bit drawn out, but it will help you fully understand how to do it.

Agreed. Draw it out using paper/pencil if that works for you.

BTW, what's your major?

I'm in highschool
I dont even know how to start with the loop to search

Didn't your teacher teach you anything? No textbook?

Oh what the hell, here is clue #1: StringTokenizer

As for loops, just google for or while loops in java and see what you get.
OP: http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html (1.4.2 version)

Learn to use the docs asap, you'll soon realize that they are an invaluable resource when coding in java.

OP stated that he could only use ArrayLists to do this. I'll also be using the capabilities of the String class.

Also, your teacher is pretty ague with the specifications. He has to specify what characters can appear in a word. By your text I am going to assume just alphabetic characters and the hyphen, but this solution would not work if words can contain other characters. In addition I will assume that a single hyphen is not considered a word. He also should specify what output he wants, I've produced output that gives the number of instances of each word in the text and a total word count. An advanced programming course would also specify a bound on the runtime.

This is how I'd do it, going on from your code snippet (you'd pick the code from Option 1 or Option 2, not both):

ArrayList<String> wordList = new ArrayList<String>();
/* Option 1: No visible use of outside class besides String and ArrayList */
String tempWord = null;
while(fin.hasNextLine()) {
String[] words = fin.nextLine().split("[^a-zA-Z-]");
int wordsLength = words.length;
for(int wordIndex = 0; wordIndex < wordsLength; wordIndex++) {
String currentWord = words[wordIndex];
int currentWordLength = currentWord.length();
if(currentWord == null || currentWord.equals("") || currentWord.equals("-")) {
continue;
} else if(wordIndex == (wordsLength - 1) && currentWord.charAt(currentWordLength - 1) == '-') {
// This and the next else if are accounting for the common practice of cutting words with a hyphen at the end of a line for spacing purposes
tempWord = currentWord.substring(0, currentWordLength - 1);
} else if(tempWord != null) {
String newWord = tempWord + currentWord;
wordList.add(newWord);
tempWord = null;
} else {
wordList.add(currentWord);
}
}
}
/* END OPTION 1 */
/* OPTION 2: Less code, does the same thing, uses the Pattern class */
Pattern wordPattern = Pattern.compile("[a-zA-Z-\n]")
while(fin.hasNext(wordPattern)) {
String nextWord = fin.next(wordPattern);
nextWord.replaceAll("-\n", "");
nextWord.replaceAll("\n", "");
if(!nextWord.equals("-"))
wordList.add(nextWord);
}
/* END OPTION 2 */
// How I do this next bit is very lazy and slow, a better solution would be to quicksort the list then walk it with a counter going up until resetting when the String changes, but I don't want to write that
System.out.println("Counts for each word appearing in this text:";
int totalWords = wordList.length();
while(!wordList.isEmpty()) {
String firstWord = wordList.get(0);
int count = 0;
while(wordList.remove(firstWord)) {
count++;
}
System.out.println(firstWord + ": " + count);
}
System.out.println("Total word count: " + totalWords);

That should do it (I didn't compile or test it, so it's probably full of errors, but the idea should come across). My option 1 probably can be written shorter with a slight bump in memory usage, but I should get to bed. You can't submit it because I doubt you know regular expressions.

I made a scanner and everything with variable fin, but it still says that the
while (fin.hasNextLine ())
is an illegal start of an expression
(I imported the java.util.Scanner)
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |