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

  • SEARCH
  • Home
  • 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 593535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:51:22+00:00 2026-05-13T15:51:22+00:00

FYI. Another practice exercise that I’m stuck on. The problem is to find the

  • 0

FYI. Another practice exercise that I’m stuck on. The problem is to find the number of occurences of each digit in a string. I feel like I’m pretty close, but I’m getting some whacky results. I’m a beginner, so please keep hints/help at my level if possible. Thanks!

Here is what I have so far:

import java.util.Scanner;

    public class Practice5 {
 public static void main(String[] args) {
  Scanner input= new Scanner(System.in);
  System.out.println("Enter a string");
  String s = input.next();

  int[] counts = countNumbers(s);

  String output = "";

  for (int i = 0; i < counts.length; i++) {
   if (counts[i] != 0)
                        {
           output += "Digit" + (char)('0' + i) + " appears " +
           counts[i] + ((counts[i] == 1) + " times\n");
                        }

           }

                System.out.print(output);
           }


 private static int[] countNumbers(String s) {
  int[] counts = new int[10];

  for(int i = 0; i < s.length(); i++) {
   if (Character.isDigit(s.charAt(i)));
   counts[s.charAt(i) - '0']++;
  }
  return counts;
 }
}

Ok. Now if I enter a string like “23 t5t6 u76u 232 g1”

I get:

2 appears 1 time
3 appears 1 time

This is obviously incorrect. It should be 2 appears 3 times, 3 occurs 2 times, 5 occurs 1 time, etc. Any help would be appreciated.

  • 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-05-13T15:51:22+00:00Added an answer on May 13, 2026 at 3:51 pm

    Your first problem is with the use of the Java scanner. If I am not mistaken, you are only reading the first substring up to the first space. You probably wanted to run over the entire string, so a scanner is not the right thing to use.

    Once you get the entire string, you still have a problem.
    The major problem in your code is that in the line that reads

    if (Character.isDigit(s.charAt(i)));
    

    you have a semicolon at the end.
    As a result of this, the next line (where you update the values) is always executed. This is a problem because sometimes your character would not be a digit, so the index “character-‘0′” could fall outside the 10 that you have defined. This should be caught by Java to create an exception. You’re not seeing this exception because your code would only process the first “23”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

FYI - I am a mainframer that moved to the .NET world a couple
I'm trying to launch another process from a service (it's a console app that
-Edit- FYI.. I am converting b&w documents scanned in as greyscale or color. 1)The
I'm considering using Django for a project I'm starting (fyi, a browser-based game) and
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
Is it a good practice to use Assert for function parameters to enforce their
I'm trying to figure out the best way to handle a simple problem: I
FYI I'm programming in objective-C but anyone might be able to help. I've been
FYI I'm pretty new to Silverlight. Okay, so I want to build a simple
I need to profile a server-client Java application (based on Jersey/REST FYI). I have

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.