Insert line by line data in txt file with arraylist - java

My program is about contacts.
When a Doctor for an example insert name,surname,telephone, everytime he wants to save the data into txt file then output will be:
somename
somesurname
sometelephone
somename
somesurname
sometelephone
...
Right now i did the output will be only in one line:
somename somesurname sometelephone as you can see at the code:
if(text.equals("Save")) {
try {
ArrayList<String> contactsinformations=new ArrayList<>();
String name=tname.getText();
String surname=tsurname.getText();
String telephone=ttelephone.getText();
contactsinformations.add(0,name+" ");
contactsinformations.add(1,surname+" ");
contactsinformations.add(2,telephone+" ");
FileWriter outFile = new FileWriter("Contacts.txt");
BufferedWriter outStream = new BufferedWriter(outFile);
for(int i=0; i<contactsinformations.size(); i++)
outStream.write(String.valueOf(contactsinformations.get(i)));
outStream.close();
JOptionPane.showMessageDialog(this,"Data saved.");
} catch(IOException e) {
System.out.println("ERROR IN FILE");
}
}
I use for loop to get the size of the ArrayList but trying to figure out how can I insert the informations in different line.
WARNING: UPDATED QUESTION!
Question solved with just a true!
if(text.equals("Save")) {
try
{
ArrayList<String> contactsinformations=new ArrayList<>();
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
FileWriter outFile = new FileWriter("Contacts.txt",true);
BufferedWriter outStream = new BufferedWriter(outFile);
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
JOptionPane.showMessageDialog(this,"Data saved.");
outStream.close();
}

Use :
outStream.write(contactsinformations.get(i) + "\n");
Here \n signifies the new line.

As ArrayList preserves insertion order you don't need to precise the index and you can omit the useless variables name, surname, telephone :
contactsinformations.add(tname.getText());
contactsinformations.add(tsurname.getText());
contactsinformations.add(ttelephone.getText());
As the type of the ArrayList is String you don't need the valueOf method :
for (int i = 0; i < contactsinformations.size(); i++) {
outStream.write(contactsinformations.get(i));
outStream.newLine();
}
The PrintWriter class has a method to do both in one call (combine with for-each loop here) :
PrintWriter pw = new PrintWriter(outFile);
for (String contactsinformation : contactsinformations) {
pw.println(contactsinformation);
}

Convert to CharArray and the write BufferedWriter :
for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "\n";
outStream.write(str.toCharArray(), 0, str.length());
}
or write the String by using offset and length the same way as above:
for(int i=0; i< contactsinformations.size(); i++) {
String str = contactsinformations.get(i) + "\n";
outStream.write(str, 0, str.length());
}

outStream.write(contactsinformations.stream()
.collect(Collectors.joining(System.lineSeparator())));

Related

read multiple times a text file for duplicates in java

I am writing a program that should create a file and then generate a unique id , read the file and if any duplicates are present in it generate a new id and repeat the check.
As of now I am able to create the id and write into the file. But my issue is I' m not able to check if any duplicates are present in the file.
Here is the code which I tried:
public class Main {
public static void main(String a[]) throws IOException {
File file = new File("text.txt");
file.createNewFile();
FileWriter filewriter = new FileWriter(file.getAbsoluteFile(), true);
filewriter.write("\r\n");
BufferedWriter bw = new BufferedWriter(filewriter);
String alphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
// randomInteger = random.nextInt();
sb.append(alphanumeric.charAt(random.nextInt(alphanumeric.length())));
}
System.out.println("\n Random Alphanumeric in Java: " + sb.toString());
// reading data from file
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String string;
while((string = br.readLine()) != null) {
if(string.equals(sb.toString())) {
for (int i = 0; i < 10; i++) {
// randomInteger = random.nextInt();
sb.append(alphanumeric.charAt(random.nextInt(alphanumeric.length())));
}
bw.write((sb.toString()));
System.out.println("new value :" + sb.toString());
}
else{
System.out.println("existing value :" +sb.toString());
}
}
System.out.println(sb.toString());
} catch (FileNotFoundException fnfe) {
System.out.println("File not found!!");
}
}
}
There are several things wrong here
1:
if(string.equals(sb.toString())) {
for (int i = 0; i < 10; i++) {
// randomInteger = random.nextInt();
sb.append(alphanumeric.charAt(random.nextInt(alphanumeric.length())));
}
bw.write((sb.toString()));
System.out.println("new value :" + sb.toString());
}
This adds to the existing id as sb is never reset to empty.
2:
What you are doing now is:
generate id
for each line L do
if L equals id
append to ID
write id
else
some output
end for
What you should be doing:
while not inserted
generate id
for each line L do
if L equals id
found = true
break // exit for each
end for
if found
continue //restart while loop
insert into file // we only get here if it was not found
inserted = true
end while
For better performance first read all the IDs into a list and use it to check for existing ids instead of reading the file every time.

Trying to save a clone of an array in a txt file but returns NULL

I am making an app that keeps username and scores from a game in a txt file. The concept is that when it writes a new username and score to the txt file it should open the .txt file, read it and then make a clone of it adding a new uername and score entry in the txt file.
I am thinking of making this with 2 object arrays. The first is the one that is read in and the new will be the one is writen which will have one more entry.
So if player[i] is readen player[i+1] should be writen with new entry.
I am giving u the code below!
private Player[] myplayer=null;
private Player[] mynewplayer=null;
//open Players.txt
int i;
int n;
String filename="players.txt";
try
{
FileReader fp=new FileReader(filename);
BufferedReader bf=new BufferedReader(fp);
n=Integer.parseInt(bf.readLine());
myplayer=new Player[n];
int x=n+1;
mynewplayer=new Player[x];
for(i=0;i<n;i++)
{
String s=bf.readLine();
String user="",score="";
user=s.substring(0,s.indexOf(","));
s=s.substring(s.indexOf(",")+1);
score=s;
myplayer[i]=new Player(user,Double.parseDouble(score));
for(i=0;i<n;i++)
{
mynewplayer[i]= myplayer[i];
}
mynewplayer[x]=new Player(Username,Double.parseDouble(score));
}
bf.close();
fp.close();
}catch(IOException e)
{
System.out.println("Exception was "+e.getMessage());
}
//----------------------------------WRITE mytxt!-------------
n=myplayer.length;
try
{
filename="players.txt";
FileWriter fp=new FileWriter(filename);
fp.write(""+n+"\n");
for(i=0;i<n+1;i++)
fp.write(""+mynewplayer[i]+"\n");
fp.close();
}catch(IOException e)
{
System.out.println("Exception was "+e.getMessage());
}
//----------------------------------WRITE mytxt!-----------
//Get on Message
String s="";
for(i=0;i<mynewplayer.length;i++)
s=s+mynewplayer[i]+"\n";
JOptionPane.showMessageDialog(null,"Players are \n "+s);
Problem is that when it's written, it returns null for mynewplayer.
I suppose the mynewplayer doesnt really take the entries of the "myplayer" but neither writes the new username.
Compile doesnt show any errors. Just writes NULL to the textfile.
Ask me if u want further info on the code writen!
Thanks in advance!
Here is an edited version of your code, with some improvements and there should be a comment around code that I changed, explaining what I did.
Player[] myPlayer = null; // first word uncapitalized, every
Player[] myNewPlayer = null; // other word begins with a capital
//open Players.txt
int i, n; // combine the variables into 1 line
String filename = "players.txt";
try {
FileReader fp = new FileReader(filename);
BufferedReader bf = new BufferedReader(fp);
n = Integer.parseInt(bf.readLine());
// not needed
//myPlayer = new Player[n];
// NOT NEEDED int x = n + 1;
myNewPlayer = new Player[n + 1];
for (i = 0; i < n; i++) {
String s = bf.readLine();
String user, score; // combine variables, doesnt need to initalize them
String[] items = s.split(","); // Splits the line into array elements on every delimiter -> ,
//user = s.substring(0, s.indexOf(","));
//s = s.substring(s.indexOf(",") + 1);
//score = s;
user = items[0];
score = items[1];
// this line below isnt actually needed
//myPlayer[i] = new Player(user, Double.parseDouble(score));
// Create a new player clone, dont copy the previous one
myNewPlayer[i] = new Player(user, Double.parseDouble(score));
}
// We've read all the variables from the text file, now we create the last one
// Since myNewPlayer is (n+1) size, the range of the array is
// 0 to n
// the last index will be n New Score Variable
myNewPlayer[n] = new Player("Username variable", Double.parseDouble("22"));
bf.close();
fp.close();
} catch (IOException e) {
System.out.println("Exception was " + e.getMessage());
}
//----------------------------------WRITE mytxt!-------------
// This is called a ternary operator
// it is a 1 line if statement
// the format is like so
// booleanLogic ? trueAnswer Execution : falseAnswer Execution;
// if () { true }else { false }
n = myNewPlayer != null ? myNewPlayer.length : 0;
// CHANGED HERE - was using the first array rather than second
// dont need the 1st array
try {
filename = "players.txt";
FileWriter fp = new FileWriter(filename);
// Dont need "" before the items
fp.write(n + "\n");
for (i = 0; i < n; i++) {
fp.write(myNewPlayer[i] + "\n");
}
fp.close();
} catch (IOException e) {
System.out.println("Exception was " + e.getMessage());
}
//----------------------------------WRITE mytxt!-----------
//Get on Message
String s = "";
for (i = 0; i < myNewPlayer.length; i++) {
// s += ""; is like doing s = s + "";
s += myNewPlayer[i] + "\n";
}
JOptionPane.showMessageDialog(null, "Players are \n " + s);
I believe that your problem is this:
for(i=0;i<n;i++)
{
String s=bf.readLine();
String user="",score="";
user=s.substring(0,s.indexOf(","));
s=s.substring(s.indexOf(",")+1);
score=s;
myplayer[i]=new Player(user,Double.parseDouble(score));
for(i=0;i<n;i++)
{
mynewplayer[i]= myplayer[i];
}
mynewplayer[x]=new Player(Username,Double.parseDouble(score));
}
You have nested loops, which is fine, but they use the same counter (the variable i ).
So what is happening is the first line of the file is read, and then added to myplayer[0]. However, instead of just also adding it to mynewplayer[0], you start another loop on i. This loop:
for(i=0;i<n;i++)
{
mynewplayer[i]= myplayer[i];
}
is going to copy the first player into mynewplayer[0]...and then null into every other entry (since myplayer only has the firsdt element filled.
The problem is that after that loop completes, i will equal n, so when you get back to the top of the outer loop, the check $i
Perhaps what you should do is this:
for(i=0;i<n;i++)
{
String s=bf.readLine();
String user="",score="";
user=s.substring(0,s.indexOf(","));
s=s.substring(s.indexOf(",")+1);
score=s;
myplayer[i]=new Player(user,Double.parseDouble(score));
mynewplayer[i]= new Player(user,Double.parseDouble(score));
}
mynewplayer[x]=new Player(<the new username>,Double.parseDouble(<the new score>));

ArrayIndexOutOfBoundsException - when parsing a csv file

I want to transform a csv file. My file looks like that:
I am using the opencsv libary to parse my csv. That is my run method to parse the file:
public void run() throws Exception {
CSVReader reader = new CSVReader(new FileReader(csvFile), ';');
String [] nextLine;
int i = -1;
String fileName = "";
String companyName = "";
String currency = "";
String writerPath;
List<String> returnList = null;
List<String> dateList = null;
while ((nextLine = reader.readNext()) != null && i < 10) {
String[] line = nextLine;
System.out.println(line[0]);
System.out.println(line);
i++;
//fileName of the String
if(!line[0].contains("NULL")) {
fileName = line[0];
}
writerPath = "C:\\Users\\Desktop\\CSVOutput\\" + fileName + ".csv";
//write csv file
CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');
//write Header
String[] entries = "Name;Date;TotalReturn;Currency".split(";");
writer.writeNext(entries);
//create Content
//companyName of the String
if(!line[1].contains("Name")) {
companyName = line[1];
System.out.println(companyName);
}
//currency
if(!line[2].contains("CURRENCY")) {
currency = line[2];
}
//total returns
returnList = new ArrayList<String>();
if(line[0].contains("NULL")) {
for(int j = 3; j <= line.length; j++) {
returnList.add(line[j]); // EXCPETION COMES HERE!
}
}
//"Name;Date;TotalReturn;Currency"
List<String[]> data = new ArrayList<String[]>();
for(int m = 0; m <= line.length; m++) {
data.add(new String[] {companyName, "lolo", "hereComesTheDateLater", currency});
}
writer.writeAll(data);
//close Writer
writer.close();
}
System.out.println("Done");
}
}
I am getting an
java.lang.ArrayIndexOutOfBoundsException: 3039
at com.TransformCSV.main.ParseCSV.run(ParseCSV.java:78)
at com.TransformCSV.main.ParseCSV.main(ParseCSV.java:20)
at this line: returnList.add(line[j]);?
Why? What are possible ways to fix that?
I really appreciate your answer!
You want j < line.length and not <=. If there are 10 elements in an Array then there is not an item at index 10 - you only have 0-9.
Further using loads of variables and assigning them is not the preferred way to parse CSV. Java is an Object Orientated language.
Use an Object to represent each line and bind the line using the opencsv javabean API
You are parsing the file till length of file <= instead you have to use <. It will access the file till line.length - 1
Replace with this
for(int j = 3; j <line.length; j++) {
returnList.add(line[j]);
}

Trying to alter a text file in java

Here is my code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Filter{
Message myMessage;
Scanner input;
Scanner input2;
String sender;
String subject;
String emailMIN;
String line;
String line2;
ArrayList<String> blacklist = new ArrayList<String>();
ArrayList<String> keywords = new ArrayList<String>();
ArrayList<String> subjectWords = new ArrayList<String>();
ArrayList<String> emails = new ArrayList<String>();
//String[] lines;
File SpamMessage;
File inFile;
File inFile2;
File tempFile;
String[] lines;
public Filter(Message m,String blacklistFile, String keywordFile, String Spam)throws IOException{
inFile = new File(blacklistFile);
inFile2 = new File(keywordFile);
input = new Scanner (inFile);
input2 = new Scanner (inFile2);
myMessage =m;
SpamMessage=new File(Spam);
}
public void filter() throws IOException{
PrintWriter output = new PrintWriter(SpamMessage);
while(input.hasNextLine()){
line = input.nextLine();
//System.out.println(line);
if(line!=null)
blacklist.add(line);
}
while(input2.hasNextLine()){
line2 = input2.nextLine();
//System.out.println(line2);
if(line!=null)
keywords.add(line2);
}
emails=myMessage.getEmails();
// System.out.println(emails.size() + emails.get(1));
for(int i = 0; i < emails.size(); i++){
// boolean isSpam = false;
lines = emails.get(i).split("\n");
// System.out.println(lines[5] + lines[7]);
sender = lines[2].substring(lines[2].indexOf('<'), lines[2].indexOf('>'));
//` System.out.println(sender);
emailMIN = lines[6].substring(lines[6].indexOf('<'), lines[6].indexOf('>'));
// System.out.println(emailMIN);
for(int j =0; j<lines.length; j++)
{
if(j==2)
{
for(String blacklist2: blacklist)
{
// System.out.println(blacklist2);
if(lines[j].contains(blacklist2))
{
output.println(emailMIN);
}
// output.close();
}
}
if(j==5 || j>=7)
{
// System.out.println(keywords.size());
for(String keywords2: keywords)
{
// System.out.println(keywords2);
if(lines[j].contains(keywords2))
{
output.println(emailMIN);
}
// output.close();
}
}
//addKeywords();
}
}
output.close();
addKeywords();
}
public void addKeywords() throws IOException
{
tempFile = new File("tempFile.txt");
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
for(int i=0; i<lines.length; i++)
{
if(i==5){
String[] words = lines[i].split(" ");
for(String word: words){
if(word.length()>=6){
subjectWords.add(word +"\n");
//System.out.println(subjectWords);
}
}
keywords.addAll(subjectWords);
pw.println(keywords);
}
}
pw.close();
if (!inFile2.delete()) {
//System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile2)){
//System.out.println("Could not rename file");
}
}
}
I'm trying to update the list of words in the keywords txt file right now it does update it but it puts it in the format [generic, pharmacy, little, inside]
Which is wrong because then if I run my code again it is searching if the file contains [generic, pharmacy, little, inside] and I need it to search for every word not the plus a comma or brace. So basically I want it to copy the words in a list format like this
generic
pharmacy
little
inside
That way it searches for each individual word. I figured out how to do this part. Now, how do I add the senders to a different text file? Also is there a way to modify this so it doesn't add the same keywords twice? Thanks
It is because you are writing an array to the file which causes the toString method of it to be called. Write every single item instead.
Instead of pw.println(keywords); 
Do:
for (String keyword : keywords)
{ 
pw.println(keyword.trim());
}
Or, if every word contains \n already, this should work
for (String keyword : keywords)
{ 
pw.print(keyword);
}
Instead of doing:
pw.println(keywords);
you should instead loop through the array and add each line individually.
for(int i = 0; i < keywords.length; i++) {
pw.println(keywords[i]);
}
That was because you are printing an ArrayList object. In your code, keywords is instance of the List and which would you give you an output of [aa,bb] . More over you would get duplicate words since these list instance are class variables, and printed inside a loop
keywords.addAll(subjectWords);
pw.println(keywords);
Either you can loop around keywords outside the for loop or print the word before adding to list.

Java Reading .csv file and getting java.lang.ArrayIndexOutOfBoundsException: 3 and don't know why

I'm banging my head against a wall about a program I'm trying to complete. I'm sure the answer is simple but I just can't figure out the solution.
When I write to the csv file it works but when reading from it, if there is more than 3 objects in the csv file, I get the ArrayIndex error but three or less and it throws no error.
Below is my code for writing to the file:
void saveDataToFile() {
String op = "";
try {
FileWriter fw = new FileWriter(filename);
for (int i =0 ; i< library.length ; i++)
if(library[i]!=null)
fw.write(library[i].getDetailsCSV().toString()+"\n");
fw.write(op.toString());
fw.close();
}
catch(Exception e) {
System.out.println("ERROR : "+e);
}
System.out.println("saveDataToFile()");
}
Below is the code for reading the file:
void loadDataFromFile() {
try{
File fi = new File(filename);
FileReader fr = new FileReader(fi);
char[] buffer = new char[(int)fi.length()];
fr.read(buffer);
fr.close();
String all = new String(buffer);
String[] ip = all.split("\n");
for (int i=0; i<ip.length; i++){
String[] op = ip[i].split(",");
String author = op[0];
String title = op[1];
int isbn = Integer.parseInt(op[2]);
String s = op[3];
boolean h = Boolean.parseBoolean(op[3]);
for(int j=0; j<op.length; j++){
if(author.equals("Dickens"))
library[i] = new title(author,isbn);
else if(author.equals("Lumas"))
library[i] = new title(author,isbn,s);
else if(author.equals("Orwell"))
library[i] = new title(author,isbn,h);
}
}
}
catch(Exception e) {
System.out.println("ERROR : "+e);
}
System.out.println("loadDataFromFile()");
}
The library[] array is size 10. I've tried a System.out.println(op.length); and System.out.println(ip.length); from the read method and ip.length is 10 and op.length is 3 (regardless of how many objects have actually been saved to the csv file i.e. even if it's full).
I'd really appreciate if anyone can see what I am obviously missing!
I'm guessing this loop is breaking it:
for(int j=0; j<op.length; j++){
if(make.equals("Ford"))
cars[i] = new Ford(model,year);
else if(make.equals("Mazda"))
cars[i] = new Mazda(model,year,colour);
else if(make.equals("Toyota"))
cars[i] = new Toyota(model,year,h);
}
If you have more than 3 lines this loop will fail since you are using i instead of j. Not sure what this loop is trying to do but that part will fail for sure.
Also, if op.length is 3, index 3 won't be there since Java arrays are indexed from 0 not 1.

Categories

Resources