Results 1 to 9 of 9
  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Need help with file Output

    Java 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.

  2. #2
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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
    Last edited by lenois; 02-07-2013 at 05:33 AM.

  3. #3
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with file Output

    Print out the text that is supposed to be printed to your file; somehere before line #108:

    Java Code:
    System.out.println(index+""+sortedList[index]);//prints the sorted array to stdout
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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

  6. #6
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Need help with file Output

    Did you read the API docs? I see no calls to flush(), close() or checkError().
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Need help with file Output

    damn it I forgot the outfile.close(); and inFile.close();
    Thanks PhHein I just didn't notice.

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Need help with file Output

    :) You're welcome.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2011, 10:21 PM
  2. XML output file
    By Lund01 in forum Advanced Java
    Replies: 8
    Last Post: 06-22-2011, 08:52 PM
  3. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  4. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  5. Replies: 7
    Last Post: 11-19-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •