• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is wrong with my code?.  RSS feed

 
Moham'd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this program, I am trying to read from a file each couple of words ends with this char "|" as a token and then write the out put in another file.

my code is working fine just for 1 line, but the rest is not

i don't know why?. is it from the loop or what?

===========================================================
import java.util.*;
import java.io.*;
import java.*;


public class Test
{
public static void main(String[] args) throws IOException
{

//FileReader fr=new FileReader("test1.txt");
//FileWriter fw=new FileWriter("testTest.txt");



BufferedReader buffer=new BufferedReader(new FileReader("test1.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("TestFileOut.txt"));

String str=buffer.readLine();

try
{
for (int i=0;i<7;i++)
{
str=buffer.readLine();
StringTokenizer st = new StringTokenizer(str, "|");



while (st.hasMoreTokens())
{
//System.out.println(st.nextToken());
pw.println(st.nextToken());


}
pw.close();

}

}

catch(Exception e)
{
System.out.println(" ");
}
}
}
==============================================================
*****************
TXT FILE
test1.txt

| Eric John Smith | iam 22 years old | thanks |
| Jessica Norman King | iam 19 years old | thanks |

****************

Regards
 
C. Alan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you be just a bit more specific about the problem you are having? My guess is that the information is not appearing in the file that you are writing to?

Which one line is working?
 
Moham'd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got only the second line.
 
Moham'd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No 1st line, No 3rd NO 5th (if there is :])

you can copy, compile and run the code.

Regards
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Class,

Welcome to JavaRanch!

In your haste to come in and ask a question, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- no joke names, "handles," or last initials are acceptable. You can fix your display name here. Thanks for your cooperation!

Regarding your problem: when you define the variable "str", you initialize it to point to the first line read from the file; then you never use that string, but immediately read and process the next seven lines. It looks as though the program would work more or less correctly if you simply didn't call "readline" when declaring "str".
 
C. Alan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking in addition to that, that since the read buffer was not being closed that the information was not being flushed from there... at least I think that is how I learned it.

Pardon me,.. I don't mean to be confusing the issues.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Method Class:
No 1st line, No 3rd NO 5th (if there is :])

you can copy, compile and run the code.

Regards


This is purely by "eyeball"... no copy. no compile. no running... so take this answer with a huge grain of salt.

The reason that you are not getting the first line is because you threw it away. You read a line, then entered a loop, which reads another line.

The reason that you are not getting more than the second line is because you close the output stream after processing one line.

Henry
[ May 08, 2005: Message edited by: Henry Wong ]
 
Moham'd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx all,

the problem is solved,

the mistake was from the close() method

it should be out side the loop

Regards
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you used [CODE] tags to preserve indentation.. then it would be obvious that the close() was inside the loop
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!