By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Returning first digit of numbers with charAt

P: 1
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
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
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

Expert 10K+
P: 11,448
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

Post your reply

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