64 735
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #63025 in Java | JSP | JSF for jonney

Question #63025
write a complete method that read a text file full of integer. you must do this
1)store the integer into A database named "Numbers" if number does not exists
or
2) increase the numbers frequency if it exists
Then you should display the entire content of the database on the screen.
Assume database has the following fields:
Number(integer) Freq(int)

you can assume that a condition to database is already existenced and is referenced by "Msconnection"
Expert's answer
import java.io.*;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
Database db = new Database();

readData(db, "input.txt");

db.printList();
}

private static void readData(Database d, final String FILENAME) {
File f = new File(FILENAME);

try {
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(f)));

//reading contacts
while (true) {
if (!input.ready()) {
return;
}

//reading empty string from file
int x = Integer.parseInt(input.readLine());
d.addNum(x);
}
} catch (IOException ioex) {
System.out.println(ioex.getMessage());
}
}

}

class Numbers {
private int number;
private int freq;

Numbers(int newNumber) {
number = newNumber;
freq = 1;
}

public void incFreq() {
freq++;
}

public int getNumber() {
return number;
}

public int getFreq() {
return freq;
}
}

class Database {
private ArrayList<Numbers> list;

public Database() {
list = new ArrayList<>();
}

public void addNum(int n) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getNumber() == n) {
list.get(i).incFreq();

return;
}
}

list.add(new Numbers(n));
}

public void printList() {
for (Numbers el: list) {
System.out.println(el.getNumber() + " - " + el.getFreq());
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions