473,691 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with .txt file

1 New Member
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.
}
}
}
}
Mar 9 '08 #1
0 1967

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

Similar topics

7
3775
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 but I have struggled with it for the last two days and would appreciate this group's helpful expertise. My problem may be related to mixing the C and C++ languages together. Namely, I have a struct which I cannot change (as legacy code) similiar to this (I will change the names throughout as...
12
8487
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too. I, too, am getting nasty FileNotFound exceptions. I've read, and digested the article, and I think I've found a bug -- it's difficult to track, though it does happen often.
15
2423
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 IDE and compiler the code from the command line, the compilation seems to work properly almost all of the time. More on this later. If I launch one copy of the IDE, then I can usually compile the project at least once. At some point, the build fails reporting "Could not copy temporary files...
0
3935
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 conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
5
2728
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 ********************** <%@ Page Language="VB" Debug="true" %>
8
9751
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 drive resource (S:\Path\FileName). Here is my problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
13
3935
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 when i try to run the app from the .exe file that .NET creates i get the following error message: "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in VisioTimeline.exe
2
1871
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 time ,still link error: strange is I put the implement to .h file directly like this: *******head file***** class LinuxTestTerminal : public Terminal{ public:
0
2029
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, and in C# Iím using .NET Framework 2.0 (Visual Studio 2005). The problem occurred when I trying to opening and reading file: ============= C++ ============= void LoadSourceFile(char * fileName, unsigned char * data, int length)
4
4053
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". My app the users are attempting to access is written A2003 and they use the A2003 runtime to access the application. They use a desktop icon that specifies the location of Access, the Appname, and the MDW file to use. Ex: "C:\ProgramFiles\Office\MSACCESS.EXE" "C:\Apps\App.MDB" /wrkgrp...
0
8600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8952
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8793
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8796
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5817
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4553
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2234
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.