Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple File IO Question...Please Help!!!

 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have to import a file using a fileInputStream and then take out all the extra white spaces and then output the file into a different file obviously using a fileOutputStream. For the life of me I cant figure out how to remove the extra white spaces. Here is my code and all that good stuff so far:

Code:

import java.io.*;
import java.util.*;
public class FileIO {
public static void main (String[] args){
Scanner inputStream = null;

try{
inputStream = new Scanner (new FileInputStream ("Lab6Input.txt"));
}

catch (FileNotFoundException e){
System.out.println("There is no file with that name");
System.exit(0);
}

String firstLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String secondLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String thirdLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String array[];

System.out.println(firstLine);
System.out.println(secondLine);
System.out.println(thirdLine);

}
}

The input file:

Now is the time for all
good people to come to the
aid of their computers.

What the output file should look like:

Now is the time for all
good people to come to the
aid of their computers.


THANKS FOR YOUR HELP!!!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by extra white space.

Can you give an example?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try reposting your code but use the code tags on either side of your code snippit (see the "Instant UBB Code" on the bottom of your edit box for the proper tags). Also, put your input and output files in the same tags. I think that you lost all of your white space when you posted the files without the code tags.

Also, if the white space is on either side of your strings, do you know of any functions that can check the characters at these particular locations? If found, do you know of any string functions to get rid of them? Look in the docs under string functions and you'll find lots of useful stuff.
[ March 11, 2007: Message edited by: pete stein ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sorry to Ernest...

and to Pete,

I have no idea how to do that whole ubb thing. I am using Eclipse and i didnt see anything that said ubb in edit.

the extra whitespaces in my input code are missing but they dont matter. they can be anywhere. I just cant figure out how to get rid of more than one between each word. It should read just like in the output. I tried using the "trim()" method but that had no effect. I think i need to read the words by themselves and then call "trim" but i cant figure out how to do this. Im new to this stuff and its kicking my rear I also looked in my book for string commands and trim was the only promising one in there that i noticed.

thanks for your help...i hope this will further help you help me! lol thanks again
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well in the beginning of your code you are using Scanner to read in your file. if i am not mistaken Scanner's main purpose is not for reading files. it is for tokenizing String objects. it just so happens that it can take a file stream as input. so look carefully at the api for that object and it should help you accomplish the algorithm you just stated.

if you're interested this probably can be done with regular expressions as well.

about the ubb code thing by the way, what they are talking about is that when you are making comments on this forum, there are some buttons on the right of the stupid smiley faces... use the one labeled CODE to enter in your source code...
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so here is the code that I have and i put a comment block in where I think the remove whitespace code goes



here is the input and output and how it should look

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use regular expressions?
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I can. That was not specified in the assignment. But I figured it out...gosh it took me forever. Thanks to everyone that helped me. Here is the snipit of code i needed to get it to work. I ran this code 3 separate times with slightly different variables for the three separate lines that i was inputing.



thanks again guys...this site is a big help...now should i delete this whole post thing since i no longer need help?
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this code 3 separate times with slightly different variables for the three separate lines that i was inputing.


Isn't it a bit of an overkill to have three copies of the same code? Just put the code into a private method, that takes a work string as a parameter, and that returns the result as a string.

BTW, if you are allowed to use regex, then this would work...

firstLine = firstLine.replaceAll("\\s+", " ");

Henry
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what happens if you are given a file with 4 lines?

You don't really need a Scanner object in the approach you took. There is also a String.split method that could have helped you out here. Using scanner to use tokens could have gone along these lines:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic