Results 1 to 8 of 8
Thread: Help with arrays
- 11-07-2011, 09:40 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
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:
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 initializeArrays( ) { mgrNames = new String[ 100 ]; quarterDays = new int[100][NUM_QUARTERS]; fiscalYear = new int[100][NUM_YEARS]; finals = new int[100]; }
Any help would be appreciated! I've just hit a wall here and am not certain where to go from here...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++; }
Thank you,
Lorelai
- 11-07-2011, 09:56 PM #2
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.
- 11-07-2011, 10:04 PM #3
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.
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.Java Code:int[][] twoDee = new int[5][]; int[] arr = {1,2,3,4}; twoDee[0] = arr;
- 11-07-2011, 11:32 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
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!
- 11-07-2011, 11:47 PM #5
Re: Help with arrays
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.By creating a parse (the string.split), would the data then be stored into a string? Would the data then look like an array?
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.
- 11-07-2011, 11:51 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Re: Help with arrays
I'm much more lost than I initially thought. The skeleton code that I have is as follows:
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?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'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!
- 11-07-2011, 11:57 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
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)
- 11-08-2011, 01:01 AM #8
Similar Threads
-
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Using Arrays Within Arrays
By im_jake9 in forum New To JavaReplies: 3Last Post: 11-07-2009, 07:11 PM -
A little help with arrays..
By zeppelin in forum New To JavaReplies: 8Last Post: 01-05-2009, 12:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks