By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

Problem with .txt file

P: 1
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work correctly so that i can allow certain indexes to be removed without completely deleting everything else.

This is what i have so far.

Code is below
It's in the e part of the code and I have //remed the location of the for loop containg the problem. If anyone can help it would be greatly appreciated.
I have been told to 1. Read the contents of your employee file into some array. Let us call it lines[]. You did this earlier in your code using an array of a fixed size and a counter that indicates how many lines of text are actually in the array. Just do the same here.
2. Now, prepare to write out to that same file to overwrite it, much as you've started doing now.
3. Write all of the content of lines[] back out to the file, but skip the one index location that contains the employee you are removing.
But i don't really understand how to write the contents of a filewriter into an array successfully and after reading the BPJ book it doesn't really explain how to do this either only how to call a file from an external source and how to delete it.

Also a folder named Employee_List needs to be made in the C: Drive not inthe program files but just in the first secton and than have some random text inside it named Employee_List also and in it just have maybe 2 or 3 names a worker number and so on to make this work correctly.
____________________________

import java.io.*;
import java.util.*;

public class Dossierbackupnew
{
public static void main(String args[])throws IOException//throws IOException is needed to allow the program to store and edit any text docs. linked to the program.
{


Scanner kbReader2= new Scanner(System.in);

int a=1;//Current Employed workers
int b=2;//Fired workers
int c=3;//calculator
int d=4;//hour calculator
int e=5;//remove workers
int f=6;//Warnings issued to workers.


String yes = "yes";//used for answer checks
String no = "no";//used for answer checks
String Restart;//needed so the program can be restarted.

double Previouscheck;//allows the user to use the previous paycheck feature.

System.out.println("Welcome to the Store Management Application.");
System.out.println("\n****************( )****************");

System.out.println("\nLogin Frame.");
System.out.println("___________");

System.out.println("\nPlease enter your password.");//Password (I need help currently does not work)



for(;;)
{
String masterpass = "programowner";

Scanner kbReader = new Scanner(System.in);
String password = kbReader.next();

if(password.equals(masterpass))//if they enter the right password.
{

System.out.println("That is the correct password.");
System.out.println("\nWelcome Sir.");
break;

}
else if(password != masterpass)//will end the program, due to incorrect text being inputted.
{
System.out.println("That is not the correct password.");
System.out.println("Please enter the correct password");
continue;
}
}



for(;;)//no set parameter because whats in the for loop itself will be whats done.
{
System.out.println("\nOptions Frame.");

System.out.println("You have the option of choosing between 6 files.");

System.out.println("1:Current Employees\n2:Fired Workers\n3:Paycheck calculator\n4:Hour calculator\n5:Remove worker\n6:Incident Reporting.");//names of the files that are being called.

System.out.println("\nPlease note that the Hour calculator must be used to calculate how much each worker will be paid before the paycheck calculator can be used for input of the worker's hours.");

System.out.println("______________________________ ________");//the lines make it easier to read each section.Makes the program much neater.

System.out.println("Please be sure to only select 1, 2, 3, 4, 5, or 6.");

System.out.println("______________________________ ________");

Scanner kbReader = new Scanner(System.in);
int Selected = kbReader.nextInt();

if(Selected ==a)
{
System.out.println("You have selected the Current Employed list.");


System.out.println(new File("C:\\Employee_List\\Employee_List.txt"));
System.out.println("______________________________ ___________");
Scanner Current_Workers = new Scanner(new File("C:\\Employee_List\\Employee_List.txt"));//calls the actual file from the folder in the C: drive.
int maxIndx =-1;
String text[] = new String [100];

while(Current_Workers.hasNext())
{
maxIndx++;
text[maxIndx] = Current_Workers.nextLine();
System.out.println(text[maxIndx]);
}

Current_Workers.close();
System.out.println("Would you like to edit any information in this document?");
System.out.println("\nPlease type yes or no.");
Scanner kbReader3 = new Scanner(System.in);
String answer2 = kbReader.next();

if(answer2.equals(yes))
{


System.out.println("You may now edit text.");//lets the programmer know it went into the if statement.
FileWriter texteditor = new FileWriter("C:\\Employee_List\\Employee_List.txt", true);
PrintWriter output = new PrintWriter(texteditor);
Scanner kbReader4 = new Scanner(System.in);
String newtext = kbReader4.nextLine();
output.println(newtext);//allows the scanner to be saved to the file.
output.close();
texteditor.close();
System.out.println("Make sure to close the program to save the information.");
}

else if(answer2.equals(no))
{
System.out.println("\nUnderstood, have a nice day.");
System.out.println("\f");//screen clear.
}



}

else if(Selected == b)
{
System.out.println("You have selected Fired Workers list.");
System.out.println("\nSearch Completed.");

Scanner Fired_TempGone_Workers = new Scanner(new File("C:\\Employee_List\\Fired_Temp_Leave_Employee _List.txt"));
int maxIndx = -1;
String text[] = new String [100];

while(Fired_TempGone_Workers.hasNext())
{
maxIndx++;
text[maxIndx] = Fired_TempGone_Workers.nextLine();
System.out.println(text[maxIndx]);
}
Fired_TempGone_Workers.close();//closes the file when finished.
System.out.println("Would you like to edit any information?");
System.out.println("Please type yes or no.");
Scanner kbReader4 = new Scanner(System.in);
String answer3 = kbReader.next();

if(answer3.equals(yes))
{
System.out.println("You may now edit the text.");
FileWriter texteditor2 = new FileWriter("C:\\Employee_List\\Fired_Temp_Leave_Em ployee_List.text",true);
PrintWriter output2 = new PrintWriter(texteditor2);
Scanner kbReader5 = new Scanner(System.in);
String newtext2 = kbReader5.nextLine();
output2.println(newtext2);
output2.close();
texteditor2.close();
System.out.println("Make sure to close the program so the information can be saved.");

}

else if(answer3.equals(no))
{
System.out.println("Understood, have a nice day.");//prints if user selects no.
System.out.println("\f");//screen clear.
}

}

else if(Selected == c)
{

System.out.println(new File("C:\\Employee_List\\Employee_List.txt"));//calls the file to help the manager see who is working and how much they make while doing their paycheck.
System.out.println("______________________________ ___________");
Scanner Current_Workers = new Scanner(new File("C:\\Employee_List\\Employee_List.txt"));//calls the actual file from the folder in the C: drive.
int maxIndx =-1;
String text[] = new String [100];

while(Current_Workers.hasNext())
{
maxIndx++;
text[maxIndx] = Current_Workers.nextLine();
System.out.println(text[maxIndx]);
}
Current_Workers.close();

System.out.println("______________________________ ______________");
System.out.println("\nAbove is the list of workers currently in employment.");
System.out.println("\nBelow is a list of the list of their paychecks.");

System.out.println(new File("C:\\Employee_List\\Monthly_Paychecks_Databas e.txt"));
System.out.println("______________________________ _______________________________");
Scanner PaycheckList = new Scanner(new File("C:\\Employee_List\\Monthly_Paychecks_Databas e.txt"));
maxIndx =-1;
String text1[] = new String [100];

while(PaycheckList.hasNext())
{
maxIndx++;
text1[maxIndx] = PaycheckList.nextLine();
System.out.println(text1[maxIndx]);
}
PaycheckList.close();


System.out.println("\nYou have selected the paycheck calculator.");//beginning of doing the math.
System.out.println("\nThis can be used to calculate how much each worker is making on an hourly basis.");
System.out.println("\nIn order to calculate you must enter the name and the amount of hours that the worker has worked.");
System.out.println("\nPlease enter the name.");
Scanner kbName = new Scanner(System.in);
String Name = kbName.nextLine();
System.out.println("\nPlease enter the amount of hours the worker has worked.");
Scanner kbHours = new Scanner(System.in);

int Hours = kbHours.nextInt();
System.out.println("\nNow Please enter how much they make an hour.");
Scanner kbMoney = new Scanner(System.in);

double Money = kbMoney.nextDouble();
double Paycheck = (Hours*Money);//calculates how much the worker would make.
System.out.println("\nThe worker"+" "+Name+" "+"makes"+" "+"$"+Paycheck+" "+"before tax"+".");

System.out.println("Would you like to edit any information in this document?");//allows them to change information about checks.
System.out.println("\nPlease type yes or no.");
Scanner kbReadera = new Scanner(System.in);
String answer3 = kbReadera.next();
if(answer3.equals(yes))
{
System.out.println("You may now edit text.");//lets the programmer know it went into the if statement.
FileWriter texteditor3 = new FileWriter("C:\\Employee_List\\Monthly_Paychecks_D atabase.txt",true);
PrintWriter output3 = new PrintWriter(texteditor3);
Scanner kbReader6 = new Scanner(System.in);
String newtext3 = kbReader6.nextLine();
output3.println(newtext3);//allows the scanner to be saved to the file.
output3.close();
texteditor3.close();
System.out.println("Make sure to close the program to save the information.");

}

else if(answer3.equals(no))
{
System.out.println("Understood, have a nice day.");
System.out.println("\f");
}


}
else if(Selected == d)
{
System.out.println("Welcome to the hour calculator.");
System.out.println("\nHere you can add how long each person has worked during the week.");

System.out.println(new File("C:\\Employee_List\\Employee_List.txt"));
System.out.println("______________________________ ___________");
Scanner Current_Workers = new Scanner(new File("C:\\Employee_List\\Employee_List.txt"));//calls the actual file from the folder in the C: drive.
int maxIndx =-1;
String text[] = new String [100];

while(Current_Workers.hasNext())
{
maxIndx++;
text[maxIndx] = Current_Workers.nextLine();
System.out.println(text[maxIndx]);
}
Current_Workers.close();
System.out.println("\nCurrent workers list above.");
System.out.println("______________________________ _____");
System.out.println("Hours list below.");



System.out.println(new File("C:\\Employee_List\\Hour_List.txt"));
System.out.println("______________________________ ___________");

Scanner HoursList = new Scanner(new File("C:\\Employee_List\\Hour_List.txt"));//calls the actual file from the folder in the C: drive.
int maxIndx3 =-1;
String text3[] = new String [100];

while(HoursList.hasNext())
{
maxIndx3++;
text3[maxIndx3] = HoursList.nextLine();
System.out.println(text3[maxIndx3]);
}
HoursList.close();

System.out.println("Would you like to edit any information in this document?");
System.out.println("\nPlease type yes or no.");
Scanner kbReader7 = new Scanner(System.in);
String answer7 = kbReader.next();
if(answer7.equals(yes))
{
System.out.println("\nYou may now edit text.");//lets the programmer know it went into the if statement.
FileWriter texteditor6 = new FileWriter("C:\\Employee_List\\Hour_List.txt",true );
PrintWriter output6 = new PrintWriter(texteditor6);
Scanner kbReader8 = new Scanner(System.in);
String newtext6 = kbReader8.nextLine();
output6.println(newtext6);//allows the scanner to be saved to the file.
output6.close();
texteditor6.close();
System.out.println("\nMake sure to close the program to save the information.");


}

else if(answer7.equals(no))
{
System.out.println("Understood, have a nice day.");
System.out.println("\f");//screen clear.
}
}

else if(Selected == e)//Section with the for loop that isnt' working right.
{
System.out.println("Here you can remove names from people who are working if they have quit.");
System.out.println("Here is a list of people who are currently working in case you need to know the name of the person.");

System.out.println(new File("C:\\Employee_List\\Employee_List.txt"));
Scanner Current_Workerss = new Scanner(new File("C:\\Employee_List\\Employee_List.txt"));
int maxIndxx =-1;
String textt[] = new String [100];

while(Current_Workerss.hasNext())
{
maxIndxx++;
textt[maxIndxx] = Current_Workerss.nextLine();
System.out.println(textt[maxIndxx]);
}
Current_Workerss.close();
System.out.println("Would you like to remove a name from this list?");
Scanner kbanswer = new Scanner(System.in);
String answer8 = kbanswer.next();
if(answer8.equals(yes))
{
System.out.println("Who would you like to remove?");
Scanner remover = new Scanner(System.in);
String employeeNumber;
System.out.println("Please enter the number of the person you would like to remove.");
employeeNumber = remover.next();

FileWriter employeelist = new FileWriter("C:\\Employee_List\\Employee_List.txt") ;
PrintWriter outputlist = new PrintWriter(employeelist);





String array[] = new String[15];//array for the file.

int maxIndxremove=0;


for(int k = 0;k<array.length;k++)//<<<<<<<<<<<<<<<<<<<<<<<<< the loop.
{
if(array[maxIndxremove] != (employeeNumber))
{
outputlist.println(array[maxIndxremove]);
maxIndxremove++;

}
else if(array[maxIndxremove] == (employeeNumber))
{
outputlist.println("Vacant Spot");
maxIndxremove++;
}
employeelist.close();
outputlist.close();

}


}
}

else if(Selected == f)
{
System.out.println("This is the section where you can issue warnings to workers to keep track of their behavior if an issue occurs.");
System.out.println(new File("C:\\Employee_List\\Incident_Report_List.txt" ));
Scanner Worker_Problems = new Scanner(new File("C:\\Employee_List\\Incident_Report_List.txt" ));
int maxIndx4 = -1;
String text4[] = new String [100];
while(Worker_Problems.hasNext())
{
maxIndx4++;
text4[maxIndx4] = Worker_Problems.nextLine();
System.out.println(text4[maxIndx4]);
}

Worker_Problems.close();

System.out.println("\nWould you like to add any information?");
System.out.println("\nIf so please print yes or no.");

Scanner kbanswer9 = new Scanner(System.in);
String answer9 = kbanswer9.next();

if(answer9.equals(yes))
{
System.out.println("______________________________ ___");
System.out.println("\nYou may now edit text.");
FileWriter texteditor10 = new FileWriter("C:\\Employee_List\\Incident_Report_Lis t.txt",true);
PrintWriter output10 = new PrintWriter(texteditor10);
Scanner kbReader10 = new Scanner(System.in);
String newtext10 = kbReader10.nextLine();
output10.println(newtext10);//allows the scanner to be saved to the file.
output10.close();
texteditor10.close();
System.out.println("\nMake sure to close the program to save the information.");

}

else if(answer9.equals(no))
{
System.out.println("Understood, have a nice day.");
System.out.println("\f");//screen clear.
}


}
else if(Selected > 6)
{
System.out.println("Please enter only 1-6.");

continue;
}

int NumTimesRestarted = 0;//will be able to tell the user how many times they have restarted.


System.out.println("\nWould you like restart the program? Type y for yes or n for no.");//prompts the user to either hit y or n to restart or stop.
NumTimesRestarted +=1;
System.out.println("\nYou have restarted the program "+ NumTimesRestarted+" times.");//will display how many times the user has restarted the program.

System.out.println("______________________________ __");



Restart = kbReader2.next();//allow for the typing of y or n from the keyboard.

if(Restart.equalsIgnoreCase("n"))//if the player types the key for no.
{
System.out.println("\nThank you for using this program.");//Displays only if n is typed.//End of the program.d
System.out.println("\nThe program will now close.");


break;
}

else if(Restart.equalsIgnoreCase("y"))//if the player types the key for yes.
{


}

else
{
System.out.println("\nYou must type y or n.");//if anything other than y or n is typed it will stay in the loop until the correct
//letter is typed.IE.. if "k" is typed. the printline will be printed out instead of the program continueing.
}
}
}
}
Mar 9 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.