Returning first digit of numbers with charAt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • penguinbob31
    New Member
    • Mar 2008
    • 1

    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.

    Code:
    import java.io.*;
    import java.util.Scanner;
     
    public class benford
    {
    	public static void main(String[]args)throws IOException
    	{
    		String number;
    		Scanner keyboard = new Scanner(System.in);
    		System.out.print("Enter the name of the file: ");
    		String filename = keyboard.nextLine();
    		File file = new File(filename);
    		Scanner inputFile = new Scanner(file);
    		while (inputFile.hasNext())
    		{
    			number = inputFile.nextLine();
    			firstDigit(number);
    		}
    		System.exit(0);
    	}
    	public static String firstDigit(String number)
    	{
    		int count = 0;
    		int one, two, three, four, five, six, seven, eight, nine;
    		switch(number.charAt(0))
    		{
    			case '1':
    						one = count + 1;
    						break;
    			case '2':
    						two = count + 1;
    						break;
    			case '3':
    						three = count + 1;
    						break;
    			case '4':
    						four = count + 1;
    						break;
    			case '5':
    						five = count + 1;
    						break;
    			case '6':
    						six = count + 1;
    						break;
    			case '7':
    						seven = count + 1;
    						break;
    			case '8':
    						eight = count + 1;
    						break;
    			case '9':
    						nine = count + 1;
    						break;
    		}
    		System.out.println("1: " + one);
    		System.out.println("2: " + two);
    		System.out.println("3: " + three);
    		System.out.println("4: " + four);
    		System.out.println("5: " + five);
    		System.out.println("6: " + six);
    		System.out.println("7: " + seven);
    		System.out.println("8: " + eight);
    		System.out.println("9: " + nine);
    		return number;
    	}	
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      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

      Comment

      Working...