473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Returning first digit of numbers with charAt

I'm writing a program that is supposed to take the first digit of every number in a list of numbers and count how many times each first digit appears. I can't seem to get the program to work properly. Any help would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class benford
  5. {
  6.     public static void main(String[]args)throws IOException
  7.     {
  8.         String number;
  9.         Scanner keyboard = new Scanner(System.in);
  10.         System.out.print("Enter the name of the file: ");
  11.         String filename = keyboard.nextLine();
  12.         File file = new File(filename);
  13.         Scanner inputFile = new Scanner(file);
  14.         while (inputFile.hasNext())
  15.         {
  16.             number = inputFile.nextLine();
  17.             firstDigit(number);
  18.         }
  19.         System.exit(0);
  20.     }
  21.     public static String firstDigit(String number)
  22.     {
  23.         int count = 0;
  24.         int one, two, three, four, five, six, seven, eight, nine;
  25.         switch(number.charAt(0))
  26.         {
  27.             case '1':
  28.                         one = count + 1;
  29.                         break;
  30.             case '2':
  31.                         two = count + 1;
  32.                         break;
  33.             case '3':
  34.                         three = count + 1;
  35.                         break;
  36.             case '4':
  37.                         four = count + 1;
  38.                         break;
  39.             case '5':
  40.                         five = count + 1;
  41.                         break;
  42.             case '6':
  43.                         six = count + 1;
  44.                         break;
  45.             case '7':
  46.                         seven = count + 1;
  47.                         break;
  48.             case '8':
  49.                         eight = count + 1;
  50.                         break;
  51.             case '9':
  52.                         nine = count + 1;
  53.                         break;
  54.         }
  55.         System.out.println("1: " + one);
  56.         System.out.println("2: " + two);
  57.         System.out.println("3: " + three);
  58.         System.out.println("4: " + four);
  59.         System.out.println("5: " + five);
  60.         System.out.println("6: " + six);
  61.         System.out.println("7: " + seven);
  62.         System.out.println("8: " + eight);
  63.         System.out.println("9: " + nine);
  64.         return number;
  65.     }    
  66. }
Mar 17 '08 #1
2 2444
r035198x
13,262 8TB
Do this in small steps.
First you need to get the first character in a variable.
Then you need to loop through the string counting the occurences of that character.
Mar 17 '08 #2
JosAH
11,448 Expert 8TB
Do this in small steps.
First you need to get the first character in a variable.
Then you need to loop through the string counting the occurences of that character.
The question is ambiguous; given this part: "count how many times each first
digit appears" the OP has to add a bit such as:

- as a first digit (the OP's interpretation)
- as a digit in the number(s) (your interpretation).

kind regards,

Jos
Mar 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is...
2
by: MLH | last post by:
Using A97, I have tables with 10-char text phone number fields to allow entry of xxxyyyyyyy phone numbers - where xxx is the 3-digit area code and yyyyyyy is the 7-digit phone number. I wish to...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
17
by: Kermit Piper | last post by:
Hello, I have been searching, Googling, searching. Cannot find a javascript to calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs to verify the correct check digit. I have...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
9
by: sunita jadhav | last post by:
my question is if i type in html textbox on key press event suppose i type 12345 values in textbox then i delete or edit any value of text box suppose i edit 3 and i insert the value 6 at 3 but i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.