944,221 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1327
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 29th, 2009
0

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

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. try {
  4. Scanner input = new Scanner(new File("file.txt"));
  5. } catch (FileNotFoundException e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. }

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 29th, 2009
0
Re: How to input and output from/or to a file?!
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:
Java Syntax (Toggle Plain Text)
  1. while (scanner.hasNextLine() ) {
  2. String line = scanner.nextLine();
  3.  
  4. // check if the line contains the words you want
  5. int i = line.indexOf("if");
  6. // if i==-1 then not found
  7. }
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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 29th, 2009
0
Re: How to input and output from/or to a file?!
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 29th, 2009
0
Re: How to input and output from/or to a file?!
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.
Actually my solution is not correct. If a word contains the argument then it will return that it found it, even though it is not a whole word but only a part of it.

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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 29th, 2009
0
Re: How to input and output from/or to a file?!
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
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 29th, 2009
1
Re: How to input and output from/or to a file?!
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)
  1.  
  2. while(scanner.hasNext()) System.out.println(scanner.next());

And just look at what it prints to answer your question.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 30th, 2009
0
Re: How to input and output from/or to a file?!
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)
  1.  
  2. while(scanner.hasNext()) System.out.println(scanner.next());

And just look at what it prints to answer your question.
WOW, thank you so much I'm now coding easily

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.
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?!
This is my code now, WHY i'm getting an error!!

Here is the Data.java:

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

and this is Driver.java:

Java Syntax (Toggle Plain Text)
  1. public class Driver {
  2.  
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8.  
  9. Data d = new Data();
  10. d.searchForFreq();
  11.  
  12.  
  13.  
  14. }
  15.  
  16. }


any clue on how to solve the error?
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?!
First of all run this code:
Java Syntax (Toggle Plain Text)
  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }
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:
Java Syntax (Toggle Plain Text)
  1. if( input.next() == "a") // this will return a word
  2. Freq[0] = Freq[0] + 1;
  3. else
  4. if( input.next() == "if") // this will return a different word from the previous one
  5. 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; Oct 30th, 2009 at 9:09 am.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 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 ...
First of all run this code:
Java Syntax (Toggle Plain Text)
  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }
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:
Java Syntax (Toggle Plain Text)
  1. if( input.next() == "a") // this will return a word
  2. Freq[0] = Freq[0] + 1;
  3. else
  4. if( input.next() == "if") // this will return a different word from the previous one
  5. Freq[1] = Freq[1] + 1;

Also use the equals method to compare objects such as the String object:
if ( s.equals("a") ) { .. }
ahaaa omg i messed around lol

So, you mean I should first save the input in a variable and compare it
like this:
Java Syntax (Toggle Plain Text)
  1. String s = input.next();
  2. if( s.equals("a") )
  3. blah blah
  4. else if( s.equals("if") )
  5. blah blah
  6. ...

That is what you meant?

And yeah I ran this code:

Java Syntax (Toggle Plain Text)
  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }

and it worked perfectly.
Reputation Points: 10
Solved Threads: 2
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008

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