11 Replies - 581 Views - Last Post: 27 November 2012 - 02:08 PM Rate Topic: -----

#1 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Writing Data to a .csv file

Posted 25 November 2012 - 03:40 PM

Hi. I have created a game which takes random rolls from 3 separate dice objects and can output them to a screen and save the data in an array. I want to have these output each of the rolls into a .csv file along with a number for each dice roll it is on.
Example
RollNumber,Die1,Die2,Die3
1,3,4,4
2,4,4,2
3,5,6,6
4, 7,8,5

I have two class files Dice.java contains my methods, but for this problem I am just working it all into main and do not want to use a method for it.

On another note, I was able to get it to write some type of weird character to the file when I just used...
dataout.writeInt(die1.getValue());


But I need to write each of 3 values so have errors when I write...
dataout.writeInt(die1.getValue(), die2.getValue(), die3.getValue());


Dice.Java is below
package dicegame;



import java.util.Random;

public class Dice {
    
    // Private variable below
    private int value;
    private int sumOfDice;
    
    
// Method Set Value   
public void rollDice() {  // This method rolls and returns the value of the die
    Random die1= new Random();
    value = die1.nextInt(6)+1;  }

// Method Get Value
public int getValue() 
{
return value; 
}
 
    public void gameLogic(int sumOfDice) // A new method which contains the games logic
{  
    if (sumOfDice < 11){
    System.out.print("Too Low!  Try again!\n");}
else if (sumOfDice > 11){
    System.out.print("Too High!  Try again!\n");}
else if (sumOfDice == 11){
    System.out.print("Winner!!!\n");}
} 

} // end class Dice




DiceGame6.java is below: This contains the coding issues I am having.

package dicegame;

//Importing Scanner for user input & ArrayList to put reference objects into the list 
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.io.*;

public class DiceGame6 {
    

public static void main(String[] args) {
   
   
       
   Scanner input = new Scanner (System.in);
   
   // ArrayList below - Will be used to store values of dice rolls
    ArrayList<Integer> list = new ArrayList<>();
   
   int rogueValue; // Sentinel Value  
   
   do { //  The do while loop will allow the user to continue to roll until the user decides to quit or another sentinel values is achieved

   System.out.println("*******************************");
   System.out.println("Welcome to the Random Dice Game");
   System.out.println("*******************************");
   System.out.println("");
   System.out.println("CHECK OUT THOSE DICE ROLLS.....!");
   
   
   
   Dice die1 = new Dice();
   Dice die2 = new Dice();
   Dice die3 = new Dice(); // New third die
   Dice callLogic = new Dice();
   
   die1.rollDice();     
   die1.rollDice();
   die2.rollDice();
   die3.rollDice();   
        
    
    int value1 = die1.getValue();
    int value2 = die2.getValue();
    int value3 = die3.getValue();  // New die object
    
    int sumOfDice = value1 + value2 + value3; 
    
    
   
    die1.getValue();
    die2.getValue();
    die3.getValue();
    
    //Having issues with below
    try 
    { DataOutputStream dataout = new DataOutputStream(new FileOutputStream("DiceGameResults.csv")); 
               
       dataout.writeInt(die1.getValue(), die2.getValue(), die3.getValue());
               
      //flushes and closes the stream
      dataout.close(); 
      
    } catch ( IOException ioException )
   
   { 
       System.out.println(
       "There was an error with the file." + ioException);
       
       System.exit (1); //terminate the program
   }
   
   
   
    
    // System Output
   System.out.println("\nYour 1st Dice Roll is....");
   System.out.println(value1);
   System.out.println("");
   System.out.println("Your 2nd Dice Roll is....");
   System.out.println(value2);
   System.out.println("");
   System.out.println("Your 3rd Dice Roll is....");
   System.out.println(value3);
   System.out.println("");
    
   System.out.println("YOU SCORED A TOTAL OF...");
   System.out.println(sumOfDice);
   
   callLogic.gameLogic(sumOfDice);  // Calling new method, passing parameter


    
    System.out.println("\n_________________________________________\nPlay Again?");
    System.out.println("(1)Play Again! (2)Print Your Roll History");
  
    int choice = input.nextInt();
    rogueValue = choice;
    
    list.add(sumOfDice); // Puts reference object into ArrayList
   
    
    } while (rogueValue != 2);  // Sentinel value, as long as roll is not 2 the game will roll dice
    for (int i : list) { // For loop to print the contents of the ArrayList
    System.out.println(i);}
    
    
}  // end main
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Writing Data to a .csv file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing Data to a .csv file

Posted 25 November 2012 - 04:35 PM

This is ridiculous ....
writting to a .csv file requires a characters oriented output stream
you are using method that do not even exist... DataOutputStream.writeInt() method expects only one parameter... and you also have to write the "," if it is for a .csv file

So, your DataOutputStream approach is really not what you want

This post has been edited by pbl: 25 November 2012 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: Writing Data to a .csv file

Posted 25 November 2012 - 05:11 PM

Use PrintWriter opened on FileWriter
Was This Post Helpful? 0
  • +
  • -

#4 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Writing Data to a .csv file

Posted 25 November 2012 - 07:43 PM

Goose, that was helpful thanks. I tinkered around with the code, and was able to output the correct dice rolls into the .csv file, and made some additional catch changes.

Each time I roll the dice I want the data to show in the .csv file. I googled around and it looked like I just needed to put a "println("New line") for the next dice roll to output to the next line. However this is not working. What am I missing so that the output can look like below?

Example
RollNumber,Die1,Die2,Die3
1,3,4,4
2,4,4,2
3,5,6,6
4, 7,8,5

Right now I have...

Die1,Die2, Die3
3,4,4

Here is the new changes I made to the code regarding this.

try 
    { 
           try (FileWriter fw = new FileWriter ("WriteTest.csv")) {
               PrintWriter pw = new PrintWriter (fw);
               
                {
               pw.print(die1.getValue());
               pw.print(",");
               pw.print(die2.getValue());
               pw.print(",");
               pw.print(die3.getValue());
               pw.print(",");
               pw.println("New Line"); } 
               
               pw.flush(); //Flush the output to the file
               pw.close(); // Close the Print Writer
               fw.close(); // Close the File Writer
           } 
      
    } 
    
    catch ( IOException ioException )
   { 
       System.out.println(
       "There was an error with the file." + ioException);
       System.exit (1); //terminate the program
   } //end IO Catch
    
    catch (SecurityException securityException)
   {
       System.err.println("You do not have credentials to write to this file." + securityException);
       System.exit(1); // terminate the program
   } //end security catch

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing Data to a .csv file

Posted 25 November 2012 - 07:49 PM

just do

pw.println();

that will add a new line
Was This Post Helpful? 0
  • +
  • -

#6 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Writing Data to a .csv file

Posted 25 November 2012 - 08:17 PM

Hm. I tried that along with the quotations I pasted earlier.

When I finish running the program and open the .csv file the data contained within it will only show the output from the first three dice rolls.

When I re-roll the dice it does not show the values of those dice on the preceding lines in the .csv file.

So if roll 1 gives me.. 1,2,3
roll 2 gives me 4,5,6
roll 3 gives me 7,8,9

And I open the .csv file it will only show 1,2,3.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing Data to a .csv file

Posted 25 November 2012 - 08:36 PM

didn't see all your code
but the one you posted in reply #4 shows only the writing of 3 values in the file
Was This Post Helpful? 0
  • +
  • -

#8 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Writing Data to a .csv file

Posted 25 November 2012 - 09:16 PM

Yes, but at the very end of all my code I have a sentinel value which will allow the user to continue rolling the dice or not.

I thought that each time the user decided to roll the dice than it would place the value of those dice rolls into the .csv file and than move down to the next line.

If that's not the case I think I would need to store these values into an array? I am confused on the logic here...
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing Data to a .csv file

Posted 25 November 2012 - 09:25 PM

You can claim whatever you want
I based my deductions based on the code I've seen
I won't comment or speculate on what you claim you did
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: Writing Data to a .csv file

Posted 26 November 2012 - 02:52 AM

You're rewriting (overwriting) the file each time. One way of preventing that is to open it in append mode, in which case your try-with-resourcces can start:

  try (PrintWriter pw = new PrintWriter(new FileWriter ("WriteTest.csv", true)))


Of course, i'm assuming you're calling your code repeatedly, though why you can't execute a loop after opening the file instead of just writing one set of throws, i'm not sure. Of course, opening the file n times for n lines is inefficient.

You don't need to call flush() btw, if you're just going to write and close the file.

This post has been edited by g00se: 26 November 2012 - 02:55 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#11 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Writing Data to a .csv file

Posted 27 November 2012 - 01:50 PM

Goose thank you very much for your assistance! I have the code working and what you said made a lot of sense in helping me to debug the code and get the 'logic' working.

Thanks
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2559
  • View blog
  • Posts: 10,683
  • Joined: 20-September 08

Re: Writing Data to a .csv file

Posted 27 November 2012 - 02:08 PM

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1