Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2005, 11:21 PM   #1 (permalink)
crazyant
Registered User
 
Join Date: Mar 2005
Posts: 3
crazyant is on a distinguished road
Output problem with file manipulation (newbie)

I am going crazy.... I have a simple file I/O prog here that imports a txt file and then outputs that same text file with alternateing uppercase and lowcase lines. It works great except for at the very end of the program it adds a string "null" to the output. I can't seem to get rid of it.... what am i forgetting guys ?

Here is my source :
Code:
import java.io.*;

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

String in_file, out_file, Line_one;
PrintStream output_file;

System.out.print("Welcome to Anthony's UPPERCASE File "
+ "Converter!\n");
InputStreamReader reader =
new InputStreamReader(System.in);
BufferedReader keyboard =
new BufferedReader(reader);

System.out.println("Enter the input file name: ");

in_file = keyboard.readLine();
FileReader fReader = new FileReader(in_file);
BufferedReader inputFile = new BufferedReader(fReader);

Line_one = inputFile.readLine();

System.out.println("Enter your output file name: ");
out_file = keyboard.readLine();
output_file = new PrintStream(
new FileOutputStream(
new File(out_file)));


while(Line_one != null) //begin loop
{
System.out.println(Line_one.toUpperCase());
output_file.println(Line_one.toUpperCase());
Line_one = inputFile.readLine();
System.out.println(Line_one);
output_file.println(Line_one);
Line_one = inputFile.readLine();
}
System.out.print("Output file saved.\n");
// Close the file.
inputFile.close();
output_file.close();
}
}
and one output... the last line contains "null"

FRANKIE OWNED A
ferocious feline named
FREDDY AS A PET.
Freddy was funny and
FURRY.
His fur is bright red
WITH BLACK STRIPES.
null <----------------------------------------- THIS!!!! HELP PLEASE!!!!


Thanks in advance,

Anthony

Last edited by Belisarius; 03-11-2005 at 02:52 AM.
crazyant is offline   Reply With Quote
Old 03-11-2005, 02:55 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
You have two readlines in your loop, but an odd number of lines to be read. Naturally the second readLine() will read a null on the last loop, because there's nothing there.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-11-2005, 08:58 AM   #3 (permalink)
crazyant
Registered User
 
Join Date: Mar 2005
Posts: 3
crazyant is on a distinguished road
Quote:
Originally Posted by Belisarius
You have two readlines in your loop, but an odd number of lines to be read. Naturally the second readLine() will read a null on the last loop, because there's nothing there.
ahhhh so is there any way of getting around this? Or is it simply a product of the input files number of lines?
crazyant is offline   Reply With Quote
Old 03-11-2005, 12:31 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Is it a constant format? Is the first line always upper-case, the second lower-case, etc?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-11-2005, 01:03 PM   #5 (permalink)
crazyant
Registered User
 
Join Date: Mar 2005
Posts: 3
crazyant is on a distinguished road
Quote:
Originally Posted by Belisarius
Is it a constant format? Is the first line always upper-case, the second lower-case, etc?

Yup... its devetively simple. I went ahead with a suggestion from another source...

while(Line_one != null) //begin loop
{
System.out.println(Line_one.toUpperCase());
output_file.println(Line_one.toUpperCase());

Line_one = inputFile.readLine();

if (Line_one != null)
{ System.out.println(Line_one);
output_file.println(Line_one);
}

Line_one = inputFile.readLine();
}


Worked well, even though its a alittle clumsy.

Thanks all
crazyant is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM
file io problem yawningdog Standard C, C++ 2 01-09-2003 04:04 PM


All times are GMT -8. The time now is 10:28 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting