How do I save a file for later use?
Hello all. I am on the verge of having my program work correctly.
First i ask the user to create a file with a .txt extension. then the user enters names until they enter a sentinel value of "end". After that the string array is sorted by calling selectionSort. Then the program ends.
When I restart the program, I have the user search for the same file name they entered previously.
How do I save the user defined file after I call selectionSort? I want to be able to find it when starting the program again. Here is my code thus far:
package assignment1;
import java.io.*;
import java.util.*;
class assignment1{
publicstaticvoid main(String[] args)throws IOException{
BufferedReader keyboard =null;
String userChoice;
BufferedReader inputFile =null;
String studentData;
String searchFile =null;
String searchName;
String stringIn;
PrintWriter pWriter;
FileWriter fWriter =null;
BufferedReader fReader =null;
String fileName =null;
File file =null;
boolean repeat;
int first;
int last;
int mid;
int midValue;
int i;
int number;
String[] names =new String[200];
int namesLen = 1;
// creates keyboard as a buffered input stream
keyboard =new BufferedReader(new InputStreamReader(System.in));
//prompts user to choose 1 or 2 to make a corresponding choice
System.out.println("Please Enter: ");
System.out.println("1 to Create a File: ");
System.out.println("2 to Search a File: ");
userChoice = keyboard.readLine();//user enters 1 or 2
// converts a String into an int value
number = Integer.parseInt(userChoice);
{
if (number == 1){
System.out.println("Please Enter the File Name to Create with a" +
".txt extension: ");
fileName = keyboard.readLine();
file =new File(fileName);
fWriter =new FileWriter(fileName);
studentData = keyboard.readLine();
// Create a PrintWriter that automatically flushes data
// to the output file whenever the println method is used.
pWriter =new PrintWriter(fWriter);
i=0;
//keep looping till sentinel
while (!studentData.equals("end") && !studentData.equals(null))
{
System.out.println("Enter a name and press Enter. " +
"Type 'end' and press Enter when done: ");
studentData = keyboard.readLine();
names[i] = studentData;
i++;
namesLen++;
//loop for putting the names into the array
pWriter.println(names[i]);
}
pWriter.close();
//call selectionSort() to order the array
selectionSort(names, namesLen);
// Now output to a file.
fWriter =new FileWriter(fileName);
pWriter =new PrintWriter(fWriter);
}elseif (number == 2){
System.out.println("Please Enter a File Name to search " +
"with a .txt extension: ");
searchFile = keyboard.readLine();
if (searchFile == fileName)
{
String str; i = 0;
fReader =new BufferedReader(new FileReader(fileName));
inputFile =new BufferedReader(fReader);
str = inputFile.readLine();
while (str !=null && i < names.length){
names[i] = str;
i++;
str = inputFile.readLine();
}
inputFile.close();
System.out.println("Please enter a Name to search for: ");
searchName = keyboard.readLine();
//enter binary search code
first = 0;
last = 200;
while (first < last)
{
mid = (first + last)/2;// Compute mid point.
if (searchName.compareTo(names[mid]) < 0){
last = mid;// repeat search in bottom half.
}elseif (searchName.compareTo(names[mid]) > 0){
first = mid + 1;// Repeat search in top half.
}else{
// Found it.
System.out.println("The Name IS in the file.");
}
}// did not find it.
System.out.println("The Name IS NOT in the file.");
}
}else//if userChoice != 1 or 2, re-prompt then start over
System.out.println("Please Enter 1 or 2 or correctly " +
"enter an existing file!!");
}
}
publicstaticvoid selectionSort(String[] names,int namesLen){
int smallIndex;
int pass, i = 0;
String temp;
for (pass = 0; pass < namesLen; pass++)
{
//scan the sublist starting at index pass
smallIndex = pass;
//jtraverses sublist names[pass+1] to names[n-1]
for (i = pass+1; i < namesLen-1; i++)
//if smaller string found, smallIndex=that position
if (names[i].compareTo(names[smallIndex]) < 0)
smallIndex = i;
i++;
temp = names[pass];//swap
names[pass] = names[smallIndex];
names[smallIndex] = temp;
}
}
}

