posted 11 years ago
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!!!
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!!!
posted 11 years ago
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 ]
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 ]
Jason Mackie
Greenhorn
Posts: 17
posted 11 years ago
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
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

thanks for your help...i hope this will further help you help me! lol thanks again
posted 11 years ago
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...
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
Jason Mackie
Greenhorn
Posts: 17
posted 11 years ago
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?
thanks again guys...this site is a big help...now should i delete this whole post thing since i no longer need help?
posted 11 years ago
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
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

Create symphonies in seed and soil. For this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
|