Dice rolling through a string [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a program that takes the string "1d8" and make the program identify it as int i = (Int) ((Math.Random()*8)+1) one time. It would also be nice if I could make it identify "10d8" to do something like :
for(int i = 1; i <= 10; i++){
int j += (int) ((Math.Random()*8)+1);
}
Thus returning basically the roll of 10 eight sided dice. So my question is how do I get the code to recognize the numbers on either side of the character d and make this work with whatever roll I do.

You can use split method of the String class.
String s = "10d8";
String[] numbers = s.split("d");
numbers[0] will have 10 and numbers[1] will have 8

Related

Is there a way to do many similar tasks in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
Say for instance I want to repeat the line of code
Integer int1 = new Integer(value1);
for many variables, such as int1 to int100. I am not asking about this exact task in particular - I am asking about any such situation where the code would be identical save for replacing small details like int1 and value1 with int2, value2. Is there a way to have the JVM complete this for me?
I am not even sure what approach to take on this or what term to search for more information. The only thing I can think to try is instead of typing "int1", having a loop that changes a string containing the name and attempting to pass the string as a symbol to the JVM but this of course does not work.
It was a little strange question and I don't know if I understood your meaning correctly or not.
But in this particular case, instead of repeating the code, you can use a data structure like an array. See Oracle tutorial.
int[] numvar = new int[arr.length];
for (int i = 0; i < args.length; i++) {
int someNumber = Integer.parseInt(args[i]);
numvar[i] = someNumber;
}

How come for Integer arrays, as in Integer arr[] = new Integer[], you can't change their value, you can only set them? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was doing a problem in Codeforces and had to use an Integer arr[] = new Integer[200001]. I wanted to store 200001 counters for their index values(for example, if I got 19 then arr[19] would be the number of 19s that appeared in the array). However, when I went to do arr[num]++ or arr[num] += 1, it would not work. Only when you set arr[num] to some number does it work. Is there a way to get around that?
It wouldn't work because you haven't initialized each element of the array, you just have an Integer array with null references.
you should first do:
for(int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
The easiest way is to use an int array instead of an Integer array.

How can I check if an ArrayList contains a specific number of instances of a certain element? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I'm programming a Go Fish game for a project of mine, and I've been at this for a while. Currently I'm trying to check if the player/computer has a full book of a certain number. I've based the whole game off of arrays, and just need to check if the array contains four instances of a given element.
I'm not sure about the specifics but this should get you started.
int count = 0;
for(int i = 0; i < YOURARRAY.length; i++)
{
if( //check to see if the element is the type youre counting)
{
count++;
}
}
if(count == 4)
{
//there are 4 instances
}

Java program to find a pallindrome [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am a beginner in Java programming.
I was a making a program to find if the entered word is a pallindrome or not can someone please tell me the logic i should use to make the given program?
boolean isPalindrome(String input) {
for (int i=0; i < input.length() / 2; ++i) {
if (input.charAt(i) != input.charAt(input.length() - i - 1)) {
return false;
}
}
return true;
}
This solution is self explanatory, the only edge case requiring explanation is what happens for words with an odd number of letters. For an input containing an odd number of letters, the middle element will not be touched by the loop, which is OK because it has no effect on whether the input is a palindrome.

Generate a random integer within a given range AND is even ONLY [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)

Categories

Resources