• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

what is wrong with my code?.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got only the second line.
 
Moham'd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No 1st line, No 3rd NO 5th (if there is :])

you can copy, compile and run the code.

Regards
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic