package addlinenumbers;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class AddLineNumbers {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String sentinel = new String();
int i=0;
try
{
FileOutputStream fos = new FileOutputStream
("dataInput.txt", true); //true means we will be appending to dataInput.txt
PrintWriter pw = new PrintWriter (fos);
//write data to the file
while(!(sentinel.equals("-1")))
{
System.out.println("Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT: ");
pw.print(input.nextLine());
i++;
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Unable to find dataInput.txt...");
}
catch (IOException ioe)
{
ioe.printStackTrace(System.out);
}
finally
{
System.out.println("# of objects: " + i);
System.out.println("Closing file...");
input.close();
}
}
}
Currently my output will endlessly ask me to enter strings to 'dataInput.txt' (which is in the appropriate project folder) but it will not exit from the while loop with the proper sentinel for Java strings . Am I missing something here? I'm not using ==
. "-1" does nothing but loop back again. It should kick out, write the inputs to the text file in prepending fashion and then close the file.
Also! As it turns out, nothing is being taken from the while-loop input and transferred to the 'dataInput.txt' file. I'm not sure why.
Any help will be greatly appreciated!
EDIT: Just as an FYI, I must use a while loop with a sentinel. Thanks again everyone who is/has/will help me on this issue.
run:
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT:
David
Goliath
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT:
Delilah
Samson
Please enter strings to prepend to 'dataInput.txt'; -1 to EXIT:
-1
# of objects prepended: 2
Closing file...
BUILD SUCCESSFUL (total time: 18 seconds)
As you can see, it takes in only TWO objects they are "Goliath" and "Samson" and they are the only strings written to the text file. Technically it should have 4 objects and "David" and "Delilah" should be in the text file also, but they're not.
Any help would be greatly appreciated.
while(!(sentinel.equals("-1")))
can never be false
(for the loop condition), because sentinel
never changes, it's always ""
Conceptually, you need to read the user input and decide what do with it, you would then use this value to determine if you need to exit the loop
So, this is a "really quick" example (not tested) of what you could do...
Scanner input = new Scanner(System.in);
int i = 0;
try (FileOutputStream fos = new FileOutputStream("dataInput.txt", true)) {
try (PrintWriter pw = new PrintWriter(fos)) {
String userInput = "";
do {
userInput = input.nextLine();
if (!userInput.equals("-1")) {
pw.print(input.nextLine());
i++;
}
} while (!userInput.equals("-1"));
}
} catch (FileNotFoundException fnfe) {
System.out.println("Unable to find dataInput.txt...");
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
} finally {
System.out.println("# of objects: " + i);
}
FYI: input.close();
isn't closing the "file", it's closing the stdin, which is never a good idea
NB: The compounding try-with
blocks are overkill, you could use a single statement to wrap it all up in, but I wanted to demonstrate the concept around a similar code structure
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.