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 7992489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:34:40+00:00 2026-06-04T13:34:40+00:00

I am trying to write a program in java which can count number of

  • 0

I am trying to write a program in java which can count number of ‘1’ in a range of numbers.

For examples: if we look from range 1 – 20 we will get 12 1’s
1, 2,3….9, 1 0, 1 1 …. 1 9, 20.

here is the code i have written.

public class Count_no_of_ones
{
public static void main( String args[] )
{
    int count = 0;

    for ( int i = 1; i<=20; i++ )
    {
      int a=i;
      char b[] = a.toString().toCharArray(); //converting a number to single digit array

      for ( int j = 0; j < b.length; j++ )
      {
        if( Integer.parseInt(b[j]) == 1 )
        {
            count++; // checking and counting if the element in array is 1 or not.
        }
      }
    }

    System.out.println("number of ones is : " + count);
} 

}

I am getting two errors on compiling.

D:\Programs\Java>javac Count_no_of_ones.java

Count_no_of_ones.java:10: error: int cannot be dereferenced
char b[] = a.toString().toCharArray(); //converting a number to single digit array
            ^
Count_no_of_ones.java:14: error: no suitable method found for parseInt(char)
if( Integer.parseInt(b[j]) == 1 )
           ^
method Integer.parseInt(String) is not applicable
(actual argument char cannot be converted to String by method invocation conversion)

method Integer.parseInt(String,int) is not applicable
(actual and formal argument lists differ in length)

2 errors
D:\Programs\Java>

Can you also explain what i did wrong in the code. i never had problem with Integer.parseInt and this de-referencing problem is new to me. i just heard about it in awt class but i never actually faced it.

  • 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-04T13:34:41+00:00Added an answer on June 4, 2026 at 1:34 pm

    You can’t call methods on primitive types in Java. Use the static method Integer.toString instead:

    char b[] = Integer.toString(a).toCharArray();
    

    You also don’t actually need to convert to a character array. You can index into a string using charAt.


    The method parseInt accepts a string, not a char, so this line doesn’t work:

    if( Integer.parseInt(b[j]) == 1 )
    

    Instead do the comparison with the char '1':

    if (b[j] == '1')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a Java program or Hadoop Pig script which will
I've been trying to write a program in java that can download a number
I'm trying to follow Java's JDBC tutorials to write a Java program that can
I am trying to write a small java program that will accept a file
I am trying to write a program (prob in java) to join a number
I am new to programming and Java and trying to write a program which
I am trying to write a program which will allow two players to play
I'm trying to write a program in Java that uses osql to generate a
I'm trying to write a program in Java that looks for patterns of strings
i am trying to write a program in java (on linux) using RandomAccessFile class

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.