Reading Data From A File And Setting It To Array

Jun 5, 2014

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;

[code]...

View Replies


ADVERTISEMENT

Reading Data From A File And Putting That Data In Arrays

Feb 8, 2015

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 .

Java Code:

public static void main(String[] args) {
// TODO Auto-generated method stub
// Variables Declaration Section
//******************************
BufferedReader br;
String sCurrentLine;
String str;

[Code] ....

My issue is that the 'str' value is not initialized, but if I initialize it, it ruins the code. I'm not sure what to do in the situation.

View Replies View Related

Reading Tabular Format Data From PDF File

Feb 20, 2015

I am working on a project where as an input I need to read to data from PDF.

The PDF data will look like

As shown below:

Serial NoName of bookQuanity of BooksLibrary NamePrice

1aaa1London

2bb2Newyork

3cc1Paris

Total number of books4

Payment info

The number of books are dynamic in every pdf which I am going to receive.

View Replies View Related

Reading Different Types Of Data From A Single Line Of A File?

Oct 12, 2014

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.

View Replies View Related

Reading Text File Into Object Array And Create Random Access File

Dec 9, 2014

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.....

View Replies View Related

Reading Text File Into Object Array And Creating Random Access File?

Dec 8, 2014

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.....

View Replies View Related

Producing Image By Reading From A Data File Filled With Digits

Nov 16, 2014

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.

View Replies View Related

How To Improve File Reading Efficiency And Its Data Insertion In Java

Feb 8, 2014

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?

View Replies View Related

I/O / Streams :: Reading From And Writing To A File Then Read Again Including Data Written

Oct 11, 2014

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

View Replies View Related

Reading From File And Sort Data Based On Person GPA - Catching Error

Dec 9, 2014

I am catching an error in my driver class that reads from a file and sorts the data based on a person's GPA. Here is my code:

import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);

[Code] ....

Why throwing this exception?

View Replies View Related

Reading A File In A String Array?

Mar 22, 2015

How do I read in a file line by line into an array without using arraylist?

I know how to do this using BufferedReader, but I am wondering how to do this using Scanner? When I used BufferedReader I noticed that there must be two exceptions to be caught which were IOException and FileNotFoundException, whereas a Scanner needs only a FileNotFoundException, why is that?

Java Code: public class practice {
public String[] array;
Scanner inputStream = null;
Scanner n = new Scanner(System.in);
public String line;

[Code].....

View Replies View Related

Java Reading Txt File Into Array

May 5, 2015

I'm currently working on a program what is reading a txt file with a bunch of numbers in it but keep getting an error back.

Validator.java:

import java.io.IOException;
import java.io.File;
import java.util.Scanner;
public class Validator {
public int[] accounts = {};

[Code] ....

Accounts.txt:

5658845
8080152
1005231
4520125
4562555
6545231
7895122
5552012
3852085
8777541
5050552
7576651
8451277
7825877
7881200
1302850
1250255
4581002

View Replies View Related

Reading Records From TXT File And Storing It Into Array

Apr 15, 2014

I am reading records from a txt file and storing it into an array

import java.util.*;
import java.io.*;
 public class PatientExercise {
//patients exercise time
public static void main (String[]args) throws IOException{
Scanner in = new Scanner(new FileReader("values.txt"));
double [] patientTimeRecords = new double [300];
int noExerciseCount=0, numPatients =0;
double highest=0, lowest=0, avg=0, totalTime=0;
 
[Code] ....

However an error msg keeps popping up:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at pastpapers.PatientExercise.main(PatientExercise.ja va:44)

line 44 is:patientTimeRecords[i]= in.nextDouble();

View Replies View Related

Array Of Linked List / Reading From File - Index Out Of Bounce

Oct 17, 2014

I'm trying to read from a file. we made an array of LinkedList and when I'm reading from the file i get a runtime error "index out of bounce in line 66"

import java.lang.*;
import java.util.*;
public class HashTester{
LinkedList_t [] hash;
LinkedList_t [][] doubleHasher;
int size;

[Code] .......

View Replies View Related

Reading From Text File - Array Index Out Of Bounds Exception

Oct 21, 2014

I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?

import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);

[Code] ....

I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?

View Replies View Related

Reading Entire Integer Text File And Putting Inside Array?

Apr 5, 2014

I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.

System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int
int count= data.length;
for (int i=0; i<data.length-1;i++) {
data[i]=scan.nextInt();
}
System.out.print(Arrays.toString(data));

rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file

View Replies View Related

Read Text File Into Array Ask User To Save File And Print Data

Jul 14, 2014

New to programming. Am supposed to create a program that reads a text file (of integers) and computes a series of computations on these integers. I don't have the code for the integers in my code yet, (i know how to do those), but am struggling getting the array to simply print in the print writer. I have the user select a text file, read the file with a scanner, and then save the computations done from my code into another file. specifically, the problem is as follows: Write a program that uses a file chooser dialog to select a file containing some integers. The file contains an integer N followed by N integers. The program then uses a file chooser dialog to let the user specify the name and location of an output file to write results to.The data written to the output file will be as follows

(1) The original list of N numbers from the input file,
(2) The original list of N numbers printed in reverse order of how they appear
in the input file.
(3) The sum and average of these numbers,
(4) The minimum of all the numbers,
(5) The maximum of all the numbers.

[import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

[Code]....

View Replies View Related

File Reading Values To Draw Image (java Graphics DrawString / ArrayList / Array)

Sep 13, 2014

//compiler error I'm receiving
J:CS3Student Folder Review Lab #2APlusImage.java:41: error: cannot find symbol
xcor.add(read.nexInt());

[Code].....

View Replies View Related

Retrieving Data From DB And Setting Into Textfield

Jul 10, 2014

How to do this <not in sql> .

View Replies View Related

ArrayList Only Adding 1 Item Even Though There Are Multiple Items (Setting Data To Files)

Oct 25, 2014

I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my

set("", "");

method, it only sets the last one that is called.

Example:

set("section1", "section1Item");
set("section2", "section2Item");
set("section3", "section3Item");

Only section3 would get set, and not the others.

Otherwise, if the file contained a section already, then each section (section1, section2, section3) would get set.

Here's how I set the data to the file:

public static void set(String section, String data) {
files.openFileWriter();
files.file.delete();
reCreateFile();
String beforeItem = section + ":" + files.dataList.get(section);

[code]....

And here is how a retrieve the data and set them to my arraylist/hashmap:

public void getData() {
String line = null;
openFileReader();
StringBuffer sb = new StringBuffer();

[code]....

View Replies View Related

Store Data From A File Into Array Of Type Product?

Mar 17, 2015

public class InputFileData {
/**
* @param inputFile a file giving the data for an electronic
* equipment supplier’s product range
* @return an array of product details
* @throws IOException
*/
public static Product [] readProductDataFile(File inputFile)
throws IOException{
// YOUR CODE HERE
}

This code is meant to be used to read a text file and store the data in an array of type Product[]. I know how to read in a text file and have it sort it into an array, but I've never seen code laid out in this fashion before (specifically "public static Product[]", and I'm unsure how to work with "(File inputfile)". I've looked all over the place but can't find any examples of anything like this.

Also, the code given cannot be changed, as it's that code I have to work with.

I still don't understand how to use it as a whole. For example, do I read the file in the main and have this method read that in and output to the Product class? Do I read the file in this method? I can't work out how to make this work when I have to use this method.

View Replies View Related

How To Read Data From Text File And Place Into Array

Nov 15, 2014

I'm trying to figure out the necessary code for reading integers from a text file, then placing them withing the two-dimensional array I have created, and then finally printing the array using a nested for-loop. I have attached the file I would like to read from. I'm having trouble figuring out how to reference the text file with the integers, and also how to properly create a dynamic preface for each row of the array. I want the preface to display as "Week #: ", with # being the row number. I tried to create an integer to be used that way, but NetBeans tells me whatever integer I declare has already been defined in method main. I know I need to use scanner to read the file. I would like the output to display like this:

Quote
Temperature Data

Week 1: 73 71 68 69 75 77 78
Week 2: 76 73 72 72 75 79 76
Week 3: 79 82 84 84 81 78 78
Week 4: 75 72 68 69 65 63 65

View Replies View Related

Get Data From Config File And Store In Array Without Using Split Method

May 8, 2014

How i can get the data from a file, for a key that have multiple values without using split method and i want to store that data in an array

file name:"new.txt"[contains below data]
Name=google.com, yahoo.com,facebook.com
Attachment=abc1,abc2,abc3

I am successfully able to do this using following code

Properties props = new Properties();
String configFilePath = (Cjava_confignew.txt);
props.load(new FileReader(configFilePath));
String TestName = props.getProperty(Name);
String test[] = TestName.split(,);

Now i just want to store the values for key "Name" in array without using TestName.split(,); but i am not able to do this.

I have also used .yml file to avoid this but did not succeed, some way to do this

file name test.yml

Name
-google.com
-yahoo.com
-facebook.com

Attachment
-abc1
-abc2
-abc3

i want to store multiple values for single key(Name) within the array

View Replies View Related

Read Data From File And Place Larger Of Two Numbers In Corresponding Position Of Third Array

Apr 14, 2015

I am learning about arrays in my class and my professor has a habit of throwing in code without explaining. We are doing a program called storing largest numbers where we read data from a file and place the larger of the two numbers in the corresponding position of a third array. They are in 4 by 4 format. Here is the ending code

import java.io.*;
import java.util.*;
public class prog464aStoringLargestNums
{
public static void main(String[] args)

[code]....

View Replies View Related

XML :: Setting Stylesheet Ref When Outputting File Using SAXTransformerFactory

Sep 27, 2012

I am using SAXTransformerFactory to output an XML file but can't figure out how to output the stylesheet setting, for example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ectd:ectd SYSTEM "util/dtd/ich-ectd-3-2.dtd">
<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>

I am using the following code to get the first two lines, I just can't see how to output the third line above.

SAXTransformerFactory mTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = mTransformerFactory.newTransformerHandler();
Transformer mTransformer = handler.getTransformer();

mTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
mTransformer.setOutputProperty(OutputKeys.ENCODING, IBF_UtilXML.XML_ENCODING_UTF8);
mTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "util/dtd/ich-ectd-3-2.dtd");

I believe I found an example using a DOM approach but I would much prefer using SAX.

<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>

View Replies View Related

Swing/AWT/SWT :: Setting File Name For JFileChooser Save Dialog

Aug 9, 2001

I am wondering how to do the following:when a user clicks on a save menu item, a save JFileChooser pops up. I would like to have a default file name set in the FileName box on the chooser, similar to what applications like Microsft word has. Right now, no file name will appear until the user has selected a file.I also need to know how to get the text from this box in the case that the user enters a new file name (the getSelection function is not useful here because the user will not have selected an existing file.)

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved