| | |
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
Hi mates..
I'm trying to find a way in how to inputing or outputting from a file in Java.
For my bad luck when I finished my Java course, our professor didn't talked about this thing.
I searched in the internet, and found a way in inputing from a file which is:
but after that, how to read the content of that file! also, if the file has a paragraph how can I search for a specific words, I think in java I can only read a line not a word!
for example "a" or "if" or "playing" how can I look for them if they exist in the file or not.
After that, how to output the data I want in an output file!
Thanks in advance.
I'm trying to find a way in how to inputing or outputting from a file in Java.
For my bad luck when I finished my Java course, our professor didn't talked about this thing.
I searched in the internet, and found a way in inputing from a file which is:
Java Syntax (Toggle Plain Text)
import java.io.File; import java.io.FileNotFoundException; try { Scanner input = new Scanner(new File("file.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
but after that, how to read the content of that file! also, if the file has a paragraph how can I search for a specific words, I think in java I can only read a line not a word!
for example "a" or "if" or "playing" how can I look for them if they exist in the file or not.
After that, how to output the data I want in an output file!
Thanks in advance.
0
#2 34 Days Ago
First of all check the Scanner API. You found a code and you didn't think to check the API.
Now, there is the nextLine method that reads the next line of the file. Combine it with the has.. methods to read the entire file:
You can store the entire file in a Vector(add each line to a vector as the loop runs) and then loop through the vector to find the words you are looking for. Or you can check the line as you read it.
You might want to use the method: String.indexOf(String s)
It will return -1 if the argument is not found (check the String API)
OR
For a more sophisticated way, use regular expressions and patterns with these methods:
http://java.sun.com/j2se/1.5.0/docs/...a.lang.String)
or
findInLine
Now, there is the nextLine method that reads the next line of the file. Combine it with the has.. methods to read the entire file:
Java Syntax (Toggle Plain Text)
while (scanner.hasNextLine() ) { String line = scanner.nextLine(); // check if the line contains the words you want int i = line.indexOf("if"); // if i==-1 then not found }
You might want to use the method: String.indexOf(String s)
It will return -1 if the argument is not found (check the String API)
OR
For a more sophisticated way, use regular expressions and patterns with these methods:
http://java.sun.com/j2se/1.5.0/docs/...a.lang.String)
or
findInLine
Check out my New Bike at my Public Profile at the "About Me" tab
0
#4 34 Days Ago
•
•
•
•
If you want to find a specific word from within a file, another way to do it would be to use Scanner's next() method, then use String.equals to see if they match.
In order for my solution to work, the OP would have to use the
.split(" ") method to "break" the line and then use the equals method the way you described. 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
#5 34 Days Ago
Thanks mates for those useful links.
Sorry for that but I got confused, so which solution you think it'll be easier to implement and as you said javaAddict I want to find the WHOLE word if exist not a part of it.
like for example when I search for "ok" it wouldn't take this as a found "okay".
Thanks
Sorry for that but I got confused, so which solution you think it'll be easier to implement and as you said javaAddict I want to find the WHOLE word if exist not a part of it.
like for example when I search for "ok" it wouldn't take this as a found "okay".
Thanks
•
•
Join Date: Sep 2008
Posts: 1,595
Reputation:
Solved Threads: 202
1
#6 34 Days Ago
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop
And just look at what it prints to answer your question.
Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop
Java Syntax (Toggle Plain Text)
while(scanner.hasNext()) System.out.println(scanner.next());
And just look at what it prints to answer your question.
Out.
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
0
#7 33 Days Ago
•
•
•
•
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop
Java Syntax (Toggle Plain Text)
while(scanner.hasNext()) System.out.println(scanner.next());
And just look at what it prints to answer your question.

one more thing please, if I want to check a specific word where it occurred in which line of the file, how to do that?
I mean for example "a" I want to see in which line it appears, how to do that!?
Thanks in advance.
•
•
Join Date: Jun 2008
Posts: 130
Reputation:
Solved Threads: 2
0
#8 33 Days Ago
This is my code now, WHY i'm getting an error!!
Here is the Data.java:
and this is Driver.java:
any clue on how to solve the error?
Here is the Data.java:
Java Syntax (Toggle Plain Text)
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[50]; public int[] getFreq() { return Freq; } public void setFreq(int[] f) { Freq = f; } public void searchForFreq() { Scanner input; try { input = new Scanner(new File("file.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } while( input.hasNext() ) { if( input.next() == "a") Freq[0] = Freq[0] + 1; else if( input.next() == "if") Freq[1] = Freq[1] + 1; else if( input.next() == "is") Freq[2] = Freq[2] + 1; else if( input.next() == "of") Freq[3] = Freq[3] + 1; else if( input.next() == "peck") Freq[4] = Freq[4] + 1; else if( input.next() == "peppers") Freq[5] = Freq[5] + 1; else if( input.next() == "peter") Freq[6] = Freq[6] + 1; else if( input.next() == "picked") Freq[7] = Freq[7] + 1; else if( input.next() == "pickled") Freq[8] = Freq[8] + 1; else if( input.next() == "piper") Freq[9] = Freq[9] + 1; else if( input.next() == "that") Freq[10] = Freq[10] + 1; else if( input.next() == "the") Freq[11] = Freq[11] + 1; else if( input.next() == "where") Freq[12] = Freq[12] + 1; } for( int i = 0; i < Freq.length; i++ ) { System.out.println( a[i] + "...." + Freq[i]); } } }
and this is Driver.java:
Java Syntax (Toggle Plain Text)
public class Driver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Data d = new Data(); d.searchForFreq(); } }
any clue on how to solve the error?
0
#9 33 Days Ago
First of all run this code:
The name of the method is pretty self explanatory:
Whenever you call it it returns the next word.
In your while loop you keep calling it again and again inside the loop so every time you get the next word. This doesn't compare the same word:
Also use the equals method to compare objects such as the String object:
Java Syntax (Toggle Plain Text)
while(scanner.hasNext()) { System.out.println(scanner.next()); }
Whenever you call it it returns the next word.
In your while loop you keep calling it again and again inside the loop so every time you get the next word. This doesn't compare the same word:
Java Syntax (Toggle Plain Text)
if( input.next() == "a") // this will return a word Freq[0] = Freq[0] + 1; else if( input.next() == "if") // this will return a different word from the previous one Freq[1] = Freq[1] + 1;
Also use the equals method to compare objects such as the String object:
if ( s.equals("a") ) { .. } Last edited by javaAddict; 33 Days Ago at 9:09 am.
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
#10 33 Days Ago
•
•
•
•
First of all run this code:
The name of the method is pretty self explanatory:Java Syntax (Toggle Plain Text)
while(scanner.hasNext()) { System.out.println(scanner.next()); }
Whenever you call it it returns the next word.
In your while loop you keep calling it again and again inside the loop so every time you get the next word. This doesn't compare the same word:
Java Syntax (Toggle Plain Text)
if( input.next() == "a") // this will return a word Freq[0] = Freq[0] + 1; else if( input.next() == "if") // this will return a different word from the previous one Freq[1] = Freq[1] + 1;
Also use the equals method to compare objects such as the String object:
if ( s.equals("a") ) { .. }
So, you mean I should first save the input in a variable and compare it
like this:
Java Syntax (Toggle Plain Text)
String s = input.next(); if( s.equals("a") ) blah blah else if( s.equals("if") ) blah blah ...
That is what you meant?
And yeah I ran this code:
Java Syntax (Toggle Plain Text)
while(scanner.hasNext()) { System.out.println(scanner.next()); }
and it worked perfectly.
![]() |
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 = :(
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class classes clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite ingres input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree windows






