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 3 of 3

Thread: A quick fix and some help reading from excel / text file

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default A quick fix and some help reading from excel / text file

    Alright, so some of you may remember me from the other day, when I needed help fixing for loops and using a Scanner. Things have come a long way since then, and my code is largely functional! My next goal is to add code that facilitates character creation from base stats, which means reading from the Google Doc that the stats are in, then writing them to (I think, ideally) a four-dimensional array: one dimension to determine whether stats are bases or growths, one to determine the race of the unit, one to determine their class and then one to actually store the numbers. If there's a drastically superior way to store that data I'm open to changing that, but speed isn't a huge issue since this will only need to be done once per game. My big question right now is how to get that data from the file, and whether it's better to manually input the data into the java code or to read it every time from a text file.
    Last edited by Iantaylora; December 22nd, 2013 at 10:30 AM. Reason: Figured out the problem myself, derp.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A quick fix and some help reading from excel / text file

    Storing data in parallel arrays is not Object Oriented Programming (OOP). It's a pain in the rear to visualize - especially 4D arrays - and keep track of. You'll forever be fighting to correct errors that will be difficult to find. Instead, create objects from the data based on classes that you've written, Base, Growth, etc., with the necessary attributes to give the objects the functionality needed for the program.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: A quick fix and some help reading from excel / text file

    Well, I took some of your advice as far as OOP, and made a unitStats class which knows its own stats, etc, and then I build the unit by giving it its race and two classes. These values provide the necessary offset to reference the correct parts of the spreadsheet I'm using to hold all this data, which is done when the unit is instantiated. My issue now is an odd ArrayOutOfBounds exception that I can't put my finger on. The relevant code for unitData is below and the junk code I'm using to test it is below that.

    	public unitData(int a, int b, int c) throws BiffException, IOException
    	{
    		Workbook workbook = Workbook.getWorkbook(new File("Job Stats FFTA2.xls"));
    		Sheet sheet = workbook.getSheet(0);
    		switch(a)
    		{ //determines the offset from the top of the sheet to draw data from
    			case 1: race = 1;
    			case 2: race = 15;
    			case 3: race = 28;
    			case 4: race = 40;
    			case 5: race = 53;
    			case 6: race = 65;
    		}
    		pClass = b;
    		sClass = c;
    		Cell cell; //the cell taken temporarily from the sheet
    		int prisoner; //the data contained in the cell
     
    		for(int x=0;x<7;x++){
    			cell = sheet.getCell(race+b,x+9);
    			prisoner = Integer.parseInt(cell.getContents());
    			unitStats[x]+=prisoner;} //adds the base stats for the primary class. 
    		for(int x=0;x<7;x++){
    			cell = sheet.getCell(race+b,x);
    			prisoner = Integer.parseInt(cell.getContents());
    			unitStats[x]+=prisoner;} //adds the growths for the primary class
    		for(int x=0;x<7;x++){
    			cell = sheet.getCell(race+c,x);
    			prisoner = Integer.parseInt(cell.getContents());
    			unitStats[x]+=prisoner;} //adds the growths for the secondary class
    		hpMax = unitStats[0]; //sets max HP for later calculations
    		mpMax = unitStats[1]; //sets max HP for later calculations
    	}



    unitData fred = new unitData(1,5,3);
    		System.out.println(fred.getHP());

    An online version of the spreadsheet used is available here: https://docs.google.com/spreadsheet/...Xc&pli=1#gid=0
    Last edited by Iantaylora; December 22nd, 2013 at 12:55 PM. Reason: Added spreadsheet

Similar Threads

  1. Trouble reading a Text File
    By iCurtisIT in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: November 30th, 2013, 07:18 PM
  2. Reading multiple columns from an excel file using apache poi 3
    By ramachandra469 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 15th, 2013, 11:01 AM
  3. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  4. Reading from a Text file & writing to Excel spreadsheets
    By javanooby in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 14th, 2012, 12:41 PM
  5. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM