Results 1 to 8 of 8
  1. #1
    Lorelai is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Help with arrays

    I have some java methods that I need to keep in place, written by another person. The program will read in a user given file, which is a series of data associated with a person's name. This data is given in a semi-colon delimited line, like so:

    Professor Oak;4;6;9;0;10;6;10;9;10;9;6;10;9;8;6;9;10;10;6;6; 7;64

    Where each delimited item corresponds to:
    Name, Quarter 1, Quarter 2.....Quarter 9, FY1, FY2....FY12, Finals

    I need to store each piece of data in separate arrays, initialized as:
    Java Code:
    public static void initializeArrays( )
    	{
    		mgrNames = new String[ 100 ];
    		quarterDays = new int[100][NUM_QUARTERS];
    		fiscalYear = new int[100][NUM_YEARS];
    		finals = new int[100];
    	}
    I'm hitting a bit of a logic wall here and can't figure out how to read the data in from the user-chosen file, and then place the proper data in the proper array. I've gotten a simple scanner method started that will read through the file, then another scanner that will read through every portion of a line, but I'm not sure how to store specific parts of that line to specific arrays. The first element of the array will always be Name, the second will always be Quarter 1, etc. My scanning code:
    Java Code:
    public static void readFile( String filename ) throws IOException
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int i=0;
    		String line = keyboard.nextLine(); // get the line as a string
    		Scanner lineScanner = new Scanner(line); // scanner for reading words from the line
    		String[] myString = new String[1000];
    			
    
    	while(lineScanner.hasNext()) { // while there is a word left in the line
    		myString[i] = lineScanner.next();
    		i++;
    
    	}
    Any help would be appreciated! I've just hit a wall here and am not certain where to go from here...


    Thank you,

    Lorelai

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Help with arrays

    Your line parser needs to correlate the position of the data it is getting with what that data is so that is puts the data in the correct array.
    The first piece of data is the Name, the next n data items are the quarter, the next q are the years etc
    Keep track of which data element: 0,1,... you are getting from the line that was read so that you know which array and slot you should put it in.

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

    Default Re: Help with arrays

    After you have read a line of data use the String.split method to split on a semi-colon. Then you can take the name from the 0th element and insert int into the name array. Should be simple.

    For the quarters, remember that a 2D array is simply a 1D array of arrays.
    Java Code:
    int[][] twoDee = new int[5][];
    int[] arr = {1,2,3,4};
    twoDee[0] = arr;
    Therefore you read the 1st to 9th elements and insert into a new 1D array and then place that 1D array into your 2D array. Do the same for years.

  4. #4
    Lorelai is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help with arrays

    Thank you Norm and Junky for the responses! I just want to make sure I totally understand what is going on:

    In plain English, I have my file with tons of rows of data:
    Lee McStein;8;9;10;7;10;4;8;7;7;6;10;7;10;7;8;8;0;0;7; 8;3;66
    Professor Oak;4;6;9;0;10;6;10;9;10;9;6;10;9;8;6;9;10;10;6;6; 7;64
    Fergie Duchess;7;8;6;4;7;8;10;1;2;7;3;7;0;6;6;10;3;10;7;8 ;4;84

    By creating a parse (the string.split), would the data then be stored into a string? Would the data then look like an array?
    ({Lee McStein},{8},[9},{10}.....)?

    Actually, typing this, I realize I am still totally lost. I'm still very new to java and am having some problems with the logic behind these arrays. Is there any chance you could explain your thought process again, but perhaps a little slower for me?

    Thank you for your help!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Help with arrays

    By creating a parse (the string.split), would the data then be stored into a string? Would the data then look like an array?
    You need to read the API doc for the split() method. Then write a very short simple program that uses it to see what it does.
    After using the split method, use the Arrays.toString() method to print out the contents of the array that was created so you can see what split() did.

    When the data is parsed into the array, you need to pick out the different pieces as per the layout and order of the input line that was read and parsed. For example the first item is the name, the next n are quarter, the next q are something else. Each piece needs to be copied to the target arrays where you are saving the data.

    Take a piece of paper and write out the contents of the input line and the arrays the data is to go to. Draw lines on the paper showing where each piece of data in the line that was read is to go in each of the arrays.

  6. #6
    Lorelai is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help with arrays

    I'm much more lost than I initially thought. The skeleton code that I have is as follows:

    Java Code:
    import java.util.*;
    import java.io.*;
    public class SickDayStats
    {
    	static String[ ] mgrNames;
    	static int[ ][ ] quarters;
    	static int[ ][ ] FY;
    	static int[ ] finals;
    	static final int NUM_QUARTERS = 9;
    	static final int NUM_FY = 12;
    	static final int QUARTERS = 1;
    	static final int FYEARS = 2;
    	public static void main(String[] args) throws IOException
    	{
    		initializeArrays( );
    		readFile( args[0] );
    		printDetails( );
    		printAverages( );
    	}
    	public static void printAverages( )
    	{
    	
    
    		for ( int i=1; i<=NUM_QUARTERS; i++ )
    		{
    			System.out.print( "Quarter #" + i + " average: " );
    			System.out.println( getAverage( QUARTERS, i ) );
    		}
    		for ( int i=1; i<=NUM_; i++ )
    		{
    			System.out.print( "FY #" + i + " average: " );
    			System.out.println( getAverage( FYEARS, i ) );
    		}
    	}
    	public static void printDetails( )
    	{
    		System.out.print( "Q1\tQ2\tQ3\tQ4\tQ5\tQ6\tQ7\tQ8\tQ9");
    		for ( int i=1; i<=12; i++ )
    			System.out.print( "\tFY" + i );
    		System.out.println( "\tFINALS\tName" );
    	}
    	public static double getAverage( int type, int whichOne )
    	{
    		if ( type == QUARTERS )
    		{
    
    		}
    		else if ( type == FYEAR )
    		{
    
    		}
    	}
    	public static void initializeArrays( )
    	{
    		mgrNames = new String[ 100 ];
    		quarters = new int[100][NUM_QUARTERS];
    		FY = new int[100][NUM_FY];
    		finals = new int[100];
    	}
    	public static void readFile( String filename ) throws IOException
    	{
    
    		
    }
    }
    I need to read in a file that is given to the program as the first command line argument. I assumed that meant the program would need to ask the user for the filename? Or does this mean that this is created in the run configurations? Given that I need to use this structure, would the .split method still be the best way to go?

    I'm sorry to ask so much, I really am just lost at this point and would love to be pointed in the right direction. Thank you!

  7. #7
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default Re: Help with arrays

    No, it means that when you call your program in the command line, you need to supply the file name/location in the arguments of the call statement.

    Have a look at:
    Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Help with arrays

    You should solve one problem at a time before moving on to the next problem.
    For now just hardcode the name of the file in the program and work on the logic to read the lines from the file and to extract from each line the data it contains.

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Using Arrays Within Arrays
    By im_jake9 in forum New To Java
    Replies: 3
    Last Post: 11-07-2009, 07:11 PM
  5. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 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
  •