Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Populating multiple arrays with a File.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Populating multiple arrays with a File.

    I am trying to populate multiple arrays with data from a file. I have created 7 arrays which correspond the files 7 lists.

    Here is an example of the rows:
    GHCND:USW00014764 2011/01/01 0 0 127 122 -17
    GHCND:USW00014764 2011/01/02 5 0 102 67 28
    When I run my program I get the following output:

    null null 0.0 0.0 0.0 0.0 0.0
    null null 0.0 0.0 0.0 0.0 0.0
    Obviously my arrays are not being populated, but I don't know why. I will post my code below. Any help would be awesome!

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
     
    public class WeatherArray {
     
    	public static void main(String[] args)
    			throws FileNotFoundException{
     
    		int counter = 0;
    		int rows = 0;
    		Scanner input = new Scanner(new File("PortlandWeather2011.txt"));
     
    		String head1 = input.nextLine();
    		String head2 = input.nextLine();
    		System.out.println(head1);
    		System.out.println(head2);
     
    		while(input.hasNext()){
    			String counts = input.nextLine();
    			rows++;
    		}
    		// Create Arrays
    		String[] station = new String[rows];
    		String[] date = new String[rows];
    		double[] prcp = new double[rows];
    		double[] snow = new double[rows];
    		double[] snwd = new double[rows];
    		double[] tmax = new double[rows];
    		double[] tmin = new double[rows];
     
    		// Populate Arrays
    		while(input.hasNextLine()){
    			station[counter] = input.next();
    			date[counter] = input.next();
    			prcp[counter] = input.nextDouble();
    			snow[counter] = input.nextDouble();
    			snwd[counter] = input.nextDouble();
    			tmax[counter] = input.nextDouble();
    			tmin[counter] = input.nextDouble();
    			counter++;
    		}
     
    		// Print Arrays
    		for(int i = 0; i <= rows - 1; i++){
    			System.out.printf("%17s %10s %8.1f %8s %8s %8.1f %8.1f \n", station[i], date[i], prcp[i], snow[i], snwd[i],
    			tmax[i], tmin[i]);
    		}
    	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Populating multiple arrays with a File.

    It appears that you're trying to read through the file twice, the first time to get the count and the second time to get the data. If so, then you will need to close the Scanner and then re-initialize it with the File between reads. You're also using the Scanner in an unsafe manner. Myself, if I check input.hasNextLine() then I follow that with input.nextLine(). Each nextXXX() should be paired with a hasNextXXX().

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating multiple arrays with a File.

    [/COLOR]Thank you for your help again! I was able to fix my problem by closing the scanner, and reopening it again like you said. I am also going to take your advice about the .nextLine() and .hasNextLine()

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Populating multiple arrays with a File.

    You're welcome!

Similar Threads

  1. Having trouble splitting a text file into multiple arrays.
    By orbin in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 19th, 2012, 05:27 PM
  2. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 12:40 AM
  3. Populating a 2D array with textfield values
    By drixnak in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 19th, 2010, 01:20 PM
  4. Populating a JTable
    By crism85 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 20th, 2010, 12:57 PM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 02:40 AM

Tags for this Thread