Write And Read Via ArrayList With Text File?
Feb 6, 2015
I wanna learn to create a method where I use a scanner or any other way to input.The stuffs I input will be strings. The whole thing will be like a destination creator.So if I input the string USA, it will write this to a file:
1:USA
When I go to the method again the ID will be added so if I input UK, the file will contain:
1:USA
2:UK
Now if I terminate the program, next time when I run it again it will be able to read the ArrayList, so if I wanna go to the method again and write: Norway the file will contain:
1:USA
2:UK
3:Norway
Later I also want to be able to use println to show all of these lines from the file, as well I would like to be able to delete an ID, so if I delete ID 2 the file would contain:
1:USA
3:Norway
I'm quite new to Java, I'm in a Java course since soon 3 weeks back and I have sure learned a lot, all the loops and basic statements that exist in most languages, but now when it comes to this I just don't understand.
View Replies
ADVERTISEMENT
Dec 12, 2014
I am checking how to do following task.
01. pickup the selected text file and read the line by line and output the text in to visual text pane.
what i did:.
01. I wrote code that read the text file and output in to jave console/ also some of the interface.
the code read txt file:
Java Code:
String fileName = "C:/Users/lakshan/Desktop/lawyer.txt";
File textFile = new File(fileName);
Scanner in = new Scanner (textFile);
while(in.hasNextLine()){
[code]....
so it will read any text file dynamically and output to the text pane in interface. I think scanner code must be execute after the select the file from the browser and set the scanned result in to variable. then later out put the var as string in some jswing component?
View Replies
View Related
Jun 4, 2014
I want to read and write json to/from a file. I read the following document:
JsonReader (Java(TM) EE 7 Specification APIs)
I try to define a JsonReader object in Eclipse:
JsonReader jr
But there appears to be no import for it. Where I am supposed to get the Json library?
View Replies
View Related
Oct 17, 2014
I'm rather new to programming and I'm trying to make a gui program that will allow the user to create text files. They will be prompted to enter what they'd like to save their file as and then they will be allowed to input text to that file . They will also be allowed to open their files to modify or view its contents. So to start things off, I'm making a prototype if you will.. I've been able to save to a file the problem occurs when I try to read/display it.
Main Class:
public class MainActivity {
public static void main(String[] args) {
// Access to other classes
WriteFile writer = new WriteFile();
ReadFile reader = new ReadFile();
[Code] ....
View Replies
View Related
Jul 25, 2013
My background is mainframe and i'm new to java. We're moving from mainframe to the java world and I'm trying to achieve a task. I have a main folder and then bunch of sub folders and each sub folder has bunch of xml files, files size are varies some of them are 900kb. I need to read these xml files and send the output to the txt file (comma separated).
Some possible options or sample example... Please find below the sample xml file. I need to extract information's only where
FORM name="F00001".
[CODE]<PACKET>
<FORM name="F00001" sequence="0">
<FIELD sequence="1">02</FIELD>
<FIELD sequence="2">00000</FIELD>
<FIELD sequence="3">CAB100</FIELD>
<FIELD sequence="4">TEFAP001</FIELD>
<FIELD sequence="5">EMPTY FIELD</FIELD>
<FIELD sequence="6">009.999.989</FIELD>
[Code] .....
View Replies
View Related
Feb 2, 2014
I am trying to read a text file into Java and split the text into tokens. Eventually I want to be able to count the number of instances of a specific word. However, at this point, when I run the file, all I get is the location of the file rather than the text in the file.
import java.util.*;
import java.io.*;
public class textTest3 {
/**
* Prints the number of words in a given file
*
* @param args
* @throws IOException
*/
[Code]...
View Replies
View Related
Apr 21, 2014
I got a question in my last interview, its all about multithreading, interviewer asked me to write a program, to write the contents of three files(F1,F2,F3) in to a new File F4, using multithreading , first thread should read the first file and second thread should read second file, so the File F4 should contain F1's contents in first then F2's contents after that etc. I tried to give my best shot, but i couldn't get a way to ensure that the first thread is reading the first file and write to F4 then second thread reading the second file and writing once first file is written completely into F4 and so on ..how to do this?
View Replies
View Related
Jan 10, 2014
What i basically do is that i read the information of what course that needs to be updated with a new course date and then i log into the wordpress page and navigate to the course and add the date.
I need the java program to be able to do these following actions.
1. Open a webpage
2. write to a form
3. click a button
4. search for a text on screen
5. click on a specific place on the screen/click on a link
6. read from an excel file.
and so on...
View Replies
View Related
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;
[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 quantity after the second comma.....
View Replies
View Related
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
Apr 15, 2014
I have been trying to get this method to work for a few hours now, for some reason I get an IndexOutOfBounds exception every time. Basically I am trying to read a txt file and add the words to an ArrayList as strings separated by a space .
private ArrayList<String> readLinesFromFile(BufferedReader inputFile) throws IOException
{
String value = null;
ArrayList<String> result = new ArrayList<String>();
while((value = inputFile.readLine()) != null){
String[] values = value.split(" ");
for (int i = 0; i < values.length; i++){
result.add(values[i]);
}
}
return result;
}
View Replies
View Related
Mar 7, 2014
I extended the SimpleFileVisitor class. And I am able to traverse the file directories. I give a file path then have it display the files full path and out put that to a text file. Here is what i have so far.
public class Main {
public static void main(String[] args) throws IOException {
Path fileDir = Paths.get("somedirpathhere");
FileTraversal visitor = new FileTraversal();
Files.walkFileTree(fileDir, visitor);
[code]...
I am able to printout of the arraylist to the console. I cant get it to write to a text file whats in the arraylist to a text file.
View Replies
View Related
Dec 8, 2014
I need to write a program where the user inputs name, age, email, and cell and write the user inputs to a text file. I need to have it so the file can be added to if more than one set of information is added. Currently, every time I click the button, the file is overwritten instead of added to. How do I correct this? Current code is as follows:
try
{
FileWriter writer = new FileWriter("ContactInformation.txt");
BufferedWriter bw = new BufferedWriter(writer);
nameTextField.write(bw);
bw.newLine();
ageTextField.write(bw);
bw.newLine();
emailTextField.write(bw);
bw.newLine();
cellTextField.write(bw);
bw.newLine();
bw.close();
nameTextField.setText("");
ageTextField.setText("");
emailTextField.setText("");
cellTextField.setText("");
nameTextField.requestFocus();
}
View Replies
View Related
Mar 3, 2014
personid gender height class
1 female 1.6 short
2 male 2.0 tall
3 male 1.85 medium
4 female 1.9 medium
5 male 1.7 short
6 female 1.8 medium
7 female 1.95 medium
for eg i have to find minimum and maximum height from column height.
View Replies
View Related
Aug 23, 2014
How to read a text file in Java?
View Replies
View Related
Nov 17, 2014
I have to write a program for sorting an array of random numbers by the users choice. These random numbers are stored in a text file, which the user inputs, and is then stored into an array for sorting. I won't have a problem with the sorting algorithms, but I've never had to read a text file and store it into an array before
The text file has the numbers stored like so :
148
626
817
4
312
652
643
etc....
I gather that I'll probably have to user the Scanner for the user to input the text file name, but how do I store it to an array? So far I only have the bones of the program done, like so
import java.util.Scanner;
public class SortingAlgorithms {
public static void main(String[] args) {
[Code].....
View Replies
View Related
Nov 17, 2014
So I am saving a file. it is an array of two strings each containing three words. i figured out how to save to a text file and read it back in and put it back into an array. I am using scanner.hasnext and scanner.next and i think that separates the strings into variables using the spaces in the strings. Well for my project i need to do it with a symbol instead of a space
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;
[Code] .....
I put some comments in the read method so you can get exactly what im trying to accomplish....
View Replies
View Related
Oct 25, 2014
I am trying to read in lines of text from a file then prints out the text. I only need one 2D array and can't copy from one array to another.This how the file looks like:
4 4
FILE
WITH
SOME
INFO
I have been able to read the file but when I am trying to run my program I have blank spacing instead of an array.
import java.util.Scanner;
import java.io.*;
public class Array {
public static void main(String[] args) throws IOException{
[code]....
View Replies
View Related
Oct 22, 2014
Write a class TextAnalysis14 which reads a text file and allows some text mining(Analyzing). Concretely the program has to support
1. total the number of words,
2. checking whether a word is contained in the text,
3. finding the most frequent word, or words in case there are more than one.
A word is a non-empty string consisting of only of letters (a,. . . ,z,A,. . . ,Z), surrounded by blanks, punctuation, hyphenation, line start, or line end. The analysis is case-sensitive.
View Replies
View Related
Jul 2, 2014
I am writing to a file coordinates of texts using PathIterator (Java Platform SE 7 ) saved as SVG format i defined the Font attributes using AttributedString (Java Platform SE 7 ) and TextLayout (Java Platform SE 8 ). It is saving to the file and working properly, but it is just writing as a single line of text where is my target is multiple lines of text, i began using LineBreakMeasurer (Java Platform SE 7 ) Class, but some text is removed plus i got single line of text only, where i face the problem that , How to determine the width to get multiple lines of text ? i tried to figure out from this exampleDrawing Multiple Lines of Text (The Java - Tutorials > 2D Graphics > Working with Text APIs) where is drawing multiple lines of text but i am using getBounds().getWidth() of TextLayout object the code i tried
View Replies
View Related
Jul 27, 2014
I am trying to write to a text file with information stored like this.
brick 500
stone 500
And so on. Here is the code I am using to write to the file.
switch(record){
case "brick":
x.format("%s%n", "brick " + value);
closeFile();
break;
case "log":
x.format("
%s%n", "log " + value);
closeFile();
break;
When I write like this, putting deletes the first line. What I can look into or other functions that would be useful?
View Replies
View Related
Mar 30, 2015
i am trying to write a program in which the user will select a text file(which contains information that the graphical output will be based). I have successfully set up my file chooser however when i select the text file,i do not get any graphical out put. please see attached .zip file for code.
View Replies
View Related
Feb 15, 2015
The initial output of the texfile is this. NO ARRAYLIST OR COMPARATOR IS ALLOWED:
Steve Jobs 9 f 91
Bill Gates 6 m 90
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Steve Jobs 9 m 95
Dennis Ritchie 5 f 100
Jeff Dean 7 m 100
Bill Gates 6 f 96
Jeff Dean 7 f 100
Sergey Brin 27 f 97
Sergey Brin 22 m 98
The collateExams method collates/sorts exam objects starting with the first 'm' (midterm) of the first object and immediately followed by the same person's 'f'(final). Only a SINGLE loop construct is allowed. The output from collateExams() should be the one below but my code is not working, i.e. collateExams method is not working. The output from collateExams() should be
Bill Gates 6 m 90
Bill Gates 6 f 96
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Dennis Ritchie 5 f 100
Steve Jobs 9 m 95
Steve Jobs 9 f 91
Jeff Dean 7 m 100
Jeff Dean 7 f 100
Sergey Brin 22 m 98
Sergey Brin 27 f 97
I am getting [b]NullExceptions[/b] at
r[2*position[exams[i].getID()]+1] =
new Exam(r[i].getFirstName(), r[i].getLastName(),
r[i].getID(), r[i].getExamType(), r[i].getScore());
import java.io.*;
import java.util.*;
[Code] ....
View Replies
View Related
Mar 9, 2014
Assignment: Develop a GUI-based program to read the entries of a matrix from a text file. The first number is the number of rows; the second number is the number of columns. The remaining numbers are integers between 1 and 9 in row by row order. Scan the matrix, highlight (display the entries in different color) all cells that form a group of five cells with the same value horizontally, vertically or diagonally.
Example of text(.txt) file:
7 7
3623161
3666669
3936293
3594955
3289867
2493998
1399998
Example of output:
3623161
3666669
3936293
3594955
3289867
2493998
1399998
* ^^this would be what it would look like when displayed in the GUI. But it would be colored. Anything that has 5 in a row ...My Class File:
// GUI-related imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
// File-related imports
[code]....
EX.
0=black(when there is no combo. of 5)1=blue(horizantal combo of 5)2=red(vertical combo of 5)3=green(right diagnol combo of 5)4=purple(left diagol combo of 5)
View Replies
View Related
Mar 20, 2014
I have a text file containing numbers and names as shown in the example below:
61133128805241Albert Rosenberg
64763645543509Joel
79256337754219Michael Burton
I'm making a program that will read the file and put the numbers in a list of int arrays and names in another list of strings. In my program i created two classes. One will receive the numbers and the other will receive the names. But i only can read the numbers! How can I read everything and separate into two different lists?
try( BufferedReader br = new BufferedReader( new FileReader( jFileChooser1.getSelectedFile().getAbsolutePath() ) ) ) {
hist = new Historic();//Will recieve the array of numbers.
while( br.ready() ) {
int line[] = new int[ 7 ];
[Code] ...
View Replies
View Related
Mar 18, 2014
I have problem with writing objects to file. The thing i want to do is write object(Measur) and then read it from Notepad++. Not from program, but normally from computer. When i'm opening my file i see patterns(i dont how to call this). I think, that I need write it as a string in some way. There is my code:
Java Code:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Task3 {
[code]....
View Replies
View Related