Computer Science Canada

Help with loop/if clause

Author:  CluelessWithJava [ Fri Oct 23, 2009 1:01 am ]
Post subject:  Help with loop/if clause

I need a little help with my program. Basically, I want my program to output the digits of a user inputted integer on seperate lines (I.E the ones digit would be 0 1 1 1 0 for the integer 1110)The integer can go up to 1 billion and the digits must be checked starting from the 1 billionth point, But I also want the program to not output the zeros preceding the actual number (So for 111 it would output 1 1 1 not 0 0 0 0 0 0 0 1 1 1).

I'm having a little trouble with the clause though that would remove the preceding zeros. I could appreciate any tips or help, my code with the clause is below.

Java:

*Displays digits of user inputted integer seperately.
*/

   import java.util.Scanner;

    public class DigitsDisplay
   {
       public static void main (String[] args)
      {
         int uinput, digit; //User input and the Digit to be outputted
         int divisor = 1000000000; //Maximum value that the user input should not exceed
         boolean zeros = false; //Boolean clause so that the preceding zeros are not shown
         Scanner input = new Scanner (System.in);
     
         System.out.print ("Enter a positive integer: ");
         uinput = input.nextInt();
         input.close();
     
         while (zeros = false) // While the zero variable is false (trying to get the divisor to the lowest value before it returns the first number other than 0, basically trying to get rid of all the preceding zeros)
         {
            if (uinput / divisor == 0) // Basically when the digit would equal 0
            {
               zeros = false; // Zero variable remains false to repeat the loop
               divisor = divisor / 10// Divisor loses a zero (i.e 1000 becomes 100 Didn't know how to explain it better =3)
            }
            if (uinput / divisor != 0) // If the digit would equal a int other than 0
                zeros = true; //Zero variable becomes true and exits the loop
         }
         
         while (divisor >= 1) // continues the loop until the divisor is less than or equals 1
         {
            digit = uinput / divisor; //Finds the digit at the certain place value (i.e the hundreds digit would be digit/100)
           
            System.out.println (digit); //Prints out that digit
           
            uinput = uinput%divisor; // Makes the user input the remainder of the previous equation (i.e 139/100 would become 39)
            divisor = divisor / 10;     
         }
     
                                                                        
      }//End of Main Method        
   
   }//End of DigitsDisplay


Thanks for your time.

Author:  Zren [ Fri Oct 23, 2009 1:34 am ]
Post subject:  RE:Help with loop/if clause

Wouldn't converting the number to a string, then printing out each character have been a simpler approach?

Forgot this:
while (zeros == false)

Why not use an else statement instead of the !=0 condition?

Not sure if that's all of it since I don't have Java set up at the mo.

Author:  CluelessWithJava [ Fri Oct 23, 2009 7:52 pm ]
Post subject:  Re: Help with loop/if clause

I seemed to have found a simpler way to do it from my friend =S

Java:
*Displays digits of user inputted integer seperately.
*/

   import java.util.Scanner;

    public class DigitsDisplay
   {
       public static void main (String[] args)
      {
         int uinput, digit; //The user input variable and digit variable
         int divisor = 1000000000; //The maximum interger/ digit to be outputted
         Scanner input = new Scanner (System.in);
     
         System.out.print ("Enter a positive integer: ");
         uinput = input.nextInt();
         input.close();
       
         while (divisor > uinput)//Loop to get rid of the preceding zeros, The condition means that the loop will continue until the divisor is small enough to divide the number and recieve a value other than 0
         {
            divisor = divisor / 10; //Lowers the number by 1 digit\divides the divisor by 10
         }
       
         while (divisor >= 1)//While the divisor is greater than 1 or equal to 1
         {
            digit = uinput/divisor; //Finds the digit at the certain spot
                
            System.out.println (digit);//Outputs that digit on a new line
                
            uinput = uinput%divisor;//The uinput now becomes the remainder of the previous equation to continue the equation               
            divisor = divisor/10;//The divisor is lowered by 1 digit\divided by 10
         }           
                                                                        
      }//End of Main Method        
   
   }//End of DigitsDisplay

Author:  Shah-Cuber [ Fri Oct 23, 2009 8:17 pm ]
Post subject:  Re: Help with loop/if clause

Your welcome Smile


: