I am reading Excel data using java apache. I got format issue while reading double value such as 869.87929 (in excel) into 869.8792899999999 (in java).
if(type.equals("Double")){ String str = content[i-1]; //System.out.println(str); BigDecimal d = new BigDecimal(str); listObjects.add(d); }
Note: type from schema.csv & content [] value from file.xls If I print **str**, it shows value as 869.8792899999999. But i need to get **str** value as 869.87929. How can I get it?
The basic gist is it's "A program that reads in a text file that uses a specific input format and uses it to produce a formatted report for output."
Specifically :"For this lab you will write a Java program that produces a simple formatted report. The program will prompt the user to enter a file name. This file must contain information in a specific format (detailed below). Each "block" of the file contains information for one player in a competition -- the name of the player followed by a number of different scores that that player achieved. The program should find each player's average score, median score and best and worst scores and display them in a line on the final summary report. The program should also determine which player has the highest average score and which player has the lowest average score."
I get the following errors when I try and compile it:
Enter an input file name: Project11.txt Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException... -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Project11.getMedian(Project11.java:68) at Project11.main(Project11.java:27)
I get that the error(s) reside in lines 68 and 27, among problem other areas, but I'm not exactly sure how I can fix them.
Here's my code:
import java.io.*; import java.util.*; public class Project11 { public static void main(String[] args) throws IOException{ Scanner in = new Scanner(System.in); System.out.print("Enter an input file name: "); String input = in.nextLine();
I am creating a program where it reads the data inside a file and then places this data into arrays. The file I created has numbers 1-30 in it, file named, testing1.txt .
When I am trying to read data from BufferedReader and writing into excel using FileOutputStream object with POI APIs then i am getting the data in excel file in bad formats. you can check the log file and excel file attached for more information.
Here my problem is I cannot use BufferedWriter in place of FileOutputStream because POI class XSSFWorkbook only have one write method and we can only pass FileOutputStream class object there.
I can read the data to a monitor perfectly. But, I'm having problem reading data from an external file into an array of class objects. Here's my attempt at writing the method:
private void openFile() //This method asks the user to enter a file name(including the file extension) and then //Sets the data to an array of Product type { String fileName, storeName="", emptyLine1="", emptyLine2="", name="", productName=""; int demandRate=0; double setupCost=0.0, unitCost=0.0, inventoryCost=0, sellingPrice=0; Scanner inputStream = null;
The intent of the code is to read date from a file, does calculation and then displays that data in a table format on the screen. Then creates another file with those values:
Reads file: Beginningbalance.txt Displays Data with calculation Creates a file called "Newbalance.txt" with the following values:
111 251.41 222 402.00
With the way the code is written I can get it to create the file but it only displays one of the customers (111). I know that I need to create a loop but I am not sure how to build that. I tried creating another while loop and changing it to outFile but that was without success.
import java.io.*; import java.util.Scanner; import java.text.DecimalFormat; public class Output { public static void main(String[]args) throws IOException {
I have to read data from a text file and print it in a new text file. An example of one line is like this: Johnson 85 98 75 89 82
I then have to take the average of all the numbers and assign a "grade" to the numbers for each line of the text file and make a new file so it looks like this for 10 lines:
Name 1 2 3 4 5 Average Grade
Johnson 85 98 75 89 82 85.80 B
My problem is extracting the data from the file so I can use it.
I'm a complete beginner in Java programming and I'm interested to learn more about its concepts.
Recently, I've been given an exercise which instructs me to display two versions of a picture. The picture to be displayed is provided in the form of a data file of 40,000 digits that are arranged in rows (although there is no marker between rows) and it starts from the top of the picture. So the first digit represents the top left corner of the picture and the last is the bottom right.
Basically, what the exercise wants me to construct a program that plots a dot in one of two colours for each digit. If the digit is in the range 0 to 3 the output should be one colour and for digits in the range 4 to 9 the dot should be in the other colour.
I understand I have to use arrays and also loops to perform this. I'm familiar with the fillEllipse, drawEllipse, drawRectangle and fillRectangle but this exercise is nothing I've attempted before.
We have an autosys job running in our production on daily basis. It calls a shell script which in turn calls a java servlet. This servlet reads these files and inserts the data into two different tables and then does some processing. Java version is 1.6 & application server is WAS7 and database is oracel-11g.
We get several issues with this process like it takes time, goes out of memory etc etc. Below are the details of the way we have coded this process.
1. When we read the file using BufferedReader, do we really get a lot of strings created in the memory as returned by readLine() method of BufferedReader? These files contain 4-5Lacs of line. All the records are separated by newline character. Is there a better way to read files in java to achieve efficiency? I couldnt find any provided the fact that all the record lines in the file are of variable length.
2. When we insert the data then we are doing a batch process with statement/prepared statement. We are making one batch containing all the records of the file. Does it really matter to break the batch size to have better performance?
3. If the tables has no indexes defined nor any other constraints and all the columns are VARCHAR type, then which operation will be faster:- inserting a new row or updating an existing row based upon some matching condition?
I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)
1 FOR SALE: Emmalaan 23 3051JC Rotterdam 7 rooms buyprice 300000 energylevel C
The user gets (let's say for simplicity) 3 options:
1. Add a House to the file, 2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and 3. Close the application.
This is how I start:
Scanner sc = new Scanner (System.in); while (!endLoop) { System.out.println("Make a choice); System.out.println("1) Add House"); System.out.println("2) Show Houses"); System.out.println("3) Exit"); int choice = sc.nextInt();
Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).
My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this
first row abc has width as 6 and FLof as 2 in file A, so it occupies position 2,3,4,5,6,7 accounting to 6 position so represented as abc_7_2 in file B
Now row three of file A has duplicate of abc so now the new offset will be previous offset plus one ie. position will start from 8 and it goes as follows 8,9,10,11 so represented as abc_11_8
This will create unique and informative names, the same goes with other rows :
In my project i am facing an problem, The My SQL Data base will accept the date format of yyyy/mm/dd only as "Date" data type but in my program i wants to use dd/mm/yyyy format. (i have this same format now) that's why I am unable to insert / retrieve it..
I have two programs that talk to each other over a TCP connection. When I write the data "STX+1234" where STX is the Ascii character STX or Ctrl B and I expect the written String length to be 6 which it is. On the other side of the socket I create the ServerSocket and use the client socket's InputStream to create a BufferedReader. When I receive the string it now has 12 characters where each original character has been replaced by NUL and the character. How do I read the string as it is originally specified without the conversion? And is the problem on the reader side or the writer side?
Ok so I know how to import a csv using java. What I'm curious about doing is importing a csv using file chooser, reading the data, sorting the data out by certain parameters, and then outputting a count of each parameter I chose. Say for example I have columns 1,2 and 3. Column 1 has the name, column 2 has the percentage, column 3 has an o'clock time. I want to use queries to sort through the parameters and have a count of each parameter I choose...
A method in Java returns a schema. In .net, we use DataTable to get the data from the schema. How to write the following code in Java:
/* Result r = method(); r.DataSchema; */ using (DataTable tbl = new DataTable()) { using (MemoryStream ms = new MemoryStream()) using (StreamWriter sr = new StreamWriter(ms))
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data. Here is the base Product class that must be used to create the objects for the array.
public class Product { public String pName; public String stringName; public double price; public int quanity;
[Code]...
these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....
I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...
The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.
Here is the base Product class that must be used to create the objects for the array.
public class Product { public String pName; public String stringName; public double price; public int quanity; //Constructor public Product( String pName, double price, int quanity )
[code]....
and then here is the data from the text file that i must extract to use to create product objects.
Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19
these continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quanity after the second comma.....
The problem is that I often receive ArrayOutOfBound errors when I try to parse the byte array. I think that I'm reading from the socket in a wrong way..
How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap? Currently my program reads the data from several text files in a directory and the saves that information in a text file called output.txt. I would like to cache this data in order to use it later. How can I do this using the TreeMap Class? These are the keys,values: TreeMap The data I'd like to Cache is (date from the file, time of the file, current time).
import java.io.*; public class CacheData { public static void main(String[] args) throws IOException { String target_dir = "C:Files"; String output = "C:Filesoutput.txt"; File dir = new File(target_dir); File[] files = dir.listFiles();
Is there anyway to format my input file "IF1.txt" into an excel/csv file, but with the content rearranged into a desired format? (all of this has to be done by a java program)
In Java® identifiers, you are allowed letters and numbers (also _ $£¢€ etc, but you should avoid them in normal identifiers). So you cannot have spaces. You cannot write public class Hello World because the javac tool will see World as a separate identifier and not understand what it means and will fail to compile the code. You must write public class HelloWorld instead. And because the class is labelled public you must call the source file Hello World. java. Since you can't have two classes with the same [fully‑qualified] name, you cannot write two public classes in the same source file.
“What about names of source files?” somebody will ask. Well, some file systems will permit spaces in file names; ext4 will and I suspect so will NTFS. Can you write file names with spaces in? You would have to have a different name of the class inside the file, because you can't have spaces, and you therefore cannot make the class public, but maybe you can write a package‑private class with a different name.
On ext4, you have to write out the name of the file and the shell will interpret the space as meaning there are two different file, so you have to escape the space.
campbell@campbellsComputer:~/java$ gedit My First Class.java // My First Class.java class Foo { public static void main(String... args) { System.out.println("Hello, World!"); } } campbell@campbellsComputer:~/java$ javac My First Class.java campbell@campbellsComputer:~/java$ java Foo