943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1327
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
I did this now I used "s.equalsIgnoreCase(argu)" because some of the words in the file having CAPITAL letters.

Data.java:

Java Syntax (Toggle Plain Text)
  1. /**
  2.  * @author Q8iEnG
  3.  *
  4.  */
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.util.Scanner;
  8.  
  9. public class Data {
  10.  
  11. private String[] a = {"a", "if", "is", "of", "peck", "peppers", "peter", "picked", "pickled", "piper", "that", "the", "where"};
  12. private int Freq[] = new int[13];
  13.  
  14. public int[] getFreq() {
  15. return Freq;
  16. }
  17. public void setFreq(int[] f) {
  18. Freq = f;
  19. }
  20.  
  21. public void searchForFreq()
  22. {
  23. String s;
  24. Scanner input = new Scanner( System.in);
  25. try {
  26. input = new Scanner(new File("file.txt"));
  27. } catch (FileNotFoundException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. while( input.hasNext() )
  32. {
  33. s = input.next();
  34. if( s.equalsIgnoreCase("a") )
  35. Freq[0] = Freq[0] + 1;
  36. else
  37. if( s.equalsIgnoreCase("if") )
  38. Freq[1] = Freq[1] + 1;
  39. else
  40. if( s.equalsIgnoreCase("is") )
  41. Freq[2] = Freq[2] + 1;
  42. else
  43. if( s.equalsIgnoreCase("of") )
  44. Freq[3] = Freq[3] + 1;
  45. else
  46. if( s.equalsIgnoreCase("peck") )
  47. Freq[4] = Freq[4] + 1;
  48. else
  49. if( s.equalsIgnoreCase("peppers") )
  50. Freq[5] = Freq[5] + 1;
  51. else
  52. if( s.equalsIgnoreCase("peter") )
  53. Freq[6] = Freq[6] + 1;
  54. else
  55. if( s.equalsIgnoreCase("picked") )
  56. Freq[7] = Freq[7] + 1;
  57. else
  58. if( s.equalsIgnoreCase("pickled") )
  59. Freq[8] = Freq[8] + 1;
  60. else
  61. if( s.equalsIgnoreCase("piper") )
  62. Freq[9] = Freq[9] + 1;
  63. else
  64. if( s.equalsIgnoreCase("that") )
  65. Freq[10] = Freq[10] + 1;
  66. else
  67. if( s.equalsIgnoreCase("the") )
  68. Freq[11] = Freq[11] + 1;
  69. else
  70. if( s.equalsIgnoreCase("where") )
  71. Freq[12] = Freq[12] + 1;
  72. }
  73. for( int i = 0; i < Freq.length; i++ )
  74. {
  75. System.out.println( a[i] + "...." + Freq[i]);
  76. }
  77.  
  78. }
  79.  
  80.  
  81. }

here is the file content: "file.txt"
Quote ...
Peter Piper picked a peck of pickled peppers . A peck of pickled
peppers Peter Piper picked . If Peter Piper picked a peck of pickled peppers ,
where is the peck that Peter Piper picked ?

Now, how can I check the lines!
like each word appeared in which line? how to do this, please?
Last edited by Q8iEnG; Oct 30th, 2009 at 9:36 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
That is what I mean, but why don't you run it and see what happens. If you get correct results, it means that you understood correct.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
Click to Expand / Collapse  Quote originally posted by javaAddict ...
That is what I mean, but why don't you run it and see what happens. If you get correct results, it means that you understood correct.
I ran it, and it works perfectly. and the Thanks goes to you and to BestJewSinceJC.

here is the output:
Quote ...
a....3
if....1
is....1
of....3
peck....4
peppers....3
peter....4
picked....4
pickled....3
piper....4
that....1
the....1
where....1

But, now I need to know how to see each word where is appeared in which line, how to do that?
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
Java Syntax (Toggle Plain Text)
  1. int count = 0;
  2. while(scanner.hasNext()) {
  3. count++;
  4. System.out.println("Line "+count+" is: "scanner.next());
  5. }

You might need a Vector because you don't know how many lines are the ones for each word.
So have a Vector for each word
When the word is found, add to the word's Vector the line number.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
Click to Expand / Collapse  Quote originally posted by javaAddict ...
Java Syntax (Toggle Plain Text)
  1. int count = 0;
  2. while(scanner.hasNext()) {
  3. count++;
  4. System.out.println("Line "+count+" is: "scanner.next());
  5. }

You might need a Vector because you don't know how many lines are the ones for each word.
So have a Vector for each word
When the word is found, add to the word's Vector the line number.
I didn't understand the part you said about Vector, what is Vector!?

And you said Count++ for each word to be found, but if both words are in the same line! this won't work, right?

let me explain more:
1- I want to search for the word "fast" in a paragraph.
2- I found the frequency of how many times "fast" has been occurred.
3- I need to find in which lines of that paragraph the word "fast" appeared, for example "in line 1, 2, 4".

How to do that?
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
java.util.Vector
Java Syntax (Toggle Plain Text)
  1. Vector<Integer> v = new Vector<Integer>();
  2.  
  3. v.add(1);
  4. v.add(2);
  5. v.add(3);
  6.  
  7. for (int i=0;i<v.size();i++) {
  8. int num = v.get(i);
  9. System.out.println(num);
  10. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 31st, 2009
0
Re: How to input and output from/or to a file?!
Click to Expand / Collapse  Quote originally posted by javaAddict ...
java.util.Vector
Java Syntax (Toggle Plain Text)
  1. Vector<Integer> v = new Vector<Integer>();
  2.  
  3. v.add(1);
  4. v.add(2);
  5. v.add(3);
  6.  
  7. for (int i=0;i<v.size();i++) {
  8. int num = v.get(i);
  9. System.out.println(num);
  10. }
Sorry for that, but I didn't understood why I have to use Vector! I mean does the vector will recognize lines! if yes, how!

also, how should I input from the file to the Vector!

sorry, things are blurry for me :\
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 31st, 2009
0
Re: How to input and output from/or to a file?!
Click to Expand / Collapse  Quote originally posted by javaAddict ...
Java Syntax (Toggle Plain Text)
  1. int lineCount = 0;
  2. while(scanner.hasNext()) {
  3. lineCount++;
  4. System.out.println("Line "+lineCount+" is: "+scanner.next());
  5. }

So have a Vector for each word
When the word is found, add to the word's Vector the line number.
Java Syntax (Toggle Plain Text)
  1. Vector vIf = new Vector();
  2.  
  3. ....
  4. // inside the while loop
  5. if (line.equals("if")) {
  6. Freq[1] = Freq[1] + 1;
  7. vIf.add(lineCount);
  8. }

You don't know how many are the lines that contain the "if". So you have a vector and whenever an "if" is found you add to the vector the line number (count).

Java Syntax (Toggle Plain Text)
  1. for (int i=0;i<vIf.size;i++) {
  2. System.out.println("The 'if' found at line: "+vIf.get(i));
  3. }

Meaning that you don't need the Freq array because the size of the vector would be the numbers the 'if' appeared.


But you might need an array of vectors to make your program more advanced:
Java Syntax (Toggle Plain Text)
  1. Vector [] vArray = new Vector[13];
  2. for (int i=0;i<vArray.length;i++) { // vArray is an array
  3. vArray[i] = new Vector(); // vArray[i] is a vector. You need to initialize each element
  4. }

and in your if statements you can skip the Freq. Instead of increasing it, replace it with the array of Vectors
Java Syntax (Toggle Plain Text)
  1. if (s.equalsIgnoreCase("a") )
  2. //Freq[0] = Freq[0] + 1;
  3. vArray[0].add(count);
  4. else if ( s.equalsIgnoreCase("if") )
  5. //Freq[1] = Freq[1] + 1;
  6. vArray[1].add(count);

Now the size of each Vector tells you how many times a word was found and their elements tell you at which lines they appeared.


PS.: This is my 1500th post! Yeeeees.
Last edited by javaAddict; Oct 31st, 2009 at 12:05 pm.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 31st, 2009
0
Re: How to input and output from/or to a file?!
You could also make a small class:

Java Syntax (Toggle Plain Text)
  1. public class TrackWord{
  2. String word;
  3. int lineNumber;
  4. int wordNumber;
  5. }

Where word is the word itself, line number is the line the word was seen on, and word number of the word's position within that line. So for the sentence "I have a massive hangover" every word would have a line number of 1 and "massive" would have a word number of 4. You could easily implement this by doing the following:

Java Syntax (Toggle Plain Text)
  1. int lineNumber = 1;
  2. while(scanner.hasNextLine()){
  3. String theLine = scanner.nextLine();
  4. //theLine is now the next line from your file
  5. Scanner lineScanner = new Scanner(theLine);
  6. int wordCount = 0;
  7. while(lineScanner.hasNext()){
  8. String nextWord = lineScanner.next();
  9. }
  10. lineNumber++;
  11. }

I intentionally left out some stuff from the above code (as per our homework rules), but that is the basic idea. To fully implement it, you'd have to increment wordCount in the appropriate place, and then you'd have to create a new TrackWord object, populate it with the name of the word and the line number and word number it was seen at, then add the TrackWords to an array.

P.S. Since you want to track every line where 'fast' was seen, you could modify the class I showed above so that it had an ArrayList of line numbers. Then every time you saw the word fast, you could check to see if you already have a TrackWord Object for the word fast. And if you do, then you could add the line number you just saw it at to the TrackWord's ArrayList of line numbers. I hope that made sense.

P.P.S not trying to step on your toes JavaAddict, your solution works, this seems simpler to me though. And congrats on your 1500th post
Last edited by BestJewSinceJC; Oct 31st, 2009 at 5:06 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 31st, 2009
1
Re: How to input and output from/or to a file?!
Actually I understood that the objective was to monitor how many times a word appeared.
I would suggest sth like this:
Java Syntax (Toggle Plain Text)
  1. String word;
  2. Vector linesAppeared;
And the size of the vector would be how many times the word appeared.

You must notice that from the code given, the words that the OP wants to search for are predetermined. And originally he was searching for the frequency of their appearance.
The reason I didn't want to go into the OO solution was the novice level of the poster, and I wanted to stick to the solution that he thought.


Thanks for your congrats remark
Of course if
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Horizontal and Vertical control on button group
Next Thread in Java Forum Timeline: Graphics + Threads = :(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC