| | |
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:
Solved Threads: 2
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:
here is the file content: "file.txt"
Now, how can I check the lines!
like each word appeared in which line? how to do this, please?
Data.java:
Java Syntax (Toggle Plain Text)
/** * @author Q8iEnG * */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Data { private String[] a = {"a", "if", "is", "of", "peck", "peppers", "peter", "picked", "pickled", "piper", "that", "the", "where"}; private int Freq[] = new int[13]; public int[] getFreq() { return Freq; } public void setFreq(int[] f) { Freq = f; } public void searchForFreq() { String s; Scanner input = new Scanner( System.in); try { input = new Scanner(new File("file.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } while( input.hasNext() ) { s = input.next(); if( s.equalsIgnoreCase("a") ) Freq[0] = Freq[0] + 1; else if( s.equalsIgnoreCase("if") ) Freq[1] = Freq[1] + 1; else if( s.equalsIgnoreCase("is") ) Freq[2] = Freq[2] + 1; else if( s.equalsIgnoreCase("of") ) Freq[3] = Freq[3] + 1; else if( s.equalsIgnoreCase("peck") ) Freq[4] = Freq[4] + 1; else if( s.equalsIgnoreCase("peppers") ) Freq[5] = Freq[5] + 1; else if( s.equalsIgnoreCase("peter") ) Freq[6] = Freq[6] + 1; else if( s.equalsIgnoreCase("picked") ) Freq[7] = Freq[7] + 1; else if( s.equalsIgnoreCase("pickled") ) Freq[8] = Freq[8] + 1; else if( s.equalsIgnoreCase("piper") ) Freq[9] = Freq[9] + 1; else if( s.equalsIgnoreCase("that") ) Freq[10] = Freq[10] + 1; else if( s.equalsIgnoreCase("the") ) Freq[11] = Freq[11] + 1; else if( s.equalsIgnoreCase("where") ) Freq[12] = Freq[12] + 1; } for( int i = 0; i < Freq.length; i++ ) { System.out.println( a[i] + "...." + Freq[i]); } } }
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.
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
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
0
#13 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.
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?
0
#14 Oct 30th, 2009
Java Syntax (Toggle Plain Text)
int count = 0; while(scanner.hasNext()) { count++; System.out.println("Line "+count+" is: "scanner.next()); }
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
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
0
#15 Oct 30th, 2009
•
•
•
•
Java Syntax (Toggle Plain Text)
int count = 0; while(scanner.hasNext()) { count++; System.out.println("Line "+count+" is: "scanner.next()); }
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.
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?
0
#16 Oct 30th, 2009
java.util.Vector
Java Syntax (Toggle Plain Text)
Vector<Integer> v = new Vector<Integer>(); v.add(1); v.add(2); v.add(3); for (int i=0;i<v.size();i++) { int num = v.get(i); System.out.println(num); }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
0
#17 Oct 31st, 2009
•
•
•
•
java.util.Vector
Java Syntax (Toggle Plain Text)
Vector<Integer> v = new Vector<Integer>(); v.add(1); v.add(2); v.add(3); for (int i=0;i<v.size();i++) { int num = v.get(i); System.out.println(num); }
also, how should I input from the file to the Vector!
sorry, things are blurry for me :\
0
#18 Oct 31st, 2009
•
•
•
•
Java Syntax (Toggle Plain Text)
int lineCount = 0; while(scanner.hasNext()) { lineCount++; System.out.println("Line "+lineCount+" is: "+scanner.next()); }
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)
Vector vIf = new Vector(); .... // inside the while loop if (line.equals("if")) { Freq[1] = Freq[1] + 1; vIf.add(lineCount); }
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)
for (int i=0;i<vIf.size;i++) { System.out.println("The 'if' found at line: "+vIf.get(i)); }
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)
Vector [] vArray = new Vector[13]; for (int i=0;i<vArray.length;i++) { // vArray is an array vArray[i] = new Vector(); // vArray[i] is a vector. You need to initialize each element }
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)
if (s.equalsIgnoreCase("a") ) //Freq[0] = Freq[0] + 1; vArray[0].add(count); else if ( s.equalsIgnoreCase("if") ) //Freq[1] = Freq[1] + 1; 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
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
0
#19 Oct 31st, 2009
You could also make a small class:
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:
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
Java Syntax (Toggle Plain Text)
public class TrackWord{ String word; int lineNumber; int wordNumber; }
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)
int lineNumber = 1; while(scanner.hasNextLine()){ String theLine = scanner.nextLine(); //theLine is now the next line from your file Scanner lineScanner = new Scanner(theLine); int wordCount = 0; while(lineScanner.hasNext()){ String nextWord = lineScanner.next(); } lineNumber++; }
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.
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:
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
I would suggest sth like this:
Java Syntax (Toggle Plain Text)
String word; Vector linesAppeared;
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
![]() |
Similar Threads
- Halp! - Input/Output File Stream Problems (C++)
- Problem with input/output in a five-file program (C++)
- input and output file (txt file) (C)
- Trying to get a while loop while using an input/output file (C++)
- Writing a file input and output program. (C++)
- input/output file (C++)
- Getting all data from an input and output file (C++)
Other Threads in the Java Forum
- Previous Thread: Horizontal and Vertical control on button group
- Next Thread: Graphics + Threads = :(
Views: 722 | Replies: 21
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






