Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:13:31+00:00 2026-06-06T04:13:31+00:00

I recently made a very simple practice program in Python, that takes user input

  • 0

I recently made a very simple practice program in Python, that takes user input and rolls dice. The code is:

import random
import sys
import math

def roll(rolls, sides, results):
    for rolls in range(1, rolls + 1):
        result = random.randrange(1, sides + 1)
        print result
        results.append(result)
def countf(rolls, sides, results):
    i = 1
    print "There were", rolls, "rolls."
    for sides in range(1, sides + 1):
        if results.count(i) != 1:
            print "There were", results.count(i), i,"s."
        else:
            print "There was", results.count(i), i
        i = i + 1
        if i == sides:
            break
    rolls = input("How many rolls? ")
        sides = input("How many sides of the die? ")
        results = []

        roll(rolls, sides, results)
        countf(rolls, sides, results)

(actually this is part of a larger program, so I had to cut’n’paste bits, and I might have missed something out).

And so I decided to translate that to Java. Notice the algorithm here: get random number, print it, append it to an array, then count the amount of each number in the array at the end, and print out that value. Problem is, I don’t know how to do the equivalent of someArray.count(someIndex) in Java syntax. So my Java program looks like this so far:

import java.util.*;

public class Dice {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        final static int TIMES_TO_ROLL = getInt("Times to roll?");
        Random flip = new Random();
        int[] results = new int[TIMES_TO_ROLL];
        for (int i = 0; i < TIMES_TO_ROLL; i++) {
            int result = flip.nextInt(6);
            System.out.println(result);
            results[i] = result;
        }
    }
    public static int getInt(String prompt) {
        System.out.print(prompt + " ");
        int integer = input.nextInt();
        input.nextLine();
        return integer;
    }
}

So can someone help me with the array counting code? I understand that this might not be a defined method, since Python is higher level after all, so I could make my own array counting method, but I was wondering if Java, like Python, has a predefined one.

EDIT: I managed something like this:

public static int arrayCount(int[] array, int item) {
    int amt = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == item) {
            amt++;
        }
        else {
            amt = amt;
        }
    }
    return amt;
}

EDIT: Just out of interest, assuming I use Command prompt to run my Java program and Python.exe (command prompt console for Python), which one will be faster (in other words, for the same code, which language has better performance?)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:13:35+00:00Added an answer on June 6, 2026 at 4:13 am

    There are a couple libraries that will do this for you:

    1. Google Guava’s MultiSet
    2. Apache Common’s Bag

    But for something so simple, you may consider an extra library a bit excessive.

    You can also do this yourself with an int[]. Assuming your dice is using whole numbers, have the number rolled refer to the index into the array, and then increment the value at that index. When you need to retrieve the value for a given number, look up its value by the index.

    private static final int NUMBER_DICE_SIDES = 6;
    public static void main(String[] args) {
        final static int TIMES_TO_ROLL = getInt("Times to roll?");
        Random flip = new Random(NUMBER_DICE_SIDES);
        int[] results = new int[NUMBER_DICE_SIDES];
        for (int i = 0; i < TIMES_TO_ROLL; i++) {
            int result = flip.nextInt;
            System.out.println(result);
            results[result]++;
        }
    
        for(int i = 0; i < NUMBER_DICE_SIDES; ++i) {
            System.out.println((i+1)+"'s: " + arraysCount(results, i));
        }
    }
    
    public static int arrayCount(int[] array, int item) {
        return array[item];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently made a program that is supposed to take two environment variables,
I recently made a simple game where user can submit his/her high scores online.
I was recently making a very simple application that just printed matrix-effect out to
I recently made a Google Apps Script that runs onSubmit of a Google form.
I recently made a c# printer management tool that uses a WCF service which
Let me set the stage here. I'm a very junior developer who's recently made
Recently I have discovered that my release executable (made with msvc++ express 2008) becomes
I recently made a new tablespace and associated a user with it (sonar). Currently,
As recently as several years ago, the developers actually made the builds that went
I've recently inherited an application that makes very heavy use of session, including storing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.