2

The question asks: Design and implement an application that first reads a list of 10 three-digit integers and then counts the number of appearances for each digit from 0 to 9.

Here is an example of 3 three-digit numbers
Input number [123, 456, 789]
Output:
Digit 0 has appeared 0 times
Digit 1 has appeared 1 times
Digit 2 has appeared 1 times

Digit 9 has appeared 1 times

I believe that I have figured out the correct formulas to computate the amount of times each number appears, but I am not sure how to create the array and begin searching through it. I think I need a while and a for loop, but I am not sure how to incorporate them. I am afraid however, that my current if statements will need to change once the loops are implemented. Am I going in the right direction here? Any help would be greatly appreciated!

double i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
int c0=0, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, c8=0, c9=0;

Scanner scan = new Scanner(System.in);

System.out.println ("Enter 10 3-digit integers");

//Counts the 1st number
System.out.println ("Enter first 3-digit integer");
i1 = scan.nextDouble();

    if (i1%10==0)
    c0++;
    if (i1%10==1)
    c1++;
    if (i1%10==2)
    c2++;
    if (i1%10==3)
    c3++;
    if (i1%10==4)
    c4++;
    if (i1%10==5)
    c5++;
    if (i1%10==6)
    c6++;
    if (i1%10==7)
    c7++;
    if (i1%10==8)
    c8++;
    if (i1%10==9)
    c9++;

    if ((i1%100>=0) & (i1%100<10))
    c0++;
    if ((i1%100>=10) & (i1%100<20))
    c1++;
    if ((i1%100>=20) & (i1%100<30))
    c2++;
    if ((i1%100>=30) & (i1%100<40))
    c3++;
    if ((i1%100>=40) & (i1%100<50))
    c4++;
    if ((i1%100>=50) & (i1%100<60))
    c5++;
    if ((i1%100>=60) & (i1%100<70))
    c6++;
    if ((i1%100>=70) & (i1%100<80))
    c7++;
    if ((i1%100>=80) & (i1%100<90))
    c8++;
    if ((i1%100>=90) & (i1%100<100))
    c9++;


    if((i1/1000>=.0) & (i1/1000<.1))
    c0++;
    if((i1/1000>=.1) & (i1/1000<.2))
    c1++;
    if((i1/1000>=.2) & (i1/1000<.3))
    c2++;
    if((i1/1000>=.3) & (i1/1000<.4))
    c3++;
    if((i1/1000>=.4) & (i1/1000<.5))
    c4++;
    if((i1/1000>=.5) & (i1/1000<.6))
    c5++;
    if((i1/1000>=.6) & (i1/1000<.7))
    c6++;
    if((i1/1000>=.7) & (i1/1000<.8))
    c7++;
    if((i1/1000>=.8) & (i1/1000<.9))
    c8++;
    if((i1/1000>=.9) & (i1/1000<1.00))
    c9++;
2
  • 1
    +1 thanks for using the hw tag and giving good info/tries Commented Apr 16, 2011 at 7:38
  • 1
    Whoever voted to close; this is an example of a good homework question! Asking for help with a particular piece, and showing what he's done. There has obviously been an attempt, and now needs some help. Like nearly every other question on SO. 'Sorry, this is work you get paid for - do it yourself' doesn't cut it. It's the way you ask, not always what you ask. Commented Apr 16, 2011 at 7:42

4 Answers 4

1

Hints:

  1. Get rid of all of the c<n> and i<n> declarations and all of the if tests. They don't help you solve the problem.
  2. Make use of the fact that '0' + <n> == '<n>' where <n> is 0 .. 9.
  3. Or use Character.digit(char, 10). Read the javadocs.
  4. You don't use nextDouble() to read an integer. Read the javadocs.
Sign up to request clarification or add additional context in comments.

Comments

1

More Hints:

  1. Generalise the problem to 'count the distinct characters in a string'. Since you're reading the input as a string anyway, just forget that you're counting integers for the moment, and focus on counting char appearances within a string.

  2. Think of a data structure that can contain a pair of (distinct char, count).

  3. Iterate through the characters within a string, add the character to your data structure in point 2 if it doesn't exist with a default value of 0, then increment the count of the char you're currently looking at.

  4. When showing the output of your program, iterate through the entries of your data structure, validate that the char is an integer (if validation is important), and return the count for that char.

I'm not sure if you have trouble with iteration or not, but here is how to loop through the characters of a string:

String s = "123 456 789";

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);
    // process c
}

// or, if you know what 'smart loops' in Java are...

for(char c : s.toCharArray()) {
    // process c
}

To confuse matters, here it is in... 'pseudocode' (edited based on comments) ;)

>>> s = "012 345 678 900"
>>> datastructure = {}
>>> for c in s:
...     if c not in datastructure:
...         datastructure[c] = 0
...     datastructure[c] += 1
... 
>>> for c in '0123456789':
...     if c not in datastructure:
...         datastructure[c] = 0
...     print 'Digit %s appeared %d times' % (c, datastructure[c])
... 
Digit 0 appeared 3 times
Digit 1 appeared 1 times
Digit 2 appeared 1 times
Digit 3 appeared 1 times
Digit 4 appeared 1 times
Digit 5 appeared 1 times
Digit 6 appeared 1 times
Digit 7 appeared 1 times
Digit 8 appeared 1 times
Digit 9 appeared 1 times

4 Comments

Feel free to edit my question and remove the 'pseudocode' if you think it's inappropriate within an answer.
eghmmm space isn't part of int :)
and 12 in string is 12, when in his checking it would be (im guessing now, but hope he checked it) 012 when 0 occurs 1 time, not 0 :)
@dantuch, a fully validated solution didn't look as cool, but I've posted one anyway.
1

Your idea isn't that bad, you just obviously need some loops :)

1: loop to get numbers - you can't duplicate the same code 10x - write it once, in a loop.

2: loop to check digits - you can use your's ifs, or do it as Josh said but before - you need data structure to store your data - occurs of digits. 10 variables is bad, BAD:), idea.
use array[2][10] or simply array[10] (where number of digits = index of array and value = occurs) or even better - HashMap<> - google it.

then inside loop you do:

for(int i = 0; i<10; i++){
    if (myCurrentNumberToCheck %10 == i)
        array[i] = array[i] + 1; // approach with simple array[10]
    if ((myCurrentNumberToCheck %100 >= i*10) && (myCurrentNumberToCheck %100 < (i+1)*10 )) // needs to be tested
        array[i] = array[i] + 1;
    if ((myCurrentNumberToCheck %1000 >= i*100) && (myCurrentNumberToCheck %1000 < (i+1)*100 )) // needs to be tested
        array[i] = array[i] + 1;
}

ahh and your inserted numbers should be in some structure too - try List numbers = new ArrayList<int>() here :) and iterate thou list, to take a look at next number.

so you will need to add numbers to List, and nextly - go thou that list and check them and here use that loop i wrote above

Comments

0

the problem with readInt() and readDouble() is that it ignores leading 0 and trailing 0 after the decimal i.e. 01.10 => 1.1

A much simpler approach is to just read every character.

  int ch;
  while((ch = System.in.read())>=0) {
      // you have one character

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.