Results 1 to 5 of 5
  1. #1
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Getting a very strange output..

    My assignment is to design a code that rolls Dice, and then give a plethora of different information pertaining to "n" dice rolls. I just finished (or at least have mostly finished) my dice class, but now while I am testing it, I am getting some output that I can't figure out how to fix.

    Here's the code for the dice class:

    Java Code:
    import java.util.*;
    
    
    public class Dice{
    	
    	Random random = new Random(); 
    	
    	//Dice1
    		int Dice1;
    	//Dice2
    		int Dice2;
    
    	
    	
    	//Method to return random integer from a-b
    	
    	public int randomInt(int a, int b){
    		
    		int value = (int)(Math.random()*b) + a;
            return value;
    		
    		}
    	
    	//Method to roll both dice and return total
    	
    	public int rollDice(){
    		
    		Dice1 = randomInt(1, 6);
    		Dice2 = randomInt(1, 6);
    		
    		int diceTotal = Dice1 + Dice2;
    		return diceTotal;
    	
    		}
    	
    	
    	//Method to check if both dice are equal
    
    	public int doubles(){
    		if(Dice1 == Dice2){
    			return Dice1;                          
    			}
    		
    		else{
    			return 0;
    			}
    		}
    	
    	
    	
    
    	
    //}
    
    	public static void main(String[] args) {
    		
    		Dice dice1 = new Dice();
    		dice1.randomInt(1, 6);
    		System.out.println(dice1);
    	
    		}
    
    }
    Using all my knowledge of code, I should be getting an output of a number between 1 and 6 for dice1 in main. However, the actual output I am getting is "Dice@65690726
    " Or something similar depending on what it randoms.

    What in the world is this and how can I fix it?

    Also, keep in mind that the main in Dice is just to test that class. My actual main will be in a different class.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Getting a very strange output..

    You're getting the default String returned by the Dice class's toString() method. If you want something else displayed, you'll want to give this class your own toString method and have it return a String that makes more sense.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Getting a very strange output..

    "Dice@65690726" is typical of what you get when you print something which is an instance of a class. So when you print dice1 which is an instance of Dice that's what you'll get.

    (A) If you want to print something descriptive of the Dice instance, add a toString() method to the class. This method should override the toString() declared in the Object class: ie it should take no arguments and should return a String. A good string to return might be made up of the two int variables declared in the class.

    (B) If you want to print what dice1.randomInt(1,6) returns, put that expression, not dice1 in the println().

    -----

    It would be a good idea to follow standard Java coding conventions and have variables begin with a lowercase letter. Also, unless you have reason not to, make things private.

  4. #4
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Getting a very strange output..

    For some reason I didn't realize that I needed to put "dice1.randomInt(1,6) in instead of dice1. Knowing that completes my Dice class, which should be sufficient for the assignment. So for now I'm not going to worry about coding a toString method. (Especially since I can't figure out how at the moment.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Getting a very strange output..

    For some reason I didn't realize that I needed to put "dice1.randomInt(1,6) in instead of dice1.
    I'm glad you've got it sorted out. If you think about it the randomInt() method doesn't make have permanent effect on the Dice instance: it just calculates an int, returns it, and forgets it. So to get that int displayed it's no good printing the dice1 instance which has, at that point, forgotten all about the int that it calculated.

Similar Threads

  1. Replies: 1
    Last Post: 08-26-2011, 07:01 AM
  2. Strange for loop
    By Grimmjow in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 10:52 PM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 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
  •