Reputation: 37
I came across this little question at work and wanted to see if I could write a little java program to solve it. Apparently I can't write a damn thing in java. Lol I am stuck but have part of it worked out. I spend the better part of a two days trying to get it working an have had no luck. Here is my problem. I have a txt file with the numbers generated in order 1-1000 one number per line. I want to calculate how many times or the frequency of the number nine between 1 & 1000. My approach is as follows:
3-A. Compare with charAt(0) to a nine and increment if appropriate
3-B. Convert the string to a char array and compare index 0 in the char Array to a nine and increment if appropriate.
Side Note: You must comment out the else if comparing charArray[1] == '9' for it to compile. If you comment that out it will work perfectly for counting the nines in the first digit location. The second and third digit comparisons don't work. Thanks and sorry for the dumb question.
An easy way to generate the text file is to use excel type in a column 1 then 2 in the second row and then 3 in the third row (same cell) and then highlight all three numbers and drag the plus sign in the bottom right corner of the cell containing the three. This will fill down to 1000. I am not attaching my txt file since you can easily generate your own.
**Not homework
My code so far is as follows:
package com.numbers;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Numbers
{
static int[] array = new int[1002];
static char[] charArray = new char[4];
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner (System.in);
System.out.print("Enter the name of the file : ");
String whatfile = scan.nextLine();
Scanner readnumber = new Scanner (new FileReader(whatfile));
int firstDigit = 0;
int secondDigit = 0;
int thirdDigit = 0;
for (int i = 0; i < 1000; i++)
{
array[i] = readnumber.nextInt();
String testString = "";
// System.out.println("the number being passed to parse is : " + numberAtIndex);
// Approach 1
testString = Integer.toString(array[i]);
if(testString.charAt(0) == '9')
{
firstDigit = firstDigit + 1;
}
// System.out.println("test string contains : " + testString);
// Approach 2
charArray = testString.toCharArray();
if(charArray[0] == '9')
{
firstDigit = firstDigit + 1;
}
/*
* If you comment out this 'else if' it will work. please comment out one of the approaches above (use only 1) My problem is that
* I can't seem to figure out how to search for nines in the second & third digit location.
* If it is in an array shouldn't I be able to compare the charArray[1] to a nine and have it increment if true?
*/
else if (charArray[1] == '9')
{
secondDigit = secondDigit + 1;
}
} // end for
// System.out.println("this is what is stored in array spot 0 : " + array[0]); //should be 1
System.out.println("the number of 9's in the first digit place holder is : " + firstDigit);
System.out.println("the number of 9's in the second digit place holder is : " + secondDigit);
// System.out.println("the number of 9's in the third digit place holder is : " + thirdDigit);
} // End of main
} // end of class
Upvotes: 1
Views: 11066
Reputation: 895
Do you have to pull the numbers from a text file? Try this:
public class NineCounter {
public static void main(String [] args) {
System.out.println(getNumberOfNines(1, 1000));
}
public static int getNumberOfNines(int from, int to) {
if (from > to || from < 0 || to < 0) {
return -1;
}
int numberOfNines = 0;
int numberOfDigits = Integer.toString(to).length();
for (int i = from; i < to; i++) {
int currentNumber = i;
for (int j = 0; j < numberOfDigits; j++) {
if (currentNumber % 10 == 9) {
numberOfNines++;
}
currentNumber -= currentNumber % 10;
currentNumber /= 10;
}
}
return numberOfNines;
}
}
This gives an output of 300 9's.
If you must use the text file, you can use part of the getNumberOfNines()
function on integers generated from a string with Integer.parseInt()
.
Upvotes: 0
Reputation: 77044
So my first thought isn't "iterate through all numbers", but rather, come up with a formulaic way to determine the count...note that:
Number of 9s in 1000
= (1000/10)*1 + (1000/100)*10 + (1000/1000)*100 = 100*1 + 10*10 + 100*1 = 300
Notably, this also gives you the per-digit counts, the first rule gives you the number of digits in the 1s place, the second rule gives you the number of digits in the 10s place, and the third rule gives you the number of digits in the 100s place.
Supposing you doubt this, here's a more concise Java program that counts 9s by iteration:
int nines = 0;
for(int i = 1; i <= 1000; i++){
for(char c : String.valueOf(i).toCharArray()){
if(c == '9') nines++;
}
}
Upvotes: 8
Reputation: 5290
public static void main(){
...
Scanner sc = new Scanner (new File(whatfile));
int[] occ = new int[3];
for(int i=0; i<3; i++) occ[i] = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
try {
int num = line.parseInt(line);
for (int j=0; j<3 && num>0; j++){
if (num%10==9) occ[j]++;
num /= 10;
}
} catch (Exception e) {}
}
... print occ[] ....
}
Upvotes: 0
Reputation: 48196
instead of
if(charArray[0] == '9')
{
firstDigit = firstDigit + 1;
}
do
for(char c:charArray){
if(c == '9')
{
firstDigit = firstDigit + 1;
}
}
however you an also convert a int to its digits directly (without needing a file) with
int tmp=i;
while(tmp>0){
int digit=tmp%10;//get last digit
tmp= tmp/10;//integer division rounds down
if(digit==9)firstDigit++
}
Upvotes: 0