there are 12 excel files that contain different products sales data, I try to sum up each month and entire year's sales using java but I can't add up sales for the whole year. Is there a way to do that? Thanks for your help.
public class Main {
public static void listOfSales(String fileName,String month){
List<PriceList> list = readsListFroExcel(fileName);
int sumOfInYearSale = 0;
int sumOfInOnlineSale =0;
int sumOfTotalMonthlySale = 0;
for (PriceList x : list){
sumOfInYearSale = sumOfInYearSale + x.getTotalPhysicalSale();
sumOfInOnlineSale = sumOfInYearSale + x.getTotalOnlineSale();
}
System.out.println(month +" Physical Sales : "+sumOfInYearSale);
System.out.println(month+ " Online Sales :" +sumOfInOnlineSale);
}
private static List<PriceList> readsListFroExcel(String fileName) {
List<PriceList> list = new ArrayList<>();
Path pathTofile = Paths.get(fileName);
try (BufferedReader br = Files.newBufferedReader(pathTofile)){
String headerLine = br.readLine();
String line = br.readLine();
while (line!=null){
String[] attributes = line.split(",");
PriceList listOfPrice = createList(attributes);
list.add(listOfPrice);
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
Related
Hi. I'm having the issue in an error of exception. I don't know what is wrong. But please help me fix this. I'm trying to store data from the file to ArrayList and display the data in the ArrayList. Here, I attached my code and data Code and data source.
the NoSuchElementException appears because you are calling input.nextToken(); while input doesn't have any token.It's due to the last empty line of your file listbook.txt. By deleting this line, the exception shouldn't appear.
A proper manner could be to ensure that you have sufficient tokens to retrieve all your fields for a given line.
public class TextFile
{
public static void main(String[] args)
{
try
{
FileReader read = new FileReader("C:\\Users\\ogawi\\Downloads\\listbook.txt");
BufferedReader bf = new BufferedReader(read);
Book B = new Book();
ArrayList<Book> bookList = new ArrayList<Book>();
String data = null;
StringTokenizer input = null;
while((data = bf.readLine()) != null)
{
input = new StringTokenizer(data,";");
//we ensure that we have enough tokens to retrieve all fields
if(input.countTokens() == 6)
{
String title = input.nextToken();
String author = input.nextToken();
String publisher = input.nextToken();
String genre = input.nextToken();
int year = Integer.parseInt(input.nextToken());
int page = Integer.parseInt(input.nextToken());
B = new Book(title, author, publisher, genre, year, page);
bookList.add(B);
}
}
//This part of code has been moved outside the while loop
//to avoid to print the total content of the array each time
//an element is added
int count=0;
for(int i=0;i<bookList.size();i++)
{
B = (Book)bookList.get(i);
System.out.println(B.toString());
System.out.println("=============================");
count++;
}
System.out.println("Number of Books: " + count);
bf.close();
}
catch(FileNotFoundException fnf)
{ System.out.println(fnf.getMessage());}
catch(EOFException eof)
{ System.out.println(eof.getMessage());}
catch(IOException io)
{ System.out.println(io.getMessage());}
finally
{ System.out.println("System end here..TQ!!");}
}
}
This issue is due to the extra line without book information:
You can see the line 31 and 32 in above figure.
To solve this issue you can add one if condition data.contains(";") . Text file has ; delimiter if we check the condition if given line has ; delimiter then it won't cause an issue.
while ((data = bf.readLine()) != null) {
if (data.contains(";")) {
input = new StringTokenizer(data, ";");
String title = input.nextToken();
String author = input.nextToken();
String publisher = input.nextToken();
String genre = input.nextToken();
int year = Integer.parseInt(input.nextToken());
int page = Integer.parseInt(input.nextToken());
B = new Book(title, author, publisher, genre, year, page);
bookList.add(B);
int count = 0;
for (int i = 0; i < bookList.size(); i++) {
B = (Book) bookList.get(i);
System.out.println(B.toString());
System.out.println("=============================");
count++;
}
System.out.println("Number of Books: " + count);
}
}
Here is the screenshot for successful execution of the code.
I am trying to read an input file. Each value of the input file is inserted into the TreeMap as
If word is not existing: Insert the word to the treemap and associate the word with an ArrayList(docId, Count).
If the Word is present in the TreeMap, then check if the current DocID matches within the ArrayList and then increase the count.
THe
For the ArrayList, I created another class as below:
public class CountPerDocument
{
private final String documentId;
private final int count;
CountPerDocument(String documentId, int count)
{
this.documentId = documentId;
this.count = count;
}
public String getDocumentId()
{
return this.documentId;
}
public int getCount()
{
return this.count;
}
}
After that, I am trying to print the TreeMap into a text file as <DocID - Count>
Not sure what I am doing wrong here, but the output I get is as follows:
The Stem is todai:[CountPerDocument#5caf905d, CountPerDocument#27716f4, CountPerDocument#8efb846, CountPerDocument#2a84aee7, CountPerDocument#a09ee92, CountPerDocument#30f39991]
Wondering if anyone can guide me what i am doing wrong and if my method isn't correct what am i supposed to do?
public class StemTreeMap
{
private static final String r1 = "\\$DOC";
private static final String r2 = "\\$TITLE";
private static final String r3 = "\\$TEXT";
private static Pattern p1,p2,p3;
private static Matcher m1,m2,m3;
public static void main(String[] args)
{
BufferedReader rd,rd1;
String docid = null;
String id;
int tf = 0;
//CountPerDocument cp = new CountPerDocument(docid, count);
List<CountPerDocument> ls = new ArrayList<>();
Map<String,List<CountPerDocument>> mp = new TreeMap<>();
try
{
rd = new BufferedReader(new FileReader(args[0]));
rd1= new BufferedReader(new FileReader(args[0]));
int docCount = 0;
String line = rd.readLine();
p1 = Pattern.compile(r1);
p2 = Pattern.compile(r2);
p3 = Pattern.compile(r3);
while(line != null)
{
m1 = p1.matcher(line);
m2 = p2.matcher(line);
m3 = p3.matcher(line);
if(m1.find())
{
docid = line.substring(5, line.length());
docCount++;
//System.out.println("The Document ID is :");
//System.out.println(docid);
line = rd.readLine();
}
if(m2.find()||m3.find())
{
line = rd.readLine();
}
else
{
if(!(mp.containsKey(line))) // if the stem is not on the TreeMap
{
//System.out.println("The stem is not present in the tree");
tf = 1;
ls.add(new CountPerDocument(docid,tf));
mp.put(line, ls);
line = rd.readLine();
}
else
{
if(ls.indexOf(docid) > 0) //if its last entry matches the current document number
{
//System.out.println("The Stem is present for the same docid so incrementing docid");
tf = tf+1;
ls.add(new CountPerDocument(docid,tf));
line = rd.readLine();
}
else
{
//System.out.println("Stem is present but not the same docid so inserting new docid");
tf = 1;
ls.add(new CountPerDocument(docid,tf)); //set did to the current document number and tf to 1
line = rd.readLine();
}
}
}
}
rd.close();
System.out.println("The Number of Documents in the file is:"+ docCount);
//Write to an output file
String l = rd1.readLine();
File f = new File("dictionary.txt");
if (f.createNewFile())
{
System.out.println("File created: " + f.getName());
}
else
{
System.out.println("File already exists.");
Path path = Paths.get("dictionary.txt");
Files.deleteIfExists(path);
System.out.println("Deleted Existing File:: Creating New File");
f.createNewFile();
}
FileWriter fw = new FileWriter("dictionary.txt");
fw.write("The Total Number of Stems: " + mp.size() +"\n");
fw.close();
System.out.println("The Stem is todai:" + mp.get("todai"));
}catch(IOException e)
{
e.printStackTrace();
}
}
}
You didn't define the function String toString() in your class CountPerDocument. So, when you try to print a CountPerDocument variable, the default printed value is CountPerDocument#hashcode.
To decide how to represent a CountPerDocument variable in your code, add in your class the next function:
#Override
public String toString() {
return "<" + this.getDocumentId() + ", " + this.getCount() + ">";
}
Try to override toString method in CountPerDocument. Something like this:
public class CountPerDocument
{
private final String documentId;
private final int count;
CountPerDocument(String documentId, int count)
{
this.documentId = documentId;
this.count = count;
}
public String getDocumentId()
{
return this.documentId;
}
public int getCount()
{
return this.count;
}
#Override
public String toString() {
return documentId + "-" + count;
}
}
I have a text file, in which each line is an Movie instance, and Movie object's fields are separated by a tab.
I need to read it and return an array of object (each line), that has multiple fields. I don't know how to make the array of Movie object ( i.e. Movie[]) and return it.
Sample text file I'm reading:
id title price
001 titanic 2
002 lady bird 3
The following is what I've got so far.
public class Loader {
//private String csvFile;
private static final Resource tsvResource = new ClassPathXmlApplicationContext().getResource("classpath:movies.txt");
private static InputStream movieIS = null;
public Loader() {
try {
movieIS = tsvResource.getInputStream();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Movie[] loadMovies() {
BufferedReader br = null;
String line = "";
String[] tempArray = new String[100];
int id;
String title;
String rating;
String synopsis;
String genre;
String director;
String[] actors;
int price;
int runtime;
int index = 0;
try {
br = new BufferedReader(new InputStreamReader(movieIS));
while ((line = br.readLine()) != null) {
index++;
String[] data = line.split("\\t");
id = Integer.parseInt(data[0]);
title = data[1];
rating = data[2];
synopsis = data[3];
genre = data[4];
director = data[5];
actors = data[6].split(";");
price = Integer.parseInt(data[7]);
runtime = Integer.parseInt(data[8]);
}
String[] lines = new String[index];
for (int i = 0; i < index; i++) {
lines[i] = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
}
You've almost got this. You can create the Movie object from the fields you've extracted (like title, rating , synopsis, actors etc.) and add them to your array.
Also I'd suggest you to use ArrayList instead of array for your movies (unless you're absolute sure of the number of movies you'd have)
Your loadMovies method would look like this:
public static List<Movie> loadMovies() {
// Initialize your movie list
List<Movie> movieList = new ArrayList<>();
String line = "", title, rating, synopsis, genre, director;
int id, price, runtime, index = 0;
String[] actors;
try (BufferedReader br = new BufferedReader(new InputStreamReader(movieIS))) {
while ((line = br.readLine()) != null) {
index++;
String[] data = line.split("\\t");
id = Integer.parseInt(data[0]);
title = data[1];
rating = data[2];
synopsis = data[3];
genre = data[4];
director = data[5];
actors = data[6].split(";");
price = Integer.parseInt(data[7]);
runtime = Integer.parseInt(data[8]);
// Create your Movie object here,
// note that I'm using constructor here,
// You can also use setters for optional fields as well
Movie movie = new Movie(id, title, rating, synopsis, genre, director, actors, price, runtime);
movieList.add(movie);
}
String[] lines = new String[index];
for (int i = 0; i < index; i++) {
lines[i] = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
//return movieList
return movieList;
}
Note that I've combined the variable declarations and try-catch blocks as well in your original code.
Do something like
ArrayList <> al = new ArrayList<Movie>();
int index = 0;
try{
br=new BufferedReader(new InputStreamReader(movieIS));
while((line=br.readLine())!=null){
index++;
String[] data=line.split("\\t");
id =Integer.parseInt(data[0]);
title=data[1];
rating=data[2];
synopsis=data[3];
genre=data[4];
director=data[5];
actors=data[6].split(";");
price= Integer.parseInt(data[7]);
runtime=Integer.parseInt(data[8]);
Movie mv = new Movie();
// load into mv
al.add(mv);
}
}
and return at the end like this:
return al.toArray();
For my program I have it set up that I can edit and change values of items that are stored within my outfile in the program itself. However the numbers that they change to only update in the program itself. For example if I sell 10 ketchups than in my program i would have 0 but my outfile would still say I have 10. I need my outfile to update with my program. I came up with an override method but all it does currently is adds content on a new line within the outfile, I am not sure how I would go about actually updating any information stored on the outfile any help would be great.
Code:
public class Driver {
public static ArrayList<Item> list = new ArrayList<Item>();
static double myBalance = 100;
/*static ArrayList<Item> list = new ArrayList<Item>();*/
/**
* #param args
* #throws IOException
* #throws FileNotFoundException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ArrayList<String> inventoryList = new ArrayList<String>();
BufferedReader readIn = null;
try {
readIn = new BufferedReader(new FileReader("inventory.out"));
readIn.lines().forEach(inventoryList::add);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(readIn != null) {
readIn.close();
}
}
for (int i = 0; i < 4; i++) {
String item = inventoryList.get(i);// input String like the one you would read from a file
String delims = "[,]"; //delimiter - a comma is used to separate your tokens (name, qty,cost, price)
String[] tokens = item.split(delims); // split it into tokens and place in a 2D array.
String name = tokens[0]; System.out.println(name);
double cost = Double.parseDouble(tokens[1]);System.out.println(cost);
int qty = Integer.parseInt(tokens[2]);System.out.println(qty);
double price = Double.parseDouble(tokens[3]);System.out.println(price);
list.add(new Item(name, cost, qty, price));
}
sell("Mayo", 10);
buy("Ketchup", 20);
remove_item("Ketchup");
add_item("Tums", 20, 10, 5);
overwrite("New line");
PrintAll();
}
// Method to sell items from the arraylist
public static void sell(String itemName, int amount) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(itemName)) {
int number = i;
list.get(number).qty -= amount;
myBalance += list.get(number).getPrice() * amount;
}
}
}
// Method to buy more of the items in our array list
public static void buy(String itemName, int amount) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(itemName)) {
int number = i;
list.get(number).qty += amount;
myBalance -= list.get(number).getPrice() * amount;
}
}
}
// Method to remove an item completely from our inventory
public static void remove_item(String itemName) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(itemName)) {
int number = i;
list.remove(number);
}
}
}
public static void add_item(String itemName, double itemCost, int qty, double itemPrice) {
list.add(new Item(itemName, itemCost, qty, itemPrice));
}
public static void PrintAll() {
String output = "";
for(Item i : list) {
int everything = i.getQty();
String everything2 = i.getName().toString();
output += everything +" "+ everything2 + "\n";
}
JOptionPane.showMessageDialog(null, "Your current balance is: $" + myBalance + "\n" + "Current stock:" + "\n" + output);
}
public static void overwrite(String update) {
try
{
String filename= "inventory.out";
FileWriter fw = new FileWriter(filename,true); //the true will append the new data
fw.write("\n"+"add a line");//appends the string to the file
fw.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
}
Outfile contents:
Ketchup,1,10,2
Mayo,2,20,3
Bleach,3,30,4
Lysol,4,40,5
If you know the name of your outfile then clear the outfile as and when you need it updated and then write to it again. You can use the below code to erase content of a file.
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
Good day!
I am making a mini bookstore program and we are required to read a file based from what the customer buys on the specified counter as follows:
counter 4,book1 2,book2 2,book3 2,tender 100.00
counter 1,book1 2,book2 1,book3 3, book4 5,tender 200.00
counter 1,book3 1,tender 50.00
In short the format is:
COUNTER -> ITEMS BOUGHT -> TENDER
I tried doing this but it is not that efficient:
public List<String> getOrder(int i) {
List <String> tempQty = new ArrayList<String>();
String[] orders = orderList.get(0).split(",");
for (String order : orders) {
String[] fields = order.split(" ");
tempQty.add(fields[i]);
}
return tempQty;
}
How can i read the file and at the same time, ensures that I will put it on the correct array? Besides the counter and the tender, I need to know the name of the book and the qty so I could get its price and computer for the total price. Do I need to do multiple arrays to store each values? Any suggestions/ codes will be
highly appreciated.
Thank you.
Map<String, Integer> itemsBought = new HashMap<String, Integer>();
final String COUNTER = "counter";
final String TENDER = "tender";
String[] splitted = s.split(",");
for (String str : splitted) {
str = str.trim();
if (str.startsWith(COUNTER)) {
//do what you want with counter
} else if (str.startsWith(TENDER)) {
//do what you want with tender
} else {
//process items, e.g:
String[] itemInfo = str.split(" ");
itemsBought.put(itemInfo[0], Integer.valueOf(itemInfo[1]));
}
}
How about this? Here we have a class that is modeling a purchase, containing counter, tender and a list of the bought items. The bought item consists of an id (e.g. book1) and a quantity. Use the method readPurchases() to read the contents of a file and get a list of purchases.
Does this solve your problem?
public class Purchase {
private final int counter;
private final List<BoughtItem> boughtItems;
private final double tender;
public Purchase(int counter, List<BoughtItem> boughtItems, double tender) {
this.counter = counter;
this.boughtItems = new ArrayList<BoughtItem>(boughtItems);
this.tender = tender;
}
public int getCounter() {
return counter;
}
public List<BoughtItem> getBoughtItems() {
return boughtItems;
}
public double getTender() {
return tender;
}
}
public class BoughtItem {
private final String id;
private final int quantity;
public BoughtItem(String id, int quantity) {
this.id = id;
this.quantity = quantity;
}
public String getId() {
return id;
}
public int getQuantity() {
return quantity;
}
}
public class Bookstore {
/**
* Reads purchases from the given file.
*
* #param file The file to read from, never <code>null</code>.
* #return A list of all purchases in the file. If there are no
* purchases in the file, i.e. the file is empty, an empty list
* is returned
* #throws IOException If the file cannot be read or does not contain
* correct data.
*/
public List<Purchase> readPurchases(File file) throws IOException {
List<Purchase> purchases = new ArrayList<Purchase>();
BufferedReader lines = new BufferedReader(new FileReader(file));
String line;
for (int lineNum = 0; (line = lines.readLine()) != null; lineNum++) {
String[] fields = line.split(",");
if (fields.length < 2) {
throw new IOException("Line " + lineNum + " of file " + file + " has wrong number of fields");
}
// Read counter field
int counter;
try {
String counterField = fields[0];
counter = Integer.parseInt(counterField.substring(counterField.indexOf(' ') + 1));
} catch (Exception ex) {
throw new IOException("Counter field on line " + lineNum + " of file " + file + " corrupt");
}
// Read tender field
double tender;
try {
String tenderField = fields[fields.length - 1];
tender = Double.parseDouble(tenderField.substring(tenderField.indexOf(' ') + 1));
} catch (Exception ex) {
throw new IOException("Tender field on line " + lineNum + " of file " + file + " corrupt");
}
// Read bought items
List<BoughtItem> boughtItems = new ArrayList<BoughtItem>();
for (int i = 1; i < fields.length - 1; i++) {
String id;
int quantity;
try {
String bookField = fields[i];
id = bookField.substring(0, bookField.indexOf(' '));
quantity = Integer.parseInt(bookField.substring(bookField.indexOf(' ') + 1));
BoughtItem boughtItem = new BoughtItem(id, quantity);
boughtItems.add(boughtItem);
} catch (Exception ex) {
throw new IOException("Cannot read items from line " + lineNum + " of file " + file);
}
}
// We're done with this line!
Purchase purchase = new Purchase(counter, boughtItems, tender);
purchases.add(purchase);
}
return purchases;
}
}