Problem with .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Filemaxor
    New Member
    • Mar 2008
    • 1

    Problem with .txt file

    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 Dossierbackupne w
    {
    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.prin tln("Welcome to the Store Management Application.");
    System.out.prin tln("\n******** ********( )************** **");

    System.out.prin tln("\nLogin Frame.");
    System.out.prin tln("__________ _");

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



    for(;;)
    {
    String masterpass = "programown er";

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

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

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

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



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

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

    System.out.prin tln("1:Current Employees\n2:Fi red Workers\n3:Payc heck calculator\n4:H our calculator\n5:R emove worker\n6:Incid ent Reporting.");//names of the files that are being called.

    System.out.prin tln("\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.prin tln("__________ _______________ _____________") ;//the lines make it easier to read each section.Makes the program much neater.

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

    System.out.prin tln("__________ _______________ _____________") ;

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

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


    System.out.prin tln(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;
    System.out.prin tln("__________ _______________ _______________ _");
    Scanner Current_Workers = new Scanner(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;//calls the actual file from the folder in the C: drive.
    int maxIndx =-1;
    String text[] = new String [100];

    while(Current_W orkers.hasNext( ))
    {
    maxIndx++;
    text[maxIndx] = Current_Workers .nextLine();
    System.out.prin tln(text[maxIndx]);
    }

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

    if(answer2.equa ls(yes))
    {


    System.out.prin tln("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(tex teditor);
    Scanner kbReader4 = new Scanner(System. in);
    String newtext = kbReader4.nextL ine();
    output.println( newtext);//allows the scanner to be saved to the file.
    output.close();
    texteditor.clos e();
    System.out.prin tln("Make sure to close the program to save the information.");
    }

    else if(answer2.equa ls(no))
    {
    System.out.prin tln("\nUndersto od, have a nice day.");
    System.out.prin tln("\f");//screen clear.
    }



    }

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

    Scanner Fired_TempGone_ Workers = new Scanner(new File("C:\\Emplo yee_List\\Fired _Temp_Leave_Emp loyee_List.txt" ));
    int maxIndx = -1;
    String text[] = new String [100];

    while(Fired_Tem pGone_Workers.h asNext())
    {
    maxIndx++;
    text[maxIndx] = Fired_TempGone_ Workers.nextLin e();
    System.out.prin tln(text[maxIndx]);
    }
    Fired_TempGone_ Workers.close() ;//closes the file when finished.
    System.out.prin tln("Would you like to edit any information?");
    System.out.prin tln("Please type yes or no.");
    Scanner kbReader4 = new Scanner(System. in);
    String answer3 = kbReader.next() ;

    if(answer3.equa ls(yes))
    {
    System.out.prin tln("You may now edit the text.");
    FileWriter texteditor2 = new FileWriter("C:\ \Employee_List\ \Fired_Temp_Lea ve_Employee_Lis t.text",true);
    PrintWriter output2 = new PrintWriter(tex teditor2);
    Scanner kbReader5 = new Scanner(System. in);
    String newtext2 = kbReader5.nextL ine();
    output2.println (newtext2);
    output2.close() ;
    texteditor2.clo se();
    System.out.prin tln("Make sure to close the program so the information can be saved.");

    }

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

    }

    else if(Selected == c)
    {

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

    while(Current_W orkers.hasNext( ))
    {
    maxIndx++;
    text[maxIndx] = Current_Workers .nextLine();
    System.out.prin tln(text[maxIndx]);
    }
    Current_Workers .close();

    System.out.prin tln("__________ _______________ _______________ ____");
    System.out.prin tln("\nAbove is the list of workers currently in employment.");
    System.out.prin tln("\nBelow is a list of the list of their paychecks.");

    System.out.prin tln(new File("C:\\Emplo yee_List\\Month ly_Paychecks_Da tabase.txt"));
    System.out.prin tln("__________ _______________ _______________ _______________ ______");
    Scanner PaycheckList = new Scanner(new File("C:\\Emplo yee_List\\Month ly_Paychecks_Da tabase.txt"));
    maxIndx =-1;
    String text1[] = new String [100];

    while(PaycheckL ist.hasNext())
    {
    maxIndx++;
    text1[maxIndx] = PaycheckList.ne xtLine();
    System.out.prin tln(text1[maxIndx]);
    }
    PaycheckList.cl ose();


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

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

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

    System.out.prin tln("Would you like to edit any information in this document?");//allows them to change information about checks.
    System.out.prin tln("\nPlease type yes or no.");
    Scanner kbReadera = new Scanner(System. in);
    String answer3 = kbReadera.next( );
    if(answer3.equa ls(yes))
    {
    System.out.prin tln("You may now edit text.");//lets the programmer know it went into the if statement.
    FileWriter texteditor3 = new FileWriter("C:\ \Employee_List\ \Monthly_Payche cks_Database.tx t",true);
    PrintWriter output3 = new PrintWriter(tex teditor3);
    Scanner kbReader6 = new Scanner(System. in);
    String newtext3 = kbReader6.nextL ine();
    output3.println (newtext3);//allows the scanner to be saved to the file.
    output3.close() ;
    texteditor3.clo se();
    System.out.prin tln("Make sure to close the program to save the information.");

    }

    else if(answer3.equa ls(no))
    {
    System.out.prin tln("Understood , have a nice day.");
    System.out.prin tln("\f");
    }


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

    System.out.prin tln(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;
    System.out.prin tln("__________ _______________ _______________ _");
    Scanner Current_Workers = new Scanner(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;//calls the actual file from the folder in the C: drive.
    int maxIndx =-1;
    String text[] = new String [100];

    while(Current_W orkers.hasNext( ))
    {
    maxIndx++;
    text[maxIndx] = Current_Workers .nextLine();
    System.out.prin tln(text[maxIndx]);
    }
    Current_Workers .close();
    System.out.prin tln("\nCurrent workers list above.");
    System.out.prin tln("__________ _______________ __________");
    System.out.prin tln("Hours list below.");



    System.out.prin tln(new File("C:\\Emplo yee_List\\Hour_ List.txt"));
    System.out.prin tln("__________ _______________ _______________ _");

    Scanner HoursList = new Scanner(new File("C:\\Emplo yee_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.nextL ine();
    System.out.prin tln(text3[maxIndx3]);
    }
    HoursList.close ();

    System.out.prin tln("Would you like to edit any information in this document?");
    System.out.prin tln("\nPlease type yes or no.");
    Scanner kbReader7 = new Scanner(System. in);
    String answer7 = kbReader.next() ;
    if(answer7.equa ls(yes))
    {
    System.out.prin tln("\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(tex teditor6);
    Scanner kbReader8 = new Scanner(System. in);
    String newtext6 = kbReader8.nextL ine();
    output6.println (newtext6);//allows the scanner to be saved to the file.
    output6.close() ;
    texteditor6.clo se();
    System.out.prin tln("\nMake sure to close the program to save the information.");


    }

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

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

    System.out.prin tln(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;
    Scanner Current_Workers s = new Scanner(new File("C:\\Emplo yee_List\\Emplo yee_List.txt")) ;
    int maxIndxx =-1;
    String textt[] = new String [100];

    while(Current_W orkerss.hasNext ())
    {
    maxIndxx++;
    textt[maxIndxx] = Current_Workers s.nextLine();
    System.out.prin tln(textt[maxIndxx]);
    }
    Current_Workers s.close();
    System.out.prin tln("Would you like to remove a name from this list?");
    Scanner kbanswer = new Scanner(System. in);
    String answer8 = kbanswer.next() ;
    if(answer8.equa ls(yes))
    {
    System.out.prin tln("Who would you like to remove?");
    Scanner remover = new Scanner(System. in);
    String employeeNumber;
    System.out.prin tln("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(emp loyeelist);





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

    int maxIndxremove=0 ;


    for(int k = 0;k<array.lengt h;k++)//<<<<<<<<<<<<<<< <<<<<<<<<< the loop.
    {
    if(array[maxIndxremove] != (employeeNumber ))
    {
    outputlist.prin tln(array[maxIndxremove]);
    maxIndxremove++ ;

    }
    else if(array[maxIndxremove] == (employeeNumber ))
    {
    outputlist.prin tln("Vacant Spot");
    maxIndxremove++ ;
    }
    employeelist.cl ose();
    outputlist.clos e();

    }


    }
    }

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

    Worker_Problems .close();

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

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

    if(answer9.equa ls(yes))
    {
    System.out.prin tln("__________ _______________ ________");
    System.out.prin tln("\nYou may now edit text.");
    FileWriter texteditor10 = new FileWriter("C:\ \Employee_List\ \Incident_Repor t_List.txt",tru e);
    PrintWriter output10 = new PrintWriter(tex teditor10);
    Scanner kbReader10 = new Scanner(System. in);
    String newtext10 = kbReader10.next Line();
    output10.printl n(newtext10);//allows the scanner to be saved to the file.
    output10.close( );
    texteditor10.cl ose();
    System.out.prin tln("\nMake sure to close the program to save the information.");

    }

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


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

    continue;
    }

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


    System.out.prin tln("\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.
    NumTimesRestart ed +=1;
    System.out.prin tln("\nYou have restarted the program "+ NumTimesRestart ed+" times.");//will display how many times the user has restarted the program.

    System.out.prin tln("__________ _______________ _______");



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

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


    break;
    }

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


    }

    else
    {
    System.out.prin tln("\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.
    }
    }
    }
    }
Working...