473,487 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Text file I/O

2 New Member
Hi, I'm pretty new to Java, and I'm really struggling with getting my code to work. The files compile with no errors, but they are supposed to input/output to text files, and I can't get it to output anything. There are two files in the program, the point of it being to count the frequency of words in a text file, and I just wondered if you could tell me where I need to insert the file names to get the code to work correctly. Thank you

FreqListCreator.java

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class FreqListCreator {
  4.  
  5. public static void
  6. main(String args[])
  7. throws IOException {
  8.     if(args.length != 1) {
  9.         System.err.println("usage: FreqListCreator filename");
  10.     } else {
  11.         FileTokeniser ft = new FileTokeniser(args[0]);
  12.         FreqList flist = new FreqList();
  13.         while(ft.hasMoreTokens()) {
  14.             flist.add(ft.getNextToken());
  15.         }
  16.         ft.close();
  17.         PrintWriter pw = new PrintWriter(new FileWriter(args[0]+".frq"));
  18.         flist.save(pw);
  19.         pw.close();
  20.     }
  21. }
  22.  
An excerpt from FreqList:

Expand|Select|Wrap|Line Numbers
  1. public void
  2. save(PrintWriter pw) {
  3.     Iterator it = storage.keySet().iterator();
  4.     while(it.hasNext()) {
  5.         String word = (String)it.next();
  6.         int freq = getFreq(word);
  7.         pw.println(word+" "+freq);
  8.     }
  9. }
  10.  
  11.  
  12. public static void
  13. main (String args[])
  14. throws IOException {
  15.     FreqList flist = new FreqList();
  16.     BufferedReader br= new BufferedReader(new FileReader(args[0]));
  17.     flist.load(br);
  18.     br.close();
  19.     Iterator it = flist.iterator();
  20.     while(it.hasNext());
  21.           String word = (String) it.next();
  22.           int freq = flist.getFreq(word);
  23.           System.out.println(word+": "+freq);
  24. }
  25. }
  26.  
Nov 14 '07 #1
4 1355
r035198x
13,262 MVP
What command did you use to run the program? It's expectiong a filename as argument to the main method.
Nov 15 '07 #2
samido
52 New Member
Hi, I'm pretty new to Java, and I'm really struggling with getting my code to work. The files compile with no errors, but they are supposed to input/output to text files, and I can't get it to output anything. There are two files in the program, the point of it being to count the frequency of words in a text file, and I just wondered if you could tell me where I need to insert the file names to get the code to work correctly. Thank you

FreqListCreator.java

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class FreqListCreator {
  4.  
  5. public static void
  6. main(String args[])
  7. throws IOException {
  8.     if(args.length != 1) {
  9.         System.err.println("usage: FreqListCreator filename");
  10.     } else {
  11.         FileTokeniser ft = new FileTokeniser(args[0]);
  12.         FreqList flist = new FreqList();
  13.         while(ft.hasMoreTokens()) {
  14.             flist.add(ft.getNextToken());
  15.         }
  16.         ft.close();
  17.         PrintWriter pw = new PrintWriter(new FileWriter(args[0]+".frq"));
  18.         flist.save(pw);
  19.         pw.close();
  20.     }
  21. }
  22.  
An excerpt from FreqList:

Expand|Select|Wrap|Line Numbers
  1. public void
  2. save(PrintWriter pw) {
  3.     Iterator it = storage.keySet().iterator();
  4.     while(it.hasNext()) {
  5.         String word = (String)it.next();
  6.         int freq = getFreq(word);
  7.         pw.println(word+" "+freq);
  8.     }
  9. }
  10.  
  11.  
  12. public static void
  13. main (String args[])
  14. throws IOException {
  15.     FreqList flist = new FreqList();
  16.     BufferedReader br= new BufferedReader(new FileReader(args[0]));
  17.     flist.load(br);
  18.     br.close();
  19.     Iterator it = flist.iterator();
  20.     while(it.hasNext());
  21.           String word = (String) it.next();
  22.           int freq = flist.getFreq(word);
  23.           System.out.println(word+": "+freq);
  24. }
  25. }
  26.  


Sorry man!! but what are this object suppose to do: FileTokeniser ft = new FileTokeniser(args[0]);
FreqList ...
Nov 15 '07 #3
amonita
2 New Member
What command did you use to run the program? It's expectiong a filename as argument to the main method.
I built and ran it in NetBeans, I'm not sure where i would put a command line argument?
Nov 15 '07 #4
r035198x
13,262 MVP
I built and ran it in NetBeans, I'm not sure where i would put a command line argument?
After buiding it in Netbeans, it tells you the commands to use for running it from the command prompt.
When you wrote the code you expected that the program would be given a file name as an argument so you have to pass that to main.
Nov 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

22
61336
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
9106
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
27
4880
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
16
2634
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
7
4299
by: Chris | last post by:
Hi I can use a text file as a datasource but am unable to get the datatable to see the text file as having multiple columns. Everything gets put into the first column in the datatable. Sample of...
3
2147
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
1
4743
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
10
3156
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
10704
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.