Berylthranox
Berylthranox

Reputation: 1

I'm new to using objects and classes. How do I print from within a user created class?

I've created a class which is used by program which rolls a dice object 6 times. My problem is in my printStats() method as I'm not sure how to print the stats back to the main class. Specifically I am getting two errors and both say "expect return statement." Here is my class called Dice.

/*

import java.io.*;
import java.util.*;

  class Dice{

Random rnd=new Random();
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;

String roll(){
    int a = rnd.nextInt(5);
    if(a == 0){
        count1++;
        return "1";
        }
    if(a == 1){
        count2++;
        return "2";
        }
    if(a ==2){
        count3++;
        return "3";
        }
    if(a == 3){
        count4++;
        return "4";
        }
    if(a == 4){
        count5++;
        return "5";
        }
    if(a == 5){
        count6++;
        return "6"; 
        }
    }
int printStats(){
        System.out.println("1:" + count1);
        System.out.println("2:" + count2);
        System.out.println("3:" + count3);
        System.out.println("4:" + count4);
        System.out.println("5:" + count5);
        System.out.println("6:" + count6);
    }

void reset(){
    count1 = 0;
    count1 = 0;
    count3 = 0;
    count4 = 0;
    `enter code here`count5 = 0;
    count6 = 0;
            }

            }

Upvotes: 0

Views: 409

Answers (4)

Nir Alfasi
Nir Alfasi

Reputation: 53525

First, don't ever duplicate code like you did here - if you can write it much more elegantly using for loops:

import java.util.Random;

public class Dice {

    Random rnd=new Random();
    int[] count = new int[6];

    String roll(){
        int a = rnd.nextInt(6);
        count[a]++;
        return (a+1) + ""; // By the way, why do you bother returning 
                           // a string ? or any value at all ?
    }

    void printStats(){
        for(int i=0; i<count.length; i++){
            System.out.println((i+1) + ":" + count[i]);
        }
    }

    void reset(){
        for(int i=0; i<count.length; i++){
            count[i] = 0;
        }
    }

}

Second, when you declare a method like: int printStats() it means that this method should return an int, since it doesn't - you are getting a compilation error.

Third, if you want this code to actually run - you should add a main method, for example:

    public static void main(String[] args) {
        Dice dice = new Dice();
        for(int i=0; i<10; i++){
            dice.roll();
        }
        dice.printStats();
        dice.reset();
        //etc
    }

Forth (and last), pay attention that if you want potentially to get 6 different random integers you should declare:

int a = rnd.nextInt(6);// returns an int in the range [0-5]

not:

int a = rnd.nextInt(5);

Upvotes: 1

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

Your roll() method contains a path that doesn't execute a return statement (ie when a<0 or a>5 then none of the if conditions would be true) - even though logically this can never happen, the compiler doesn't know how to infer that.

Your printStats() method is declared with return type int, but never returns any value.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

The methods signature indicates it will return a primitive int. The class fails to compile because the compiler realizes the method does not return an int and throws the compilation error you receive.

A return type is specified within a method signature after its scope and before the method name. The return type indicates the type of the primitive or object the method will return when called. If no object is returned by a method, a return type of void is used.

In this case, the method should not return anything, which is indicated by specifying a return type of void in the method's signature.

   void printStats(){
        System.out.println("1:" + count1);
        System.out.println("2:" + count2);
        System.out.println("3:" + count3);
        System.out.println("4:" + count4);
        System.out.println("5:" + count5);
        System.out.println("6:" + count6);
    }

Upvotes: 0

nhgrif
nhgrif

Reputation: 62052

Part of the problem is that your roll() method doesn't have a return for every possible execution path.

Now, you and I know that your logic guarantees one of the if statements will be true, and you'll return one of those, but because all of your return statements are within if statements, it won't compile.

There are three easy fixes.

  1. Change your list of if statements to a big if-else structure, and simply change if(a==5) to the last else.
  2. Use a switch statement, and use case 0-4, with 5 being default.
  3. Just add return ""; at the end of the current method, outside any if statement.

And for your printStats() method, you just need to change the return type to void.

Upvotes: 1

Related Questions