Results 1 to 6 of 6
  1. #1
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Help with reading a list and converting it into an array.

    I have a list of values in a text file, I would like to take that list and make it into an array writing it into a text file. It tells me that I have a bounds exception error
    and tried everything that I learned from the previous semester. We got up to string operations chapter and don't know any other techniques, from the Starting Out with Java by Gaddis, but Im open to anything just to make this work.


    Here is my code:

    Java Code:
    import java.util.Scanner;			// Needed to import the Scanner class.
    import java.io.*;					// Needed to import the PrintWriter and the IOException class.
    
    class ConvertToArray
    {
    	public static void main(String[] args) throws IOException
    	{
    		// List of variables in the ConverToArray main method.
    		String input;				// Declaring a String for a variable named input.
    		String output;			// Declaring a String for a variable named output.
    		int row = 0;				// Declaring an integer for a variable namd row.
    		double data;			// Declaring a double for a variable named data, intialized at zero.
    		double[] dataArray;		// Declaring an array of doubles for a variable named data.
    		
    		
    		// Creating the Scanner object to read input.
    		Scanner keyboard = new Scanner(System.in);
    		
    		// Gets the user to enter the name of the existing data file.
    		System.out.print("Enter the name of the data file: ");
    		input = keyboard.nextLine();
    		
    		// Opens and reads the file from the disk.
    		File file = new File(input);
    		Scanner inputFile = new Scanner(file);
    		
    		// Gets the user to enter the name of the created data file.
    		System.out.print("Enter the name of new file: ");
    		output = keyboard.nextLine();
    		
    		// Creates and opens the file.
    		PrintWriter outputFile = new PrintWriter(output);
    		
    		while(inputFile.hasNext())
    		{
    			// Redirects the number of data in the file and creates the length of the array.
    			dataArray = new double[row];
    			
    			// Redirects a new variable from input from a disk.
    			data = inputFile.nextDouble();
    			
    			// Adds the data to the current row.
    			dataArray[row] = data;
    			
    			// Redirects it into a new text file with suffix 'Array.txt'
    			outputFile.println(dataArray[row]);
    		}
    		
    		// Closes all open files.
    		inputFile.close();
    		outputFile.close();
    	}
    	// End of the main method.
    }
    Thank you guys, I really really appreciate that.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help with reading a list and converting it into an array.

    You've created a zero length array in your program. That's why you get an ArrayIndexOutOfBoundsException error.

  3. #3
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Re: Help with reading a list and converting it into an array.

    Any suggestions on to to fix this? I'm trying to copy each element in the list and making it into an array.

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

    Default Re: Help with reading a list and converting it into an array.

    Don't use an array; use a List<Double> instead; arrays are kind of primitive, i.e. you can't resize them and you don't know the number of items in your file so you don't know how big your array should be. A List<Double> can resize itself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Re: Help with reading a list and converting it into an array.

    Thanks Jos appreciate the help

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Help with reading a list and converting it into an array.

    Why use an array/List at all? If the objective is to read values in from one file and write them out to a second do it immediately without storing each value in a collection. On the other hand if a future objective will be to use these values for something else then yes you will need a list.

Similar Threads

  1. converting linked list to sortedset
    By jumpydady in forum New To Java
    Replies: 5
    Last Post: 04-09-2012, 02:15 AM
  2. problem reading converting file to hash map
    By cataschok in forum New To Java
    Replies: 2
    Last Post: 02-27-2012, 05:42 PM
  3. Replies: 1
    Last Post: 12-03-2009, 07:03 AM
  4. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 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
  •