• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use integers from CSV file to initialize corresponding integer values  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a text based game to run out of the Command Prompt that simulates Decking in Shadowrun (3e). Ideally, the player should be able to have a CSV file (65 rows, 2 columns, first column being a string description, second column being the integer I ultimately want input into an int variable) with all the character's pertinent information, and that information should be able to be uploaded into the program.  I am at the point where the csv file is properly loaded and displays everything correctly, while still in the loop. I cannot for the life of me figure out how to get the values out of the loop and into something that I can use to set the values of the various character stats. Snippets of my code are as follows:



The two values at the bottom, DeckerDetectionFactor and DeckerHackingPool, need to be 6 and 9, respectively (this info being drawn from the CSV file). I am looking for a little direction on how to ensure these values are properly loaded into the Character[][] array so the variables are initialized properly instead of being put at 0.  I have also tried, rather than initializing the values directly from the array, initializing the values first and defining them after the array is processed, but it produces the same results (DeckerDetectionFactor = 0 and DeckerHackingPool = 0).  Placing the CSV reader into its own method also produces the same results with my current code.  Thank you for your time and patience, I only started learning Java at the beginning of this year.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to tell what you're trying to do from the code, but if I understand you, you're try to get the two numbers from the CSV file into an array.  The problem I see is that the value of characterload[0] never gets assigned to Character[][]
 
Andrew Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
characterload[0] is more of just a relic leftover from the CVS file, and doesn't really need to be used at all in my code.  That said, could not initializing that cause a problem for the rest of the code?

What I'm trying to do is load a bunch of character statistics (that are all integers) from the CVS file, and be able to reference those values so that I can define what those integers should be.  For example, I've started with int DeckerDetectionFactor = 0, and then follow up by uploading the CVS file.  From there, I want to reference the object that corresponds with the 1st row, 2nd column in the CVS, which is a 6, and essentially use that 6 to define the DeckerDetectionFactor.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing in your code is moving the "row" index.  I'm assuming that Character[row][col] is how you're thinking of the array or arrays.  I'd create a row variable and increment it each time you read a line from the CSV file.
 
Andrew Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that [row][col] is indeed how I am thinking of it, but doesn't the Character[i] move the "row" index in the for loop?  The system print out does show the correct values for the System.out.println(Character[i][1]), but if I try printing Character[0][1] after that, my thoughts are that it should say 6, but instead says 0.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Nothing in your code is moving the "row" index.  I'm assuming that Character[row][col] is how you're thinking of the array or arrays.  I'd create a row variable and increment it each time you read a line from the CSV file.

In which case I don't think your inner loop is necessary anymore.
 
Andrew Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You two are awesome!  Removing the inner loop caused the whole thing to start functioning!  Thank you so much, I have been stuck on that for days!
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. ;-)
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using a Scanner to read the file instead of a BufferedReaderWhy are you using arrays of ints rather than something object‑oriented?
Don't write variable names starting with CapitalLetters. Least of all Character which is the name of a class in the java.lang package.

And welcome to the Ranch
 
Andrew Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Buffered Reader came from when I was trying to figure out how to read a CVS file without downloading any other sort of import packages - the first few examples I found used it, so I thought it was standard.

Thanks for the tip about Capital Letters, however!
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this project it would be
because the first part of the line is a description, and then the int.

I sometimes prefer BufferedReader because I don't want to deal with some of Scanner's idiosyncrasies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!