DiceRoller.java

You are to write a program which will ask the user for the number and type of dice they would
like to roll in the form of “xdy”. The x represents the number of dice, and the y represents how
many sides the dice have. Your program MUST read the input as a string, then parse the data.
You may assume that the input is entered in the form of an integer, followed by the character “d”
followed by an integer.

Once you have the number of dice you will use the commands below to generate that many
random numbers between 1 and the largest number on the dice. You must display each value
that is rolled and then report the results to the user as well as the sum of all the rolls.

Input: Number and size of dice (String)

import java.util.Random;
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(n);<-- generates a random number between 0 and n-1
The program should ask for input and display output clearly and using English. For example:

Please enter the number and type of dice to roll.
5d8
Rolling: 2, 6, 1, 8, 6
The total amount rolled is 23.

I am having a lot of trouble and any help would be awesome :)

Dani AI

Generated

A concise, robust approach that fixes both issues mentioned by (parsing the "xdy" input and printing rolls as a comma-separated list) is to parse the input into two integer variables (for example count and sides), validate them, generate each roll into a StringBuilder while accumulating the sum, and then print the formatted line. was correct to point out that the parsed integers should be kept in variables so they can be reused for display or further logic.

The code below demonstrates the full flow (parsing, validation, comma-separated output, sum). It uses ThreadLocalRandom to produce values in the 1..sides range and avoids trailing commas.

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

Scanner in = new Scanner(System.in);
String input = in.nextLine().trim().toLowerCase();
int d = input.indexOf('d');
if (d < 0) throw new IllegalArgumentException("Format must be xdy");
int count = Integer.parseInt(input.substring(0, d));
int sides = Integer.parseInt(input.substring(d + 1));
if (count <= 0 || sides <= 0) throw new IllegalArgumentException("Numbers must be positive");
if (count > 1000) throw new IllegalArgumentException("Too many dice");

StringBuilder list = new StringBuilder();
int total = 0;
for (int i = 0; i < count; i++) {
  int roll = ThreadLocalRandom.current().nextInt(1, sides + 1);
  total += roll;
  if (i > 0) list.append(", ");
  list.append(roll);
}
System.out.printf("Rolling: %s%nThe total amount rolled is %d.%n", list.toString(), total);

Troubleshooting notes: accept uppercase 'D' by using toLowerCase() (as above), catch NumberFormatException to print a friendly error when the integers are malformed, and enforce a sensible count cap to avoid huge output. For testing, use a fixed seed or print fewer rolls. If a frequency summary is preferable for large counts, build a histogram instead of listing every roll.

Recommended Answers

All 5 Replies

it would be nice if you would exert an effort doing it rather than typing that here, no one is gonna do your homework for you, you can atleast try,

it would be nice if you would exert an effort doing it rather than typing that here, no one is gonna do your homework for you, you can atleast try,

i know. im sorry. i didnt mean for it to appear that i wanted someone to do it for me... i have been trying at it for 4 days now. the parts i am having trouble with is getting "xdy" to appear and to get the rolling results to appear in the "2,3,4,5,6,etc" fashion. any help with that would be awesome!

i am having trouble with is getting "xdy" to appear

Hard to recommend changes to your code if there is no code to be changed.

Hard to recommend changes to your code if there is no code to be changed.

could u give me an example of how I would get "xdy" to appear?
I dont get how to save/keep the x and y the user would input.

an example of how I would get "xdy" to appear?

System.out.println(x + "d" + y); // show xdy

how to save/keep the x and y the user would input.

x = <what the user input>; // save what the user input in x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.