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;

}

}

}

[8551 byte] By [djsperlinga] at [2007-11-26 18:11:49]
# 1

} else if (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));

BufferedWriter br = new BufferedWriter(

new FileWriter("nameitwhatyoulike.txt"));

//inputFile = new BufferedReader(fReader); // Don't do this

//str = fReader.readLine(); // inputFile.readLine();

while ( (str = fReader.readLine()) != null && i < names.length){

names[i] = str;

i++;

str = inputFile.readLine();

br.write(str);

br.newLine();

}

br.close();

inputFile.close();

System.out.println("Please enter a Name to search for: ");

searchName = keyboard.readLine();

//enter binary search code

first = 0;

last = 200;

abillconsla at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 2
djsperling, I would be concerned that anything I might say would be construed as "jumping down your throat", so please step aside.Next!
warnerjaa at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 3
Did you read jennyFromTheBronx's reply in your last thread? You definitely didn't implement her advice yet.
doremifasollatidoa at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 4

warnerja,

Go back to your dungeons and dragons game, or world of warcraft. What ever you do with your free time. I did ask for you help.Grow up and stop acting like a **** child. Some of us are attemtping to be professional, while you are boosting your ego from giving beginner programers ****.

djsperlinga at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 5
Go back and re-read my answer to your question in the other post, eejit. Then tell me how I jumped down your throat with that answer, which caused you to get your undies all wadded up.Actually, no, just shove it.
warnerjaa at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 6

> warnerja,

>

> Go back to your dungeons and dragons game, or world

> of warcraft. What ever you do with your free time.

> I did ask for you help.Grow up and stop acting

> like a **** child. Some of us are attemtping to be

> professional, while you are boosting your ego from

> giving beginner programers ****.

Well, I hope you don't believe that telling someone to "go back to your dungeons and dragons" is professional.I would suggest you read over this before you post anymore:

http://catb.org/esr/faqs/smart-questions.html#answers

zadoka at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 7
I'm not going to spend my day arguing with people i've never met. There is no point in arguing anyway. All I wanted was some help. Things didn't go 100% smoothly. Big deal. Lets move on, as I'm sure we all have important work to do.best to all
djsperlinga at 2007-7-9 5:44:30 > top of Java-index,Java Essentials,Java Programming...
# 8
> I'm sure we all have important work to do.Indeed. There is a brace of Orcs coming down the corridor towards me now.
DrLaszloJamfa at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 9

> > I'm sure we all have important work to do.

>

> Indeed. There is a brace of Orcs coming down the

> corridor towards me now.

I just took my walk and ate my sandwitch and now it's nap time - I always get the mid-day droops (I blame that on my lame mid-days posts ;o) ... then it's back to work - no time for arguments boys, this is business!

BTW - did you take a minute or two to look over the coe I posted OP ... I did not know that warnerja already answered your question, so I spent some time tying to help. ;O)

abillconsla at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 10
> Indeed. There is a brace of Orcs coming down the corridor towards me now.Two measly orcs?Yawmark (<-- ain't had nuffin but maggoty bread for three stinkin' weeks)
yawmarka at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 11

> ... I did not know that warnerja

> already answered your question, so I spent some time

> tying to help. ;O)

I didn't answer his new question posted here, so you haven't wasted your time duplicating anything. At best you've just wasted your time on a thick-skulled whiney brat of an OP who probably won't get it anyway.

I had at least given a hint in his other thread (about a different problem), and he got his undies in a wad about it, I guess because I used bold for emphasis and he read it as yelling at him, or something.

As far as I'm concerned, he can flunk away.

warnerjaa at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 12

> > ... I did not know that warnerja

> > already answered your question, so I spent some

> time

> > tying to help. ;O)

>

> I didn't answer his new question posted here, so you

> haven't wasted your time duplicating anything. At

> best you've just wasted your time on a thick-skulled

> whiney brat of an OP who probably won't get it

> anyway.

>

> I had at least given a hint in his other thread

> (about a different problem), and he got his undies in

> a wad about it, I guess because I used bold

> for emphasis and he read it as yelling at him, or

> something.

>

> As far as I'm concerned, he can flunk away.

I was mostly just joshing around and reminding the OP that there was some code to take a peak at.

abillconsla at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 13
BTW - YOU ARE WELCOME
abillconsla at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 14
Thanks Appreciate it!
djsperlinga at 2007-7-9 5:44:31 > top of Java-index,Java Essentials,Java Programming...
# 15
wow warnerja,Make you feel good to just wish bad things upon people? nice.
djsperlinga at 2007-7-21 17:17:40 > top of Java-index,Java Essentials,Java Programming...