Need help with file Output
Code:
import java.io.*;
import javax.swing.JOptionPane;
public class SortingSearching
{
public static void main (String[]args)throws IOException
{
String[]fileNames=new String [2];
String[]inputData=new String [20];
String[]sortedList;
fileNames=getFileNames(fileNames);
System.out.println(fileNames[0]+fileNames[1]);
inputData=readFile(fileNames,inputData);
sortedList=sortFile(inputData);
printToFile(sortedList,fileNames);
}
/************************************************************************
*Function getFileNames ||this function gets user inputed filenames and *
*stores them in a string array for later use. *
*Pre:fileNames array Post:returns the array with the filenames *
*************************************************************************/
public static String[] getFileNames(String[]fileNames)
{
fileNames[0]=JOptionPane.showInputDialog("Please enter name of input File:");
fileNames[1]=JOptionPane.showInputDialog("Please enter name of output File:");
return fileNames;
}
/***************************************************************************
*Function readFile ||This function reads the text in the user chosen *
*file and stores up to 20 in a string array. *
*Pre:fileNames, inputData arrays Post:returns up to 20 strings in inputData*
****************************************************************************/
public static String[] readFile(String[]fileNames,
String[]inputData)throws IOException
{
int i=0;
boolean test=false;
String holder;
BufferedReader inFile =
new BufferedReader( new FileReader(fileNames[0]+".txt"));
while(test!=true)
{
holder=inFile.readLine();
if (holder==null)
{
test=true;
}else if(i==20)
{
test=true;
}else
{
inputData[i]=holder;
}
i++;
}
return inputData;
}
/************************************************************************
*Function sortFile ||this function sorts the data aquired by readFile *
*and uses an improved bubble sort to sort them *
*Pre:inputData Array Post:returns a sorted array with the filenames *
*************************************************************************/
public static String[] sortFile(String[] inputData)
{
int lastPos;
int index;
String temp;
for(lastPos=inputData.length-1; lastPos>=0; lastPos--)
{
for(index=0;index<=lastPos-1;index++)
{
if(inputData[index].compareTo(inputData[index+1])>0)
{
temp=inputData[index];
inputData[index]=inputData[index+1];
inputData[index+1]=temp;
}
}
}
return inputData;
}
/************************************************************************
*Function printToFile ||this function prints the sorted array to a file *
* *
*Pre:SortedList array Post:none *
*************************************************************************/
public static void printToFile(String[] sortedList,
String[] fileNames)throws IOException
{
PrintWriter outFile=
new PrintWriter(fileNames[1]+".txt");
int index;
for(index=0;index<sortedList.length;index++)
{
outFile.println(index+""+sortedList[index]);//prints the sorted array to a file
}
}
}
So I am having trouble with my last function, it should write to a file. Here is what I checked.
Is the array i sent the writer empty? No I ran a for loop to check it.
Is the fileNames String not correct? no it creates the file, it just doesn't write.
I am at my wits end, no compiler errors, just run time.
Re: Need help with file Output
I am also having another problem, array.length returns the total size of an array, even null values, is there a function that returns the number of elements in an array instead of the total size. so no nulls.
edit:nevermind I figure out a way to avoid nullPointer, no longer need help with this just the output
Re: Need help with file Output
public static void printToFile(String[] sortedList,
String[] fileNames)throws IOException
{
PrintWriter outFile=
new PrintWriter(fileNames[1]+".txt");
int index;
for(index=0;index<sortedList.length;index++)
{
outFile.println(index+""+sortedList[index]);//prints the sorted array to a file
}
} this is the output statement, I know I have been told to highlight the particular code before so I though I'd do it here
Re: Need help with file Output
Print out the text that is supposed to be printed to your file; somehere before line #108:
Code:
System.out.println(index+""+sortedList[index]);//prints the sorted array to stdout
kind regards,
Jos
Re: Need help with file Output
it prints what it should, does the printwriter autostop if a value is null.
0address
1allocation
2argument
3array
4class
5effective
6function
7immutable
8primitive
9program
10reference
11runtime
12size
13string
14style
15variable
16null
17null
18null
19null
Re: Need help with file Output
Although it doesn't print with a full array either.
0argument
1birds
2corn
3element
4fall
5flowers
6football
7function
8golf
9leaves
10orange
11pilgrim
12plants
13pumpkins
14runtime
15showers
16size
17spring
18sunshine
19variable
just to console
Re: Need help with file Output
Did you read the API docs? I see no calls to flush(), close() or checkError().
Re: Need help with file Output
damn it I forgot the outfile.close(); and inFile.close();
Thanks PhHein I just didn't notice.
Re: Need help with file Output