2

I have been trying to create a program in Java that outputs each word found in a string and the number of times they appear. I have been trying to do this without using any arrays or hashmap, and by only using string manipulation methods and string tokenizer, but I can't seem to wrap my head around the logic.

Here is what I have so far.

Scanner sc=new Scanner(System.in);
String input="";
int count=1;
String word="";

System.out.println("Enter your paragraph.");
input=sc.nextLine();

StringTokenizer st=new StringTokenizer(input);

while(st.hasMoreTokens()){
    word=st.nextToken();
    while(st.hasMoreTokens()){
        if(st.nextToken()==word){
            count++;
        }
    }
    System.out.println(word+": "+count);
    input=input.replaceAll(currentToken," ")
    count=1
}

The output I am currently receiving is (the first word entered)=0.

Any help or advice with this or anything to lead me in the right direction?

Pang
  • 9,564
  • 146
  • 81
  • 122
fizzle
  • 23
  • 3
  • Why the restriction against hashmaps? – a.deshpande012 Oct 23 '17 at 01:12
  • 3
    Javadoc of [`StringTokenizer`](https://docs.oracle.com/javase/9/docs/api/java/util/StringTokenizer.html) since Java 1.4 (2002): *`StringTokenizer` is a **legacy class** that is retained for compatibility reasons although its **use is discouraged** in new code. It is recommended that anyone seeking this functionality use the `split` method of `String` or the `java.util.regex` package instead.* --- Tell your teacher to throw out that 15 year old teaching material. It's about time to get material from this decade. – Andreas Oct 23 '17 at 01:14
  • @coolioasjulio I am in school and haven't learned about hashmaps yet. I am trying to figure this out using only basic java knowledge. – fizzle Oct 23 '17 at 01:18
  • After the inner `while` loop ends (because `st.hasMoreTokens()` returned `false`), what do you think `st.hasMoreTokens()` in the outer `while` loop will return? – Andreas Oct 23 '17 at 01:18
  • 1
    Why are you using two while loops and also use .equals for comparing strings instead of == – Olantobi Oct 23 '17 at 01:19
  • Agree with Andreas on the outdated syllabus. Some programs such as International Baccalaureate loves outdated syllabus. – user3437460 Oct 23 '17 at 04:25

1 Answers1

2

You are quite close. But the 0 as result does not match your posted code, it is 1, but even if there are more occurrences. That is because you used == instead of .equals() in the if.

And in the end of the loop the tokenizer has been drained by the inner loop, so you should reinitialize a new one, espacially after you already changed the input string.

StringTokenizer st = new StringTokenizer(input);

while (st.hasMoreTokens()) {
  word = st.nextToken();

  while (st.hasMoreTokens()) {
    if (st.nextToken().equals(word)) {
      count++;
    }
  }
  System.out.println(word + ": " + count);
  input = input.replaceAll(word, "");
  count = 1;
  st = new StringTokenizer(input);
}

Note: As Andreas and coolioasjulio mentioned is the use of StringTokenizer discouraged (more about it here).

Cryptjar
  • 1,079
  • 8
  • 13
  • I was thinking that I would have to reinitialize the string tokenizer! Thank you! – fizzle Oct 23 '17 at 01:24
  • If I may... `.equals()` > `==` – Pranav A. Oct 23 '17 at 01:25
  • It should be noted that `StringTokenizer` is a legacy class, and really shouldn't be used. You might have to use it for this assignment, but just keep that in mind. – a.deshpande012 Oct 23 '17 at 01:29
  • @PranavAvva yes. `equals` should be implemented to match equal content, and because `==` matches identity, it has the same content. So if `a == b` is true than should `a.equals(b)` also true. But it can be implemented differently. – Cryptjar Oct 23 '17 at 01:29