I want to write the console output to the text file, but whenever I write it\'s
ID: 3703459 • Letter: I
Question
I want to write the console output to the text file, but whenever I write it's writing only one line.
Note: inorder for it to run you need to create your own input file.txt with just a few numbers for testing.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LinkedHashEntry {
private int key;
private int value;
private PrintWriter writer;
private LinkedHashEntry next;
LinkedHashEntry(int key) {
this.key = key;
writer = null;
//this.value = value;
this.next = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getKey() {
return key;
}
public LinkedHashEntry getNext() {
return next;
}
public void setNext(LinkedHashEntry next) {
this.next = next;
}
public static void main(String[] args)throws IOException
{
int key = 0;
LinkedHashEntry myMap = new LinkedHashEntry(key);
myMap.readFile();
}
public class HashMap {
private final static int TABLE_SIZE = 128;
LinkedHashEntry[] table;
HashMap() {
table = new LinkedHashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public int get(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
return -1;
else {
LinkedHashEntry entry = table[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return -1;
else
return entry.getKey();
}
}
public void put( int key) throws IOException {
writer = new PrintWriter("P8Output.txt");
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
table[hash] = new LinkedHashEntry(key);
else {
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
//System.out.println("Value " + entry.getKey() + " Collision " + hash);
if (entry.getKey() == key) {
entry.setValue(key);
}else{
entry.setNext(new LinkedHashEntry(key));
}
System.out.println("Value " + entry.getKey() + " Collision " + hash);
writer.println("Value " + entry.getKey() + " Collision " + hash);
}
writer.close();
}
}
public void readFile()throws IOException{
HashMap myMap = new HashMap();
String fileName = "numbers.txt";
int integerContent = 0;
File file = new File(fileName);
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
integerContent = sc.nextInt();
myMap.put(integerContent);
}
sc.close();
}catch(FileNotFoundException fnf){
System.out.println("File not found.");
System.exit(0);
}
catch (Exception e) {
System.out.println(" Program terminated Safely...");
}
}
}
Explanation / Answer
Here is your solution:
You re-open the file each time round the loop and this overwrites the file; it does not append to it. you should open and close the output file outside the loop.
So, you should remove pritwriter logic from put() method and place it readFile() method. Then finally your read method will be like this.
public void readFile() throws IOException {
HashMap myMap = new HashMap();
String fileName = "numbers.txt";
writer = new PrintWriter("P8Output.txt");
int integerContent = 0;
File file = new File(fileName);
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
integerContent = sc.nextInt();
myMap.put(integerContent);
}
sc.close();
writer.close();
} catch (FileNotFoundException fnf) {
System.out.println("File not found.");
System.exit(0);
}
catch (Exception e) {
System.out.println(" Program terminated Safely...");
}
}
Lines to be removed from put method :
writer = new PrintWriter("P8Output.txt");
writer.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.