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"
1
Expert's answer
2016-11-22T12:59:20-0500
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()); } } }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment