472,353 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 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
0 1849

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

Similar topics

7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: ...
15
by: Ken Allen | last post by:
I have been developing a suite of assemblies over the past couple of weeks, and this afternoon somethign started misbehaving. If I do not run the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network...
13
by: Lee Newson | last post by:
Hi, I have just written my first application using VB.NET. The app works fine when i am running it within .NET for debugging purposes, however...
2
by: key9 | last post by:
Hi all on last post I confused on how to organize file of class, ok ,the problem solved : should include class define head on cpp file. but this...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly,...
4
by: Salad | last post by:
I have a situation where some, not all, users get the message "Couldn't find file "F:\AccessApps\AppName.mdw". This file is required for startup"....
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.