Re: Stuck in while loop prison.
 
mmoski wrote:
Hmm... it should be just... enter the input, hit return, and the while
loops excecute.  Is there a way to use return or /n or something as a
condition for escaping the loop.
Perhaps you want to inout a series of numbers on one line like this?
12 5 6 8 9
9 Hi-5!  It works!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class BitStat {
     public static void main(String[] args) {
         // read one line containing one or more integer numbers
         BufferedReader br = new BufferedReader(
                 new InputStreamReader(System.in));
         String numbers = null;
         try {
             numbers = br.readLine();
         } catch (IOException e) {
             e.printStackTrace();
         }
         // mmoski's code
         Scanner sc = new Scanner(numbers);
         int totalOnes = 0;
         String input;
         while (sc.hasNext()) {      // Note!
             input = sc.next();
             int inputHex = Integer.parseInt(input, 16);
             String usBinary = Integer.toBinaryString(inputHex);
             // Reveal the truth!
             System.out.println(input+" -> "+inputHex+" -> "+usBinary);
             int stopCase = usBinary.length();
             while (stopCase > 0) {
                 String currentChar = usBinary.substring(0, stopCase);
                 String test = currentChar.substring(
                         (stopCase - 1),stopCase);
                 int testAsInt = Integer.parseInt(test);
                 if (testAsInt == 1) {
                     totalOnes++;
                 }// if
                 stopCase--;
             }// while
         }// while
         System.out.println(totalOnes + " Hi-5!  It works!");
     }// main
}// test
1 2 3 7 9 19 255 256 257
1 -> 1 -> 1
2 -> 2 -> 10
3 -> 3 -> 11
7 -> 7 -> 111
9 -> 9 -> 1001
19 -> 25 -> 11001
255 -> 597 -> 1001010101
256 -> 598 -> 1001010110
257 -> 599 -> 1001010111
28 Hi-5!  It works!
The rest of the code has what looks like bugs to me! The binary you get
for input 19 as a decimal number doesn't make much sense. Also if you're
only reading one line, you probably don't need scanner and could perhaps
do the job more simply with something like String.split()