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; } }
Comment