aspose file tools*
The moose likes Beginning Java and the fly likes Incomplete Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incomplete Java Code" Watch "Incomplete Java Code" New topic
Author

Incomplete Java Code

Heart Meishen
Greenhorn

Joined: Dec 29, 2010
Posts: 6
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

Joined: Dec 30, 2010
Posts: 3
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

Joined: Dec 29, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Manoj Rawat Pmp
Greenhorn

Joined: Dec 30, 2010
Posts: 3
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

Joined: Dec 30, 2010
Posts: 3
Moderators...

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

- Manoj Rawat.
Heart Meishen
Greenhorn

Joined: Dec 29, 2010
Posts: 6
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incomplete Java Code