We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,408 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help! Beginner Problem. null being printed out at the end of the program

public String roll()
    {
        Random generate = new Random();
        int diceRoll; 
        int firstNum;
        int secondNum;
        boolean winner = false;


            for (int x=1; x<=3; x++)
            {
                firstNum = generate.nextInt(6)+1;
                secondNum = generate.nextInt(6)+1;
                diceRoll = firstNum + secondNum;
                System.out.println("Roll number " + x + " was " + diceRoll);

                if(userNum == diceRoll)
                {
                    winner = true;
                    break;
                }
            }

            if(winner)
            {
                System.out.println("\nYou win!! Congratulations");
            }   
            else
            {
                System.out.println("\nYou lose");
            }


        return roll;
    }

When I run This class on my driver class at the end of the whole loop I get my numbers and all but I also get the word null at the end.
Just like this:

Roll number 1 was 6
Roll number 2 was 8
Roll number 3 was 2

You win!! Congratulations
null

3
Contributors
2
Replies
6 Hours
Discussion Span
7 Months Ago
Last Updated
3
Views
joseph.henriquez
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Where is the println statement that prints out the null?
What is the value of the roll variable that is returned by the roll() method?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Line 34, you are returning roll variable but you didn't have anything to do with the variable inside the method. The method is supposed to build up a String and return it?

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0735 seconds using 2.78MB