File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incomplete Java Code

 
Heart Meishen
Greenhorn
Posts: 6
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's my first time to learn about Java and I hope you will help me on how to debug this code..

This is the problem..

Write a program that reads in a whole bunch of words (each word is no more than 20 characters) and spit out the duplicate words. For our purposes, words are to be separated by spaces, commas (,), !, ?, and periods (.). No word goes past the end of a line. All comparisons should ignore case.
Output: Output the sorted words, one per line, in lower case. Display same words only once.
Sample Input:
How much wood would a woodchuck chuck if a woodchuck could chuck wood? Well? What
do you think? Are you feeling well?
Sample Console:


a
are
chuck
could
do
feeling
how
if
much
think
well
what
wood
woodchuck
would


I tried to make one but unfortunately this code is still lacking..
-All comparisons should ignore case and duplicate words..
-because I used for loop, the words to be printed are not enough..




This is the Output:

How
a
a
chuck
if
much
wood
woodchuck
woodchuck
would




 
Manoj Rawat Pmp
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read the scanner string as an parameter to the constructor. Modify the code to your requirement. Pardon me for bad formatting

package codetest;

import java.util.*;

public class UniqueValues {

public static void main(String[] args)
{
Vector vector = new Vector();

Scanner input=new Scanner("How much wood would a woodchuck chuck if a woodchuck could chuck wood ? Well ? What do you think ? Are you feeling well ?");
while(input.hasNext()) {
vector.addElement((String)input.next());
}

TreeSet treeSet = new TreeSet(new ValueComparator());
treeSet.addAll(vector);
Iterator iterator = treeSet.iterator();

while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}


class ValueComparator implements Comparator
{
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String) {
return ((String)o1).compareToIgnoreCase((String)o2);
}
return 0;
}
}

- Manoj Rawat (PMP)
 
Heart Meishen
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your reply
But how did you came up with those codes..
Those are difficult to understand for me as a begginner..

I tried to run it but [?] appeared first..
and all comparisons should ignore case also.. so all output must be in lowercase..
the program needs to read any words too..
How about my code?? Is it wrong?

Again, thank you very much for the help...
I'm so sorry for being too slow..
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the both of you!

Manoj, please LetThemDoTheirOwnHomework. People will not learn anything if you provide them clear cut answers. In this case it didn't even help Heart.
And please UseCodeTags next time.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Rawat Pmp wrote:Pardon me for bad formatting

Welcome to JavaRanch. When you UseCodeTags your source code will be formatted nicely by the forum software.
 
Manoj Rawat Pmp
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friend,

OOPS!!!

I have been writing code since last 8 years. I am sorry to put such kind of code for you which is difficult for a fresher to understand.

The output shows [?] first since we are displaying the words in natural ordering ie. a will come before b which will come before c and so on...

In ASCII, ? has a lower numeric value than a , hence its been shown first.


You can use the following code to display all the vales in small case :

while(iterator.hasNext()){
System.out.println(((String)iterator.next()).toLowerCase());
}

You can use your original code from Line 10-15 to read the string from the console but instead of adding it into a Array[] , add it into a Vector.

Array[i] = input.next(); Instead add, vector.add(input.next());

Your code had a problem with the comparision of the individual word with another. I will not explain it to you here, but try to print the value of each of the array element and the temp variable in the for loop and understand where are you going wrong.. Its okay to write wrong code. I did it too when I was new to programming. But keep trying which will make you a better programmer.

Another thing to clarify, did you wanted the words which are inputted first should be displayed first ? for eg. why ? what ? WHY ? A a

why
?
what
a

Please let me know if you have any queries.

- Manoj Rawat.
 
Manoj Rawat Pmp
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderators...

I will keep this in mind... I understand the harm if we provide the code directly...

- Manoj Rawat.
 
Heart Meishen
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help..

Now, I understand much better..

I tried to apply those informations that you gave..
Our professor haven't teach us about this yet that's why the codes are very new for me..
But still, I want to learn in advance..


I will try to debug my own codes too..
About the clarification? No, it's not like that.. it must be in alphabetical order..

Thank you very much..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic