Printing onto a text file [duplicate] - java

This question already has answers here:
How to append text to an existing file in Java?
(31 answers)
Closed 9 years ago.
I created a PrintWriter and new text file and want to print my answers into the text file. However, because the code is embedded within the program's loop, every time the loop is restarted, a new text file is created to replace the last one. How do I write the code so that the text file isn't recreated every time the loop starts over?
Here is my code:
public class Wordler {
public static void main(String[] args) throws IOException{
//introduces the game
System.out.println("Wordler: Finding words within a word");
System.out.println(" ");
System.out.println("Directions:");
System.out.println("Create as many words as you can with the letters in the word given. Once you have written as many words as you can think of, type");
System.out.println("'x' and then hit the enter key to end the game round. Good luck!");
System.out.println("--------------------------------------------------------------------------------------------------------------------------");
runGame();
}
//method to run the game
public static void runGame() throws FileNotFoundException{
//array list of arrays that contains all the possible words
ArrayList<String> wholeList = new ArrayList<String>();
wholeList.add("vulnerability");
wholeList.add("calculate");
wholeList.add("virtual");
//PrintWriter and File
File results = new File("WordlerResults.txt");
PrintWriter output = new PrintWriter(results);
//list of words and their answers (as a sublist)
ArrayList<String> arr1 = new ArrayList<String>();
arr1.add("vulnerabiliy");
arr1.add("ability");
arr1.add("nearby");
arr1.add("lite");
arr1.add("near");
arr1.add("bare");
arr1.add("rule");
arr1.add("bury");
arr1.add("lair");
arr1.add("rile");
arr1.add("bear");
arr1.add("liberality");
arr1.add("virulently");
arr1.add("vulnerably");
arr1.add("inevitably");
arr1.add("tenurially");
arr1.add("inertially");
arr1.add("neutrally");
arr1.add("unlivable");
arr1.add("unitarily");
arr1.add("veniality");
arr1.add("reliantly");
arr1.add("brilliant");
arr1.add("urinative");
arr1.add("nailbiter");
arr1.add("illuviate");
arr1.add("unitively");
arr1.add("veritably");
arr1.add("trivially");
arr1.add("vibratile");
arr1.add("virtually");
// stopped at #20, www.wordplays.com/w/13810606276/vulnerability
List<String> arr1Sub = arr1.subList(1, 30);
ArrayList<String> arr2 = new ArrayList<String>();
arr2.add("calculate");
arr2.add("late");
arr2.add("call");
arr2.add("teal");
arr2.add("talc");
arr2.add("catcall");
arr2.add("tall");
arr2.add("cult");
arr2.add("lace");
arr2.add("tela");
arr2.add("acute");
arr2.add("lacteal");
arr2.add("callet");
arr2.add("acuate");
arr2.add("luteal");
arr2.add("actual");
arr2.add("cullet");
arr2.add("caecal");
arr2.add("alulae");
arr2.add("acetal");
arr2.add("alate");
arr2.add("caeca");
arr2.add("aceta");
arr2.add("eclat");
arr2.add("cecal");
arr2.add("lutea");
arr2.add("cella");
arr2.add("cleat");
arr2.add("tulle");
arr2.add("culet");
arr2.add("alula");
arr2.add("calla");
arr2.add("tale");
arr2.add("tace");
arr2.add("celt");
arr2.add("clue");
arr2.add("alec");
arr2.add("tell");
arr2.add("cull");
arr2.add("alae");
arr2.add("cate");
arr2.add("acta");
arr2.add("tule");
arr2.add("caca");
arr2.add("ceca");
arr2.add("tael");
arr2.add("latu");
arr2.add("lute");
arr2.add("caul");
arr2.add("cute");
arr2.add("luce");
arr2.add("cell");
arr2.add("tala");
List<String> arr2Sub = arr2.subList(1, 52);
ArrayList<String> arr3 = new ArrayList<String>();
arr3.add("virtual");
arr3.add("ritual");
arr3.add("vault");
arr3.add("virtu");
arr3.add("vital");
arr3.add("trial");
arr3.add("rival");
arr3.add("viral");
arr3.add("ultra");
arr3.add("urial");
arr3.add("trail");
arr3.add("aril");
arr3.add("vair");
arr3.add("tali");
arr3.add("virl");
arr3.add("lair");
arr3.add("rail");
arr3.add("airt");
arr3.add("vita");
arr3.add("lati");
arr3.add("vial");
arr3.add("alit");
arr3.add("tail");
arr3.add("lair");
arr3.add("rial");
arr3.add("vatu");
arr3.add("latu");
arr3.add("tirl");
arr3.add("ulva");
arr3.add("litu");
arr3.add("lira");
arr3.add("lari");
arr3.add("vail");
List<String> arr3Sub = arr3.subList(1, 32);
//input list
ArrayList<String> inputList = new ArrayList<String>();
Scanner input = new Scanner(System.in);
//to print the words for the game
int r = (int) (Math.random() * 2);
String word = wholeList.get(r);
System.out.println(word);
while (input.hasNextLine()){
String words = input.nextLine();
if (words.equalsIgnoreCase("x")){
break;
}
else{
inputList.add(words);
}
}
//check answers
ArrayList<String> validAnswers = new ArrayList<String>();
ArrayList<String> wrongAnswers = new ArrayList<String>();
ArrayList<String> notFound = new ArrayList<String>();
List<String> compare = new ArrayList<String>();
if (r == 0){
compare = arr1Sub;
}
else if (r == 1){
compare = arr2Sub;
}
else if(r == 2){
compare = arr3Sub;
}
else{
compare.add("error");
System.out.println(compare);
}
for (int i = 0; i < inputList.size(); i++){
if (compare.contains(inputList.get(i))){
validAnswers.add(inputList.get(i));
}
else if (!compare.contains(inputList.get(i))){
wrongAnswers.add(inputList.get(i));
}
else{
notFound.add(compare.get(i));
}
}
System.out.println("Valid Answers: " + validAnswers);
System.out.println("Wrong Answers: " + wrongAnswers);
output.println(wholeList.get(r));
output.println("Valid Answers: " + validAnswers);
output.println("Wrong Answers: " + wrongAnswers);
output.close();
System.out.println(" ");
System.out.println("Would you like to play again? (Y/N)");
String response = input.nextLine();
System.out.println(" ");
if (response.equalsIgnoreCase("y")){
repeatGame();
}
else if (response.equalsIgnoreCase("n")){
System.out.println(" ");
System.out.println("Thank you for playing!");
}
}
public static void repeatGame(){
try {
runGame();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Supposing that you want appending data by keeping the old written data in the file: Creating an instance of FileOutputStream(file, append) and wrapping it with PrintWriter should work:
PrintWriter writer = new PrintWriter(new FileOutputStream(myFile, true));
writer.write("a String");
writer.close();

put the creating file codez into the main before you start the game
the main would be like this
public static void main(String[] args) throws IOException{
...
File results = new File("WordlerResults.txt");
PrintWriter output = new PrintWriter(results);
runGame(output);
}
and the run game accept the output as input argument
public static void runGame(PrintWriter output) throws FileNotFoundException{
...
//also remove these two lines from here
// File results = new File("WordlerResults.txt");
// PrintWriter output = new PrintWriter(results);
}
but still the file is going to overridden for the next call(application run), so you just need to check the file exist state, and just markup the cursor to the end.
by the way don't forget to flush and close the output at the end of the programming life cycle as
output.flush();
output.close();

Related

How to count unique words in a text file?

I have implemented code to count number of:
- chars
- words
- lines
- bytes
in text file.
But how to count dictionary size: number of different words used in this file?
Also, how to implement iterator which can iterate over only letters? (Ignore whitespaces)
public class wc {
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File file = new File("Sample.txt");
try(Scanner scanner = new Scanner(new BufferedReader(new FileReader(file)))){
while (scanner.hasNextLine()) {
String tmpStr = scanner.nextLine();
if (!tmpStr.equalsIgnoreCase("")) {
String replaceAll = tmpStr.replaceAll("\\s+", "");
charsCount += replaceAll.length();
wordsCount += tmpStr.split("\\s+").length;
}
++linesCount;
}
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
System.out.println("# of bytes: " + file.length());
}
}
}
To get unique words and their counts:
1. Split your obtained line from file into a string array
2. Store the contents of this string array in a Hashset
3. Repeat steps 1 and 2 till end of file
4. Get unique words and their count from the Hashset
I prefer posting logic and pseudo code as it will help OP to learn something by solving posted problem.
Example of how my code works:
File with words "aa bb cc cc aa aa" has 3 unique words.
First, turn words into a string with each word separated by "-".
String: "aa-bb-cc-cc-aa-aa-"
Get the first word: "aa", set the UniqueWordCount = 1, and then replace "aa-" with "".
New String: "bb-cc-cc-"
Get the first word: "bb", set the UniqueWordCount = 2, and then replace "bb-" with "".
New String: "cc-cc-"
Get the first word: "cc", set the UniqueWordCount = 3, and then replace "cc-" with "".
New String: "", you stop when the string is empty.
private static int getUniqueWordCountInFile(File file) throws FileNotFoundException {
String fileWordsAsString = getFileWords(file);
int uniqueWordCount = 0;
int i = 0;
while (!(fileWordsAsString.isEmpty()) && !(fileWordsAsString.isBlank())) {
if (Character.toString(fileWordsAsString.charAt(i)).equals(" ")) {
fileWordsAsString = fileWordsAsString.replaceAll(fileWordsAsString.substring(0, i+1),"");
i = 0;
uniqueWordCount++;
} else {
i++;
}
}
return uniqueWordCount;
}
private static String getFileWords(File file) throws FileNotFoundException {
String toReturn = "";
try (Scanner fileReader = new Scanner(file)) {
while (fileReader.hasNext()) {
if (fileReader.hasNextInt()) {
fileReader.nextInt();
} else {
toReturn += fileReader.next() + " ";
}
}
}
return toReturn;
}
If you want to use my code just pass getUniqueWordCountInFile() the file that has the words for which you want to count the unique words.
hey #JeyKey you can use HashMap. Here I using Iterator too. You can check out this code.
public class CountUniqueWords {
public static void main(String args[]) throws FileNotFoundException {
File f = new File("File Name");
ArrayList arr=new ArrayList();
HashMap<String, Integer> listOfWords = new HashMap<String, Integer>();
Scanner in = new Scanner(f);
int i=0;
while(in.hasNext())
{
String s=in.next();
//System.out.println(s);
arr.add(s);
}
Iterator itr=arr.iterator();
while(itr.hasNext())
{i++;
listOfWords.put((String) itr.next(), i);
//System.out.println(listOfWords); //for Printing the words
}
Set<Object> uniqueValues = new HashSet<Object>(listOfWords.values());
System.out.println("The number of unique words: "+uniqueValues.size());
}
}

How to separate data types from a text file?

This is the code I have Implemented but I am getting multiple arrays of data in a incremental order but I need single arrays of data. How do I approach that ? Also is it ok to have try catch inside catch block ?
Text File:
The 1493 fox 0.4 -6.382 jumped -832722 0 1 9E-21 162 over the dog!
Eg: Double values are printed each time data is added to it.
#SuppressWarnings("resource")
Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");
ArrayList<Long> itr= new ArrayList<Long>();
ArrayList<Double> dub = new ArrayList<Double>();
ArrayList<String> str = new ArrayList<String>();
while(s.hasNext())
{
String str1=s.next();
try
{
long b=Long.parseLong(str1);
itr.add(b);
System.out.println("Integer values are ::"+itr);
}
catch(NumberFormatException e)
{
try
{
double b1=Double.parseDouble(str1);
dub.add(b1);
System.out.println("Double values are ::"+dub);
}
catch(NumberFormatException e1)
{
String b2 = (String) str1;
str.add(b2);
System.out.println("String Values are"+str);
}
}
}
}}
Expected output :
Integer values are ::[1493, -832722, 0, 1]
Double values are ::[0.4, -6.382, 9.0E-21]
String Values are[The, fox, jumped, over, the, dog!]
As #RubioRic answered, move the SOP statements outside the while loop to get the desired output.
As to other ways to get the data types, I feel your implementation is good enough and widely used. But if you want to do it in an another way then try using regex patterns to validate the string and determine the data type (not reliable) OR use the Scanner class API to determine the data type like below.
#SuppressWarnings("resource")
Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");
ArrayList<Long> itr= new ArrayList<Long>();
ArrayList<Double> dub = new ArrayList<Double>();
ArrayList<String> str = new ArrayList<String>();
while(s.hasNext())
{
if(s.hasNextLong()){
itr.add(s.nextLong());
}
else if(s.hasNextDouble()){
dub.add(s.nextDouble());
}
else{
str.add(s.next());
}
}
s.close();
System.out.println("Long values are ::" + itr);
System.out.println("Double values are ::" + dub);
System.out.println("String Values are" + str);
Just move the printing outside the loop. Got no better solution without using try-catch, sorry.
#SuppressWarnings("resource")
Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");
ArrayList<Long> itr = new ArrayList<Long>();
ArrayList<Double> dub = new ArrayList<Double>();
ArrayList<String> str = new ArrayList<String>();
while(s.hasNext()) {
String str1=s.next();
try {
long b=Long.parseLong(str1);
itr.add(b);
} catch(NumberFormatException e) {
try {
double b1=Double.parseDouble(str1);
dub.add(b1);
} catch(NumberFormatException e1) {
String b2 = (String) str1;
str.add(b2);
}
}
}
System.out.println("Integer values are ::" + itr);
System.out.println("Double values are ::" + dub);
System.out.println("String Values are" + str);

Print out words that are found in an ArrayList but not a file? [duplicate]

This question already has answers here:
String searching algorithms in Java
(5 answers)
Closed 8 years ago.
I have a spell check program that has these containing words:
Mary had a little lambb
Its fleece was white as ssnow
And everywhere that Mary wentt
The lamb was sure to ggo
The lambb, snnow wentt and ggo are purposely spelled like that.
I have written code that prints out the words that ARE found in the dictionary but cannot figure how to print out the words that AREN'T found.
Here is my code so far:
package testDelimeter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> dict = new ArrayList<String>();
File inFile = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "dict" + ".txt");
Scanner in = new Scanner(inFile);
File text = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "text" + ".txt");
Scanner s = new Scanner(text);
while(in.hasNext()){
dict.add(in.next());
}
while(s.hasNext()){
String temp = s.next();
for(int i = 0; i < dict.size(); i++){
if(temp.equalsIgnoreCase(dict.get(i))){
System.out.println(dict.get(i) + temp);
}
}
}
}
}
Use a java.util.set for your dictionary. Check if an entry exists using the method .contains(..) of the java.util.collection interface implemented by List and Sets.
You shouldn't use a List as a dictionary because a dictionary has no order and can contain duplicate entries.
The best improvement you can make is changing you List for a Set.
List is intended to be used when order is important and Set is intended to be used for unordered lists of unique elements. As you dictionary contains unique words, Set is better for your case.
Take a look at What is the difference between Set and List?
public static void main(String[] args) throws FileNotFoundException {
Set<String> dict = new HashSet<String>();
File inFile = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "dict" + ".txt");
Scanner in = new Scanner(inFile);
File text = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "text" + ".txt");
Scanner s = new Scanner(text);
while (in.hasNext()) {
dict.add(in.next().toLowerCase());
}
while (s.hasNext()) {
String temp = s.next().toLowerCase();
if (dict.contains(temp)) {
System.out.println("Found " + temp);
} else {
System.out.println("Not Found " + temp);
}
}
}
Edit: As OP wants to maintain lists, the code can be as follows:
public static void main(String[] args) throws FileNotFoundException {
List<String> dict = new ArrayList<String>();
File inFile = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "dict" + ".txt");
Scanner in = new Scanner(inFile);
File text = new File(
"C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
+ "text" + ".txt");
Scanner s = new Scanner(text);
while (in.hasNext()) {
// Notice toLowerCase()
dict.add(in.next().toLowerCase());
}
while (s.hasNext()) {
// Notice toLowerCase()
String temp = s.next().toLowerCase();
if (dict.contains(temp)) {
System.out.println("Found " + temp);
} else {
System.out.println("Not Found " + temp);
}
}
}
A quick way to do this would be to introduce a boolean variable as shown below :
boolean isWordFound;
while (s.hasNext()) {
String temp = s.next();
isWordFound = false;
for (int i = 0; i < dict.size(); i++) {
if (temp.equalsIgnoreCase(dict.get(i))) {
isWordFound = true;
System.out.println("Found"+dict.get(i)+temp);
break;
}
}
if (!isWordFound) {
System.out.println("Could not find"+temp);
}
}
That being said, your program is a bit inefficient. Using a Map instead of list is the first change you can make.

Java FileNotFoundException error as statement not in a method

I have 2 files where 1(OrderCatalogue.java) reads in contents of a external file and 2(below). But I'm having the "FileNotFoundException must be caught or declard to be thrown" error for this line "OrderCatalogue catalogue= new OrderCatalogue();" and I understand that because its not in a method. But if i try puting it in a method, the code under the "getCodeIndex" and "checkOut" methods can't work with the error message of "package catalogue does not exist". Anyone has any idea how i can edit my code to make them work? Thank you!!
public class Shopping {
OrderCatalogue catalogue= new OrderCatalogue();
ArrayList<Integer> orderqty = new ArrayList<>(); //Create array to store user's input of quantity
ArrayList<String> ordercode = new ArrayList<>(); //Create array to store user's input of order number
public int getCodeIndex(String code)
{
int index = -1;
for (int i =0;i<catalogue.productList.size();i++)
{
if(catalogue.productList.get(i).code.equals(code))
{
index = i;
break;
}
}
return index;
}
public void checkout()
{
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Your order:");
for(int j=0;j<ordercode.size();j++)
{
String orderc = ordercode.get(j);
for (int i =0;i<catalogue.productList.size();i++)
{
if(catalogue.productList.get(i).code.equals(orderc))
{
System.out.print(orderqty.get(j)+" ");
System.out.print(catalogue.productList.get(i).desc);
System.out.print(" # $"+df.format(catalogue.productList.get(i).price));
}
}
}
}
And this is my OrderCatalogue file
public OrderCatalogue() throws FileNotFoundException
{
//Open the file "Catalog.txt"
FileReader fr = new FileReader("Catalog.txt");
Scanner file = new Scanner(fr);
while(file.hasNextLine())
{
//Read in the product details in the file
String data = file.nextLine();
String[] result = data.split("\\, ");
String code = result[0];
String desc = result[1];
String price = result[2];
String unit = result[3];
//Store the product details in a vector
Product a = new Product(desc, code, price, unit);
productList.add(a);
}
It seems the OrderCatalogue constructor throws FileNotFoundException. You can initialize catalogue inside Shopping constructor and catch the exception or declare it to throw FileNotFoundException.
public Shopping() throws FileNotFoundException
{
this.catalogue= new OrderCatalogue();
or
public Shopping()
{
try{
this.catalogue= new OrderCatalogue();
}catch(FileNotFoundException e)
blah blah
}

how to reset program to main string args?

I am writing a program and if it catches an Exception I want to reset the whole program is there anyway please tell me I really need to finish it tonight ?
public static void readinfile(ArrayList<ArrayList> table,
int numberOfColumns,ArrayList<String> header,
ArrayList<ArrayList<String>> original,
ArrayList<String> sntypes, ArrayList<Integer> displaySize,
ArrayList<String> writeOut, Scanner inputStream) {
//System.out.print("enter data file: ");
Scanner keyboard = new Scanner(System.in);
System.out.print("enter data file: ");
String fileName = keyboard.nextLine();
try {
System.out.println("try " + fileName);
inputStream = new Scanner(new FileInputStream(fileName));
System.out.println(inputStream);
} catch (FileNotFoundException E) {
System.out.println("Error in opening file ");
//readinfile(table, numberOfColumns, header,
//original, sntypes,displaySize, writeOut, inputStream );
}
// file is now open and input scanner attached
if (inputStream.hasNextLine()) {
String Line = inputStream.nextLine();
Scanner lineparse = new Scanner(Line);
lineparse.useDelimiter(",");
ArrayList<String> rowOne = new ArrayList<String>();
while (lineparse.hasNext()) {
String temp = lineparse.next();
String originaltemp = temp;
writeOut.add(temp);
temp = temp + "(" + (++numberOfColumns) + ")";
displaySize.add(temp.length());
// row.add(lineparse.next());
if (temp.trim().substring(0, 2).equalsIgnoreCase("S ")
|| temp.trim().substring(0, 2).equalsIgnoreCase("N ")) {
rowOne.add(originaltemp);
header.add(temp.substring(2));
sntypes.add(temp.toUpperCase().substring(0, 2).trim());
} else {
System.out.println("Invalid file please enter a new file: ");
//readinfile(table, numberOfColumns, header, original, sntypes,displaySize,writeOut,Name);
readinfile(table, numberOfColumns, header,
original, sntypes, displaySize, writeOut, inputStream);
}
}
// add table here it gives problem later on...
original.add(rowOne);
}
while (inputStream.hasNextLine()) {
String Line = inputStream.nextLine();
Scanner lineparse = new Scanner(Line);
lineparse.useDelimiter(",");
ArrayList row = new ArrayList();
int j = 0;
while (lineparse.hasNextLine()) {
String temp = lineparse.next().trim();
int sizeOfrow = temp.trim().length();
if (sizeOfrow > displaySize.get(j)) {
displaySize.set(j, sizeOfrow);
}
if (j < numberOfColumns && sntypes.get(j).equalsIgnoreCase("N")) {
try {
if (temp.equalsIgnoreCase("")) {
row.add(new Double(0.0));
} else {
row.add(new Double(temp.trim()));
}
} catch (NumberFormatException E) {
System.out.println("Opps there is a mistake "
+ "I was expecting a number and I found: " + temp);
System.out.println("This row will be ignored");
// break;
}
} else {
if (temp.equalsIgnoreCase("")) {
row.add((" "));
} else {
row.add(temp);
}
}
j++;
}
if (row.size() == numberOfColumns) {
table.add(row);
}
}// close for while
inputStream.close();
}
homework?
Here's a clue on how to think about it:
main:
start loop
start
do stuff
set ok to end
catch exception
set not ok to end
loop if not ok to end
I'm not sure if you meant this, but the following code will run again and again until it succeeds (as in: doesn't throw an exception):
public static void main(String[] args){
while(true){
try{
// execute your code
break; // if successful, exit loop
}catch(SomeException e){
// handle exception
}catch(SomeOtherException e){
// handle exception
}finally{
// clean up, if necessary
}
}
}
Note: while(true) is an awful construct that I'm sure your teachers won't like. Perhaps you'll find a better way to rephrase that.
This is a bit of a hack but you could try calling the main method again, passing the arguments. As long as you didn't modify the string array of arguments, just call main(args); from a try/catch block in the main routine. Of course, if the exception keeps happening you'll loop infinitely and blow the stack:P

Categories