How to input and output from/or to a file?!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #11
Oct 30th, 2009
I did this now I used "s.equalsIgnoreCase(argu)" because some of the words in the file having CAPITAL letters.

Data.java:

  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"
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #12
Oct 30th, 2009
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #13
Oct 30th, 2009
Originally Posted by javaAddict View Post
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:
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #14
Oct 30th, 2009
  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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #15
Oct 30th, 2009
Originally Posted by javaAddict View Post
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #16
Oct 30th, 2009
java.util.Vector
  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. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #17
Oct 31st, 2009
Originally Posted by javaAddict View Post
java.util.Vector
  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 :\
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #18
Oct 31st, 2009
Originally Posted by javaAddict View Post
  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.
  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).

  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:
  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
  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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,613
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #19
Oct 31st, 2009
You could also make a small class:

  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:

  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
1
  #20
Oct 31st, 2009
Actually I understood that the objective was to monitor how many times a word appeared.
I would suggest sth like this:
  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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC